tomcat中的路径实践

今天用实际例子验证了一下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 >

在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:&nbsp;&nbsp;");
        sb.append(req.getContextPath());
        sb.append(
"<br>PathInfo:&nbsp;&nbsp;");
        sb.append(req.getPathInfo());
        sb.append(
"<br>PathTranslated:&nbsp;&nbsp;");
        sb.append(req.getPathTranslated());
        sb.append(
"<br>RealPath(/"index.jsp/"):&nbsp;&nbsp;");
        sb.append(req.getRealPath(
"index.jsp"));
        sb.append(
"<br>RequestURI:&nbsp;&nbsp;");
        sb.append(req.getRequestURI());
        sb.append(
"<br>RequestURL:&nbsp;&nbsp;");
        sb.append(req.getRequestURL().toString());
        sb.append(
"<br>ServletPath:&nbsp;&nbsp;");
        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 >

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可以映射很多个请求路径)
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值