当前位置: 首页 > news >正文

响应式网站建站价格西安疫情最新数据消息5分钟前

响应式网站建站价格,西安疫情最新数据消息5分钟前,自己做网站生意怎么样,做网站挂广告版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。 本文链接: https://blog.csdn.net/qq_27770257/article/details/79438987 最近对于request中的几种“路径”有点混淆,查…
版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接: https://blog.csdn.net/qq_27770257/article/details/79438987

最近对于request中的几种“路径”有点混淆,查找网上资源都没有很好的总结,希望此文章能够帮助我理解一下这几种“路径”。
+++++++++++++++++++++++++++++++++++++++++++++++++
本文章主要讨论以下几种request获取路径的方法:

request.getServletPath()
request.getPathInfo()
request.getContextPath()
request.getRequestURI()
request.getRequestURL()
request.getServletContext().getRealPath()
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

以一个简单的例子说明:
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>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17

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{@Overrideprotected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {this.doPost(req, resp);}@Overrideprotected 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("/"));}}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28

此时请求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\
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8

请求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\
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8

当我们修改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>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20

请求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
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

通过观察打印结果,我们可以总结:
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


以上

http://www.mmbaike.com/news/36542.html

相关文章:

  • ui设计需要学历吗太原关键词优化服务
  • 房屋装修效果图大全seo课程多少钱
  • 中国自适应网站建设南京百度seo排名
  • 澳门网站设计淘宝指数查询官网
  • 网站链接锚点怎么做长沙百度快速优化
  • 网站怎么做rss订阅功能可以进入任何网站的浏览器
  • 网站banner设计价格seo怎么学
  • 山东建设网站首页seo教程技术
  • 国外域名注册商哪家好焦作网站seo
  • 个人php网站全国人大常委会副委员长
  • 佛山网站建设哪家好什么平台可以推销自己的产品
  • 采集器wordpress沈阳网络seo公司
  • 水果网站怎么做的高平网站优化公司
  • 汕头网站建设备案舆情监测分析系统
  • 烟台网站制作企业网络营销的企业有哪些
  • 建设交通职业技术学院招聘信息网站武汉 网络 推广
  • 个人网页制作dreamweaver福州网站优化
  • 北京网站建设公司收购百度推广seo怎么学
  • 网站做多语言万网官网登录
  • 关于动物自己做的网站百度新闻发布平台
  • 天津做网站企业排名优化课程
  • 教师网站建设企业实践总结有什么公司要做推广的
  • 山东做网站建设的好公司排名广告营销案例100例
  • 北京网站建设 合一百度搜索首页
  • 网页设计模拟试题答案seo包括什么
  • 用凡科做的手机网站版怎样创建一个网站
  • wordpress文章加载慢6seo牛人
  • 免费咨询律师网自然搜索优化
  • 上海手机网站建设电话北京本地网络推广平台
  • 郑州大型网站建设电话上海广告公司