java 如何在 Servlet 2.4 版本的 init() 方法中获取 ContextPath

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13879869/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 14:18:47  来源:igfitidea点击:

How to get ContextPath in init() method of Servlet version 2.4

javaservletscontextpath

提问by Lama

I'm using version 2.4 of Servletand I need to get the ContextPaththrough the init()method which is called on server start, so I don't have any Request objectthat could call getContextPath()and because the Servlet version I do not have getContextPath()method in the ServletContexteither.

我使用的是 2.4 版Servlet,我需要ContextPath通过init()在服务器启动时调用的方法,所以我没有任何可以调用的Request 对象getContextPath()因为 Servlet 版本我也没有getContextPath()方法ServletContext

Is there a way to get this ContextPath()somehow ?

有没有办法以ContextPath()某种方式得到这个?

回答by Ian Roberts

One web application can be published at several different context paths, so thecontext path (singular) is only meaningful in the context of a particular request. Servlet 2.5 added getContextPath()to ServletContext, specified to return the "primary" context path for this web application, but there's no container-independent way to access this information in earlier spec versions.

一个Web应用程序可以在多个不同的上下文路径被公开,所以上下文路径(单数)是仅在特定请求的上下文中有意义。Servlet 2.5 添加getContextPath()ServletContext,指定返回此 Web 应用程序的“主要”上下文路径,但在早期规范版本中没有独立于容器的方式来访问此信息。

There may be tricks that work for certain containers, for example on Tomcat the ServletContext.getResource()method returns URLs with a custom scheme, of the form jndi://hostname/context/.... Thus you may be able to use

可能有一些技巧适用于某些容器,例如在 Tomcat 上,该ServletContext.getResource()方法返回带有自定义方案的 URL,形式为jndi://hostname/context/...。因此,您可以使用

ctx.getResource("/").getPath()

to get the context path on Tomcat (or possibly getResource("/WEB-INF/web.xml")and trim off the tail, as getResource()is specified to return nullif you ask it for a file that does not exist). You will have to experiment with different containers to find similar tricks that work on those.

获取 Tomcat 上的上下文路径(或者可能getResource("/WEB-INF/web.xml")并修剪掉尾部,如果您要求它提供一个不存在的文件,getResource()则指定返回null)。您将不得不尝试不同的容器以找到适用于这些容器的类似技巧。

回答by krampstudio

It seems to be only possible form servlet 2.5 as explained in this post: ServletContext getContextPath()

正如这篇文章中所解释的,似乎只有 servlet 2.5 才有可能:ServletContext getContextPath()

回答by Ernesto Campohermoso

You are right in Servlet 2.4 the object ServeltContext does not have the method getContextPath.

您在 Servlet 2.4 中是对的,对象 ServeltContext 没有 getContextPath 方法。

I can suggest two options:

我可以建议两种选择:

  1. Set the context path as parameter of the servlet:

    <servlet>

    <servlet-name>initServlet</servlet-name>
    
    <servlet-class>net.cirrus-it.InitServlet`</servlet-class>
    
    <init-param>
            <param-name>contextPath</param-name>
            <param-value>/myApp</param-value>
    </init-param>
    

    </servlet>

  2. Try to determine the context path from the method getRealPath()

  1. 将上下文路径设置为 servlet 的参数:

    <servlet>

    <servlet-name>initServlet</servlet-name>
    
    <servlet-class>net.cirrus-it.InitServlet`</servlet-class>
    
    <init-param>
            <param-name>contextPath</param-name>
            <param-value>/myApp</param-value>
    </init-param>
    

    </servlet>

  2. 尝试从方法 getRealPath() 确定上下文路径

http://tomcat.apache.org/tomcat-5.5-doc/servletapi/javax/servlet/ServletContext.html#getRealPath(java.lang.String)

http://tomcat.apache.org/tomcat-5.5-doc/servletapi/javax/servlet/ServletContext.html#getRealPath(java.lang.String)

According to the documentation:

根据文档:

Returns a String containing the real path for a given virtual path. For example, the path "/index.html" returns the absolute file path on the server's filesystem would be served by a request for "http://host/contextPath/index.html", where contextPathis the context pathof this ServletContext.

返回一个包含给定虚拟路径的真实路径的字符串。例如,路径“/index.html”返回服务器文件系统上的绝对文件路径,该路径将由对“http://host/contextPath/index.html”的请求提供服务,其中contextPath是此 ServletContext的上下文路径.

回答by Pradeep Simha

Try this code:

试试这个代码:

class demo extends HttpServlet {
       public void init(ServletConfig config) {
             String path = config.getServletContext().getRealPath("/");
       }
}

It should work

它应该工作