本文章主要讨论以下几种request获取路径的方法:

request.getServletPath()
request.getPathInfo()
request.getContextPath()
request.getRequestURI()
request.getRequestURL()
request.getServletContext().getRealPath()

以一个简单的例子说明: 
web.xml配置(注意此处的url-pattern项)

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
<display-name>aab</display-name>
<welcome-file-list>
<welcome-file>a.jsp</welcome-file>
</welcome-file-list>
<servlet>
<servlet-name>test</servlet-name>
<servlet-class>com.java.test.TestServlet</servlet-class> </servlet>
<servlet-mapping>
<servlet-name>test</servlet-name>
<url-pattern>/*</url-pattern><!-- 注意此处 -->
</servlet-mapping> </web-app>

TestServlet.java文件:

package com.java.test;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; public class TestServlet extends HttpServlet{
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
this.doPost(req, resp);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
System.out.println("servletPath:"+req.getServletPath());
System.out.println("contextPath:"+req.getContextPath());
System.out.println("contextPath2:"+req.getServletContext().getContextPath());
System.out.println("pageInfo:"+req.getPathInfo());
System.out.println("uri:"+req.getRequestURI());
System.out.println("url:"+req.getRequestURL());
System.out.println("realPath:"+req.getServletContext().getRealPath("/")); } }

此时请求http://localhost:8080/testweb (url-pattern=/*) 
打印出来的值为:

servletPath:
contextPath:/testweb
contextPath2:/testweb
pageInfo:null
uri:/testweb
url:http://localhost:8080/testweb
realPath:G:\java\.metadata\.plugins\org.eclipse.wst.server.core\tmp0\wtpwebapps\testweb\

请求http://localhost:8080/testweb/abc 打印的值为:

servletPath:
contextPath:/testweb
contextPath2:/testweb
pageInfo:/abc
uri:/testweb/abc
url:http://localhost:8080/testweb/abc
realPath:G:\java\.metadata\.plugins\org.eclipse.wst.server.core\tmp0\wtpwebapps\testweb\

当我们修改web.xml为如下时(注意url-pattern的改变):

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
<display-name>aab</display-name>
<welcome-file-list>
<welcome-file>a.jsp</welcome-file>
</welcome-file-list> <servlet>
<servlet-name>test</servlet-name>
<servlet-class>com.java.test.TestServlet</servlet-class> </servlet> <servlet-mapping>
<servlet-name>test</servlet-name>
<url-pattern>/abc/def/*</url-pattern><!-- 注意此处 -->
</servlet-mapping> </web-app>

请求http://localhost:8080/testweb/abc/def/ghi/test.html (url-pattern=/abc/def/*) 
打印的值为:

servletPath:/abc/def
contextPath:/testweb
contextPath2:/testweb
pageInfo:/ghi/test.html
uri:/testweb/abc/def/ghi/test.html
url:http://localhost:8080/testweb/abc/def/ghi/test.html
realPath:G:\java\.metadata\.plugins\org.eclipse.wst.server.core\tmp0\wtpwebapps\testweb\

通过观察打印结果,我们可以总结:
1. getServletPath():获取能够与“url-pattern”中匹配的路径,注意是完全匹配的部分,*的部分不包括。
2. getPageInfo():与getServletPath()获取的路径互补,能够得到的是“url-pattern”中*d的路径部分
3. getContextPath():获取项目的根路径
4. getRequestURI:获取根路径到地址结尾
5. getRequestURL:获取请求的地址链接(浏览器中输入的地址)
6. getServletContext().getRealPath(“/”):获取“/”在机器中的实际地址
7. getScheme():获取的是使用的协议(http 或https)
8. getProtocol():获取的是协议的名称(HTTP/1.11)
9. getServerName():获取的是域名(xxx.com)
10. getLocalName:获取到的是IP

使用情景

servlet路径获取的更多相关文章

  1. Servlet路径跳转问题

    Servlet中路径跳转(服务器端跳转)JSP 1.相对路径  注意这里的相对含义,相对于谁而言 经过多次试验总结,servlet相对路径跳转相对于servlet配置的xml路径(或servlet3. ...

  2. Servlet学习笔记(二)之Servlet路径映射配置、Servlet接口、ServletConfig、ServletContext

    Servlet路径映射配置 要使Servlet对象正常的运行,需要进行适当的配置,以告诉Web容器哪个请求调用哪个Servlet对象处理,对Servlet起到一个注册的作用.Servlet的配置信息包 ...

  3. c#根据绝对路径获取 带后缀文件名、后缀名、文件名

    zz   C#根据绝对路径获取 带后缀文件名.后缀名.文件名 1.c#根据绝对路径获取 带后缀文件名.后缀名.文件名. string str =" F:\test\Default.aspx& ...

  4. Servlet路径跳转2--在servlet当中,跳转到某网页时的路径写法

    课程1-13   http://www.imooc.com/video/5554 Servlet路径跳转: 绝对路径:放在任何地方都对的路径 相对路径:相对于当前资源的路径 两种方法:请求重定向,服务 ...

  5. Servlet路径跳转1---使用相对路径和绝对路径,在页面上调用servlet的路径写法(超链接的方式和表单的方式)

    课程1-13   http://www.imooc.com/video/5554 Servlet路径跳转: 绝对路径:放在任何地方都对的路径 相对路径:相对于当前资源的路径 index文件 加上/,表 ...

  6. SpringMVC核心——参数获取与Servlet资源获取问题

    一.SpringMVC 使用 @PathVariable.@RequestParam.@RequestHeader.@CookieValue 等来解决参数获取问题. 1. @PathVariable: ...

  7. JSP Servlet 路径解析 路径设置

    转自:http://ethen.iteye.com/blog/800415 在用JSP和Servlet编写Web应用时,经常遇到的问题就是找不到.do路径,或者.do路径不能解析,其实归根到底就是Se ...

  8. Java学习-009-文件名称及路径获取实例及源代码

    此文源码主要为应用 Java 获取文件名称及文件目录的源码及其测试源码.若有不足之处,敬请大神指正,不胜感激!源代码测试通过日期为:2015-2-3 00:02:27,请知悉. Java获取文件名称的 ...

  9. 自建目录中jsp页面访问servlet路径出错404

    ---恢复内容开始--- 自建目录中jsp页面访问servlet路径出错404 使用eclipse建立的项目,总是会遇到路径问题,比如jsp页面访问servlet,jsp在默认的路径.jsp在自建目录 ...

随机推荐

  1. [UE4]认识Decorator

    Decorator装饰器:即为其他行为树系统中的条件语句,附着于一个Composite(组合节点)或者Task(任务节点),并定义树中的一个分支或者单个节点是否可被执行. Decorator装饰器节点 ...

  2. sqlserver数据库设计完整性与约束

    use StudentManageDB go --创建主键约束 if exists(select * from sysobjects where name='pk_StudentId') alter ...

  3. 2017湖湘杯复赛writeup

    2017湖湘杯复赛writeup 队伍名:China H.L.B 队伍同时在打 X-NUCA  和 湖湘杯的比赛,再加上周末周末周末啊,陪女朋友逛街吃饭看电影啊.所以精力有点分散,做出来部分题目,现在 ...

  4. 使用gulp压缩js详细步骤笔记

    先要安装nodejs,初始化项目配置文件package.json,根据情况输入配置参数 npm init test command: git repository: keywords: author: ...

  5. Delphi StringGrid控件的用法

    Delphi StringGrid控件 组件名称:StringGrid         ●固定行及固定列:  StringGrid.FixedCols:=固定行之数;  StringGrid.Fixe ...

  6. 新的开始 接触ASP.NET Core跨平台的框架

    今天我刚学习了使用Visual Studio 2015新建了.NET Core项目写了一个小的CSHTML代码.按我的话说就是,把C#和HTML合起来使用了,写了一个简单的关于学生的“增” “删” “ ...

  7. 二、Html5元素、属性、格式化

  8. jmeter本机内存溢出如何修改?

    websocket连接过程中内存溢出,本机配置的内存最大和最小设置的512: 一.后台返回 二.结果树返回: Thread Name: 线程组 1-9Sample Start: 2017-09-11 ...

  9. truncate(can)

    1. 如果给了参数, 从头截取到参数位置 2. 如果不给参数, 从头截取到光标 f = open("司马光砸缸", mode="r+", encoding=&q ...

  10. mysql外键的三种关系

    因为有foreign key的约束,使得两张表形成了三种了关系: 多对一 多对多 一对一 一对多或多对一 create table press( id int primary key auto_inc ...