本文章主要讨论以下几种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]Math Expression计算数学公式,可以接受参数

  2. CentOS安装redis.tar.gz

    1. # cd /usr/local/src 2. # tar -zxvf redis-3.0.6.tar.gz 3. # cd redis-3.0.6 4.# make 5.#  make PREF ...

  3. 第1课 类型推导(1)_auto关键字

    1.  auto关键字 (1)auto的作用是让编译器自动推断变量的类型,而不需要显式指定类型.这种隐式类型的推导发生在编译期. (2)auto并不能代表实际的类型声明,只是一个类型声明的“占位符” ...

  4. C++和C#进程之间通过命名管道通信(上)

    C++和C#进程之间通过命名管道通信(上) "命名管道"是一种简单的进程间通信(IPC)机制.命名管道可在同一台计算机的不同进程之间,或在跨越一个网络的不同计算机的不同进程之间,支 ...

  5. 12 文件查找--find命令

    之前,我们学习过grep来过滤文件内容,而这种查找找的是某一个文件内的内容:以及 less 或者 man 或者上一节提到的 vim 编辑器中的 / 与 ? 都是用来查找单个文件内的内容.而这一节,我们 ...

  6. hbase启动后子节点的regionserver不能启动

    启动hbase后,主节点的进程正常,但是子节点的regionserver进程会自动挂掉 然后我们看看子节点的情况 可以看到挂掉了 我们这样解决问题,先把hadoop目录下的这个两个文件放到hbase的 ...

  7. vue实现未登录跳转到登录页面

    环境:vue 2.9.3; webpack;vue-router 目的:实现未登录跳转 例子:直接在url地址栏输入...../home,但是这个页面要求需要登陆之后才能进入,判断的值就通过登陆之后给 ...

  8. 03-spark kafka

    1.概念 Kafka是一个开源的消息系统.由Scala编写,它具备以下特点: ①消息持久化: 为了从大数据中获取有价值的信息,任何信息的丢失都是负担不起的.使用Kafka时,message会被存储并且 ...

  9. Spring配置,事务使用

    1.spring redis session超时配置 <bean class="org.springframework.session.data.redis.config.annota ...

  10. 关于微信小程序更新内容后手机上不能及时显示的办法

    这几天一直在做微信小程序的二次开发,每天都要发布程序,但是发布之后微信上查看小程序和以前的一模一样,丝毫没有改变,但是我再本地上却改变了,而且没有开的不校验合法域名那个.这是为啥呢????? 这是跟小 ...