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

免费做ppt网站建程网官网

免费做ppt网站,建程网官网,天津百度推广,建网赌网站流程版权声明:本文为博主原创文章,遵循 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.yayakq.cn/news/214710/

相关文章:

  • 福田公司网站建设北京seo业务员
  • 建设cpa网站需要什么哪个网站做的最好
  • 自己做的网站怎么才有用户访问福州建设公司名单
  • 注册网站免费注册ins毕业答辩ppt 网站开发
  • 三河市城乡建设局网站wordpress 首页调用页面标题
  • 重庆靓号网站建设在线写作网站
  • 深圳建设银行网站wordpress登入后台没反应
  • asp源码-漂亮企业源码大气公司网站模版wordpress替换主题数据库
  • 网站 展示展示网站模板下载
  • 网站导航页面设计单页营销型网站模板
  • 鞍山市城乡建设局网站北京有哪些软件公司在招聘
  • 做生鲜的网站拼多多网站
  • 照片展示网站百度收录入口在哪里查询
  • 企业网站明细费用网站数据每隔几秒切换怎么做的
  • 哪些行业做网站的多陕西省建设厅网站查询
  • 网站幻灯片尺寸设置免费平面设计模板网站
  • 连云港做网站建设vs2010网站开发 SQL
  • 做外贸最适合的网站系统软件下载网站模板
  • 网站优化的目的wordpress 大内存
  • 苏州网站建设seo开个小网站要怎么做
  • 如何做网站安全扫描有奖竞猜网站建设
  • 毕节建设网站平面设计做兼职网站
  • 金属加工网站建设淘宝网网页设计作业
  • 企业二级域名自助建站平台网站开发使用的技术有哪些
  • 苏州网站建设学费做国外的营销的网站
  • 什么网站可以做家教域名网站大全
  • 临沂网站优化如何做放单网站
  • 深圳制作网站制作公司哪家好上海网站开发与
  • 自己如何做外贸公司网站seo常用工具包括
  • 建设网站熊掌号wordpress搭的