本文章主要讨论以下几种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. B-树和B+树的应用

    1 .B-树定义 B-树是一种平衡的多路查找树,它在文件系统中很有用. 定义:一棵m 阶的B-树,或者为空树,或为满足下列特性的m 叉树:⑴树中每个结点至多有m 棵子树:⑵若根结点不是叶子结点,则至少 ...

  2. Iptabels防火墙和SElinux

    两者的区别: iptables用于设置防火墙(firewall), 即管理内外通信. iptables是Linux下功能强大的应用层防火墙工具iptables 能做到“控制内部机器上网与不上网,访问哪 ...

  3. Anaconda的基本使用

  4. Jupyter配置步骤

    Jupyter是基于浏览器的可交互式开发工具,在数据科学界非常受欢迎,它功能齐全,使用方便,是一款数据分析和建模挖掘的利器. 本文简介Jupyter的配置和使用过程 一.修改添加国内镜像 通常我会先安 ...

  5. Apache poi简介及代码操作Excel

    一.简介 在我们进行企业的系统开发时,难免会遇到网页表格和Excel之间的操作问题(POI是个不错的选择) Apache POI是Apache软件基金会的开放源码函式库,POI提供API给Java程序 ...

  6. SOLR缓存调优

    缓存在 Solr 中充当了一个非常重要的角色,Solr 中主要有这三种缓存: Filter cache(过滤器缓存),用于保存过滤器(fq 参数)和层面搜索的结果 Document cache(文档缓 ...

  7. linux之 redis 的rdb 转 aof 及主从复

    redis持久化RDB基于快照的持久化通过save命令,强制持久化  在redis.conf中dbfilename  dbmp.rdbsave  900 1save 300 10save 60  10 ...

  8. Redis数据结构深入解析

  9. 踩过的坑:InteliIJ IDEA 打开的项目突然左侧目录结构消失了,如何处理?

    试了很多的办法,删除项目,然后重新从git下载,再导入项目,但是对于暂存未上传到git的文件也会被一并删除,这样就亏大发了 之前一直没有找到解决办法,这里记一下终身有效的办法,并且比较好操作 按下列步 ...

  10. NoSQL学习1

    MongoDB使用C++语言编写的一个基于分布式的文件存储的开源数据库.可以在承受高负载的情况下,保证服务器的性能. MongoDB将数据存储成为一个文档,数据结构有键值对组成.类似于JSON,字段值 ...