servlet路径获取
本文章主要讨论以下几种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路径获取的更多相关文章
- Servlet路径跳转问题
Servlet中路径跳转(服务器端跳转)JSP 1.相对路径 注意这里的相对含义,相对于谁而言 经过多次试验总结,servlet相对路径跳转相对于servlet配置的xml路径(或servlet3. ...
- Servlet学习笔记(二)之Servlet路径映射配置、Servlet接口、ServletConfig、ServletContext
Servlet路径映射配置 要使Servlet对象正常的运行,需要进行适当的配置,以告诉Web容器哪个请求调用哪个Servlet对象处理,对Servlet起到一个注册的作用.Servlet的配置信息包 ...
- c#根据绝对路径获取 带后缀文件名、后缀名、文件名
zz C#根据绝对路径获取 带后缀文件名.后缀名.文件名 1.c#根据绝对路径获取 带后缀文件名.后缀名.文件名. string str =" F:\test\Default.aspx& ...
- Servlet路径跳转2--在servlet当中,跳转到某网页时的路径写法
课程1-13 http://www.imooc.com/video/5554 Servlet路径跳转: 绝对路径:放在任何地方都对的路径 相对路径:相对于当前资源的路径 两种方法:请求重定向,服务 ...
- Servlet路径跳转1---使用相对路径和绝对路径,在页面上调用servlet的路径写法(超链接的方式和表单的方式)
课程1-13 http://www.imooc.com/video/5554 Servlet路径跳转: 绝对路径:放在任何地方都对的路径 相对路径:相对于当前资源的路径 index文件 加上/,表 ...
- SpringMVC核心——参数获取与Servlet资源获取问题
一.SpringMVC 使用 @PathVariable.@RequestParam.@RequestHeader.@CookieValue 等来解决参数获取问题. 1. @PathVariable: ...
- JSP Servlet 路径解析 路径设置
转自:http://ethen.iteye.com/blog/800415 在用JSP和Servlet编写Web应用时,经常遇到的问题就是找不到.do路径,或者.do路径不能解析,其实归根到底就是Se ...
- Java学习-009-文件名称及路径获取实例及源代码
此文源码主要为应用 Java 获取文件名称及文件目录的源码及其测试源码.若有不足之处,敬请大神指正,不胜感激!源代码测试通过日期为:2015-2-3 00:02:27,请知悉. Java获取文件名称的 ...
- 自建目录中jsp页面访问servlet路径出错404
---恢复内容开始--- 自建目录中jsp页面访问servlet路径出错404 使用eclipse建立的项目,总是会遇到路径问题,比如jsp页面访问servlet,jsp在默认的路径.jsp在自建目录 ...
随机推荐
- Jmeter(三十四)Jmeter-Question之“Cookie获取”
2018.4.27 还在做性能测试的过程中,唉,只能说坑很多. 无明确需求.无人手协调等问题,什么都需要自己去挖掘. 本次测试的工具选型依然是Jmeter,真实场景中遇到了这么个问题.可能解决办法有点 ...
- OOP学习
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- C# 将本地文件远程拷贝到其他电脑(转)
string newpath = System.IO.Path.GetFullPath(@"////10.144.26.252//d$//quyuan//图片" +"// ...
- php数组和部分操作函数
1. 数组定义 数组的定义使用 array()方式定义,可以定义空数组: <?php $number = array(1,3,5,7,9); //定义空数组 $result = array(); ...
- 微信小程序如何引用其他js文件
1.我们先建立一个common.js文件,在common.js编写我们的程序, function myfunc() { console.log("myfunc....");} mo ...
- sql server 给表加说明,给列/字段加说明
--sql server给表加说明: --banner EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'Banner ...
- js中script的上下放置区别 , Dom的增删改创建
回顾 javascript分为三部分: 1.ECMAScript5.0 es6(阮一峰) es7 es8 es6中有类的概念 声明变量 var let(es6中语法) 内置函数 Date Math.r ...
- [Unity工具]批量修改Texture
BatchModifyTexture.cs using UnityEngine; using System.Collections; using UnityEditor; using System.I ...
- 查询oracle约束所关联的表
ORA-02292: 违反完整约束条件 (ADMIN.FK_PROJECTP_RELATIONS_OFFICIAL) - 已找到子记录 遇到这样的错误,熟悉的话可能从约束名称看出是那张表的约束,因为名 ...
- 【Python爬虫实战】Scrapy框架的安装 搬运工亲测有效
windows下亲测有效 http://blog.csdn.net/liuweiyuxiang/article/details/68929999这个我们只是正确操作步骤详解的搬运工