servlet访问路径测试(path)

来源:互联网 发布:高仿阿迪达斯淘宝店 编辑:程序博客网 时间:2024/06/15 19:39

1、getContextPath:站点名[/context](Web应用的入口点名字)

2、getServletPath:servlet类的对应映射路径[/resource]

3、getRequestURI:uri地址 = [/context] + [/resource]

4、getRequestURL:protocol://IP adderss:[port][/context][/resource][/?query string]

5、代码显例如下:

package com.tiger.path.action;import java.io.IOException;import java.io.PrintWriter;import javax.servlet.ServletException;import javax.servlet.annotation.WebServlet;import javax.servlet.http.*;/** * 结论:在jsp的标签中写入下面一段代码,之后所有链接都可以使用绝对路劲,像这样 * " /> * 测试访问路径,测试输出结果如下: * ContextPath = /Struts_0040_Path    * ServeltPath = /path.do    * RquestURI = /Struts_0040_Path/path.do    * RquestURL = http://localhost:8080/Struts_0040_Path/path.do    * Scheme = http    * ServerName = localhost    * ServerPort = 8080 * @author tiger * @time 2017年9月7日 */@WebServlet("/path.do")public class Path extends HttpServlet {private static final long serialVersionUID = 1L;           public Path() { }protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {PrintWriter out = response.getWriter();out.append("   

ContextPath = ").append(request.getContextPath()).append("
").append(" ServeltPath = ").append(request.getServletPath()).append("
").append(" RquestURI = ").append(request.getRequestURI()).append("
").append(" RquestURL = ").append(request.getRequestURL()).append("
").append(" Scheme = ").append(request.getScheme()).append("
").append(" ServerName = ").append(request.getServerName()).append("
").append(" ServerPort = ").append(String.valueOf(request.getServerPort())+"

");}}