首页 > 解决方案 > tomcat 8.5 servlet 参数:webappBasePath

问题描述

我有一个有几个 servlet 的 web 应用程序,一个 servlet 对 init-param 进行了webappBasePath假设ServletConfig。我不知道这是如何设置的,但旧版本(例如,在 centos 5 主机上的 tomcat 5 上测试)tomcat5-5.5.23-0jpp.40.el5_9将 './' 附加到webappBasePath,而新版本似乎没有这样做。(编辑:变量没有附加'./',下面的代码调用ServletContext.getRealPath()默认设置'.'并且不同的tomcat版本处理不同的事情)

我在 WEB-INF/web.xml 中添加了一个新的 servlet:

    <servlet>
        <servlet-name>TKServlet</servlet-name>
        <servlet-class>servlets.TKServlet</servlet-class>

    </servlet>

    <servlet-mapping>
        <servlet-name>TKServlet</servlet-name>
        <url-pattern>/tkservlet</url-pattern>
    </servlet-mapping>

TKServlet.java

package ImageGenerator;

import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.*;

import java.io.*;

public class TKServlet extends HttpServlet {
    private static final long serialVersionUID = 9830958231344L;

    private static String webappBasePath = ".";

    public void init(ServletConfig config) throws ServletException {
        super.init(config);
        if (config.getInitParameter("webappBasePath") != null) {
            webappBasePath = config.getInitParameter("webappBasePath");
            System.err.println("init-param 'webappBasePath' = " + webappBasePath);
        }
        webappBasePath = config.getServletContext()
                             .getRealPath(webappBasePath) + "/";
        System.err.println("getRealPath(webappBasePath) = " + webappBasePath);
    }

    public void doGet(HttpServletRequest request, HttpServletResponse response)
    throws IOException, ServletException {
        response.setContentType("text/ascii");
        PrintWriter out = response.getWriter();
        out.println("Here is some response doc.");
        out.println("webappBasePath is: " + webappBasePath);
    }
}

来自tomcat5的日志:http://localhost:8080/myapp/tkservlet

catalina.out:

getRealPath(webappBasePath) = /usr/share/tomcat5/webapps/XSLT_HIQ/./

登录tomcat8.5:

getRealPath(webappBasePath) = /adtech/tomcat/webapps/XSLT_HIQ/

问题:集合在哪里,webappBasePath我该如何修改它?

标签: tomcatservlets

解决方案


找到答案:我想,我只是让自己感到困惑。有兴趣的请看我的回答。

catalina.out 日志显示 init-parameter根本没有设置:如果是,那么日志文件中webappBasePath会有:init-param 'webappBasePath' = <something>消息。catalina.out

需要注意的一点是,tomcat 版本之间的输出ServletContext.getRealPath()不同,我想这是我的 servlet 代码中的一个错误。

当然也可以<init-params>WEB-INF/web.xml文件中设置。

我已经添加(在<servlet>标签内):

     <init-param>
        <param-name>webappBasePath</param-name>
        <param-value>/var/tmp/somewebapp/files/./</param-value>
     </init-param>
</servlet>

tomcat 5 日志显示

init-param 'webappBasePath' = /var/tmp/somewebapp/files/./
getRealPath(webappBasePath) = /usr/share/tomcat5/webapps/XSLT_HIQ/var/tmp
                                  /somewebapp/files/./

tomcat8.5日志

init-param 'webappBasePath' = /var/tmp/somewebapp/files/./
getRealPath(webappBasePath) = /adtech/tomcat/webapps/XSLT_HIQ/var/tmp
                                  /somewebapp/files//

tomcat8.5 上的注意 ServletContext.getRealPath()事项是通过删除单个点组件来“规范化”路径。

编辑

使用以下代码的旧版 tomcat ApplicationContext(来自svn tc5.0.x的示例)

public String getRealPath(String path) {                                    
    // ...                                                                        
    File file = new File(basePath, path);                                   
    return (file.getAbsolutePath());                                        

}

虽然使用较新的版本getRealPath此处StandardContext为第 4319-4349 行)

public String getRealPath(String path) {
    if (resources != null) {
        // ...
            WebResource resource = resources.getResource(path);
            String canonicalPath = resource.getCanonicalPath();
            // ...

                return canonicalPath;
            }
    // ...
}

WebResourcegetCanonicalPath()调用java.io.File#getCanonicalPath()哪个

删除多余的名称,例如“。” 和路径名中的“..”

(从File#getCanonicalPath 的 javadocs复制)


推荐阅读