tomcat中的路径实践
来源:互联网 发布:淘宝店铺优惠如何设置 编辑:程序博客网 时间:2024/05/22 06:56
今天用实际例子验证了一下tomcat中的各种路径.在 webapps目录中创建了一个pathtest的目录,结构如下:
webapps/
pathtest/
WEB-INF/
classes/
FMAction.java
FMAction.class
web.xml
sub/
index.jsp
index.jsp
web.xml对servlet的配置如下:
<?xml version="1.0" encoding="ISO-8859-1"?>
<!DOCTYPE web-app
PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.2//EN"
"http://java.sun.com/j2ee/dtds/web-app_2_2.dtd">
<web-app>
<display-name>Struts Blank Application</display-name>
<!-- Standard Action Servlet Configuration (with debugging) -->
<servlet>
<servlet-name>fmaction</servlet-name>
<servlet-class>FMAction</servlet-class>
</servlet>
<!-- Standard Action Servlet Mapping -->
<servlet-mapping>
<servlet-name>fmaction</servlet-name>
<url-pattern>*.html</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>fmaction</servlet-name>
<url-pattern>fmaction/*</url-pattern>
</servlet-mapping>
<!-- The Usual Welcome File List -->
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
<!DOCTYPE web-app
PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.2//EN"
"http://java.sun.com/j2ee/dtds/web-app_2_2.dtd">
<web-app>
<display-name>Struts Blank Application</display-name>
<!-- Standard Action Servlet Configuration (with debugging) -->
<servlet>
<servlet-name>fmaction</servlet-name>
<servlet-class>FMAction</servlet-class>
</servlet>
<!-- Standard Action Servlet Mapping -->
<servlet-mapping>
<servlet-name>fmaction</servlet-name>
<url-pattern>*.html</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>fmaction</servlet-name>
<url-pattern>fmaction/*</url-pattern>
</servlet-mapping>
<!-- The Usual Welcome File List -->
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
在FMAction.java中输出所有路径变量,代码如下:
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
public class FMAction extends javax.servlet.http.HttpServlet
...{
public FMAction()
...{
System.out.println("you com in FMAction.java file");
}
public void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException
...{
PrintWriter out = resp.getWriter();
StringBuffer sb = new StringBuffer();
sb.append("ContextPath: ");
sb.append(req.getContextPath());
sb.append("<br>PathInfo: ");
sb.append(req.getPathInfo());
sb.append("<br>PathTranslated: ");
sb.append(req.getPathTranslated());
sb.append("<br>RealPath(/"index.jsp/"): ");
sb.append(req.getRealPath("index.jsp"));
sb.append("<br>RequestURI: ");
sb.append(req.getRequestURI());
sb.append("<br>RequestURL: ");
sb.append(req.getRequestURL().toString());
sb.append("<br>ServletPath: ");
sb.append(req.getServletPath());
out.println(sb.toString());
}
public void doPost(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException
...{
doGet(req,resp);
}
}
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
public class FMAction extends javax.servlet.http.HttpServlet
...{
public FMAction()
...{
System.out.println("you com in FMAction.java file");
}
public void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException
...{
PrintWriter out = resp.getWriter();
StringBuffer sb = new StringBuffer();
sb.append("ContextPath: ");
sb.append(req.getContextPath());
sb.append("<br>PathInfo: ");
sb.append(req.getPathInfo());
sb.append("<br>PathTranslated: ");
sb.append(req.getPathTranslated());
sb.append("<br>RealPath(/"index.jsp/"): ");
sb.append(req.getRealPath("index.jsp"));
sb.append("<br>RequestURI: ");
sb.append(req.getRequestURI());
sb.append("<br>RequestURL: ");
sb.append(req.getRequestURL().toString());
sb.append("<br>ServletPath: ");
sb.append(req.getServletPath());
out.println(sb.toString());
}
public void doPost(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException
...{
doGet(req,resp);
}
}
pathtest和sub目录下的index.jsp的代码都如下:
<%...@page contentType="text/html;charset=gb2312" language="java"%>
<html>
<head><title>struts test</title></head>
<body>
<form action="login.html?fea=fi" method="post">
<input type="text" name="user" value="" />
<input type="submit" value="提交" />
<form>
</body>
</html>
<html>
<head><title>struts test</title></head>
<body>
<form action="login.html?fea=fi" method="post">
<input type="text" name="user" value="" />
<input type="submit" value="提交" />
<form>
</body>
</html>
以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可以映射很多个请求路径)
- tomcat中的路径实践
- tomcat中的路径问题
- eclipse 项目在tomcat中的部署路径
- tomcat路径
- Servlet/JSP中的路径,Tomcat部署项目时问题
- 修改Eclipse中项目在Apache Tomcat中的部署路径
- 修改Eclipse中项目在Apache Tomcat中的部署路径
- 修改Eclipse中项目在Apache Tomcat中的部署路径
- 修改Eclipse中项目在Apache Tomcat中的部署路径
- Servlet/JSP中的路径,Tomcat部署项目时问题
- Eclipse中的Web项目自动部署到Tomcat路径下
- 修改Eclipse中项目在Apache Tomcat中的部署路径
- tomcat默认跳转去掉路径中的项目名
- tomcat虚拟路径的实践---javaweb项目上传视频,图片到任意磁盘文件夹
- eclipse4.3里修改项目在tomcat中的上下文访问路径和部署路径
- eclipse4.3里修改项目在tomcat中的上下文访问路径和部署路径
- [tomcat]tomcat中文路径无法访问
- tomcat虚拟路径-外包路径-路径配置
- 2007-8-11
- 线程经典代码二,(同步情况下的生产者/消费者关系)
- 360度看呼叫中心
- 思科统一通信应用问题解答
- 明天你来自己做外挂
- tomcat中的路径实践
- 用VB写下载者最短代码
- JavaPhone API和开发要点
- 关于单元测试自动打桩问题
- 在XP里提升自己的程序以获得特定的权限
- javascript日期比较
- C#的委托
- 开天劈地博一客
- 一些网站地址备份|APPEND