tomcat - 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
我该如何修改它?
解决方案
找到答案:我想,我只是让自己感到困惑。有兴趣的请看我的回答。
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()
哪个
删除多余的名称,例如“。” 和路径名中的“..”
推荐阅读
- c# - 将字节数组与 EF Core InMemoryDatabase 进行比较以进行单元测试
- templates - Xamarin MVVM 显示来自另一个模型/表/对象的数据
- android - 找不到小米手机 id 列中的 PICK_CONTACT
- python - 为什么我在尝试添加 opencv 模块时在 PyCharm 中出现错误,尽管已经安装
- apache-spark - presto with hudi - 从表中选择 *
- python - 在 Airflow 上使用 PythonOperator 时如何使用 Python 函数的返回值/
- ruby - 为什么我的简单连接池不执行这些简单的 put 语句?
- git - 忽略 git checkout 中的脚本
- flutter - 如何将焦点设置到 ListView 中的 TextField
- r - 遍历 2 个不同长度的大数据帧(如果,否则)