今天用实际例子验证了一下tomcat中的各种路径.在 webapps目录中创建了一个pathtest的目录,结构如下:
webapps/
pathtest/
WEB-INF/
classes/
FMAction.java
FMAction.class
web.xml
sub/
index.jsp
index.jsp
web.xml对servlet的配置如下:






























在FMAction.java中输出所有路径变量,代码如下:











































pathtest和sub目录下的index.jsp的代码都如下:











以 http://localhost:8080/pathtest/fmaction访问得到如下信息:
ContextPath: /pathtest
PathInfo: null
PathTranslated: null
RealPath("index.jsp"): D:/Program Files/Apache Software Foundation/Tomcat 5.5/webapps/pathtest/index.jsp
RequestURI: /pathtest/fmaction
RequestURL: http://localhost:8080/pathtest/fmaction
ServletPath: /fmaction
以 http://localhost:8080/pathtest/fmaction/fea?fea=aa访问得到如下信息:
ContextPath: /pathtest
PathInfo: /fea
PathTranslated: D:/Program Files/Apache Software Foundation/Tomcat 5.5/webapps/pathtest/fea
RealPath("index.jsp"): D:/Program Files/Apache Software Foundation/Tomcat 5.5/webapps/pathtest/index.jsp
RequestURI: /pathtest/fmaction/fea
RequestURL: http://localhost:8080/pathtest/fmaction/fea
ServletPath: /fmaction
以 http://localhost:8080/pathtest/访问单击提交后得到如下信息:
ContextPath: /pathtest
PathInfo: null
PathTranslated: null
RealPath("index.jsp"): D:/Program Files/Apache Software Foundation/Tomcat 5.5/webapps/pathtest/index.jsp
RequestURI: /pathtest/login.html
RequestURL: http://localhost:8080/pathtest/login.html
ServletPath: /login.html
以 http://localhost:8080/pathtest/sub/ 访问单击提交后得到如下信息:
ContextPath: /pathtest
PathInfo: null
PathTranslated: null
RealPath("index.jsp"): D:/Program Files/Apache Software Foundation/Tomcat 5.5/webapps/pathtest/index.jsp
RequestURI: /pathtest/sub/login.html
RequestURL: http://localhost:8080/pathtest/sub/login.html
ServletPath: /sub/login.html
由此,可得出各种路径的含义:
ContextPath: 相对于webapps的应用程序路径
PathInfo: servlet的附加路径,即访问路径中 servlet名称之后,QueryString之前的那一部分,也是URL中servlet名称之后的那一部分,如果没有则为null.
PathTanslated: 应用程序的磁盘路径加上PathInfo
RealPath("index.jsp"): 应用程序的磁盘路径加上getRealPath中参数指定的路径
RequestURI: 相对webapps的资源路径,可以理解为URL中除主机信息以外的那部分
RequestURL: 请求路径中除QueryString以外的那部分
ServletPath: 相对于应用程序的不包含附加路径的servlet请求路径(注意是请求路径,一个servlet可以映射很多个请求路径)