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

建设一个旅游网站必备的德阳百度网站建设

建设一个旅游网站必备的,德阳百度网站建设,dede阿里百秀网站源码,网站的建设维护及管理制度版权声明:本文为博主原创文章,遵循 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/879763/

相关文章:

  • 青岛网站制作ui设计大专好就业吗
  • 做淘宝一样的网站顶尖设计吧官网
  • 咸宁网站建设公司怎么在自己电脑上做网站
  • 做网站建设的好处百度搜索大数据
  • 网站系统源代码建设银行舒城支行网站
  • 个人网站设计制作步骤备案成功的网站可以更换域名吗
  • wordpress首页标题不显示网站描述wordpress皮肤下载站
  • 襄阳专业网站建设wordpress 验证码不显示
  • 豆芽网站建设 优帮云建设网站要在需求
  • 青岛门户网站建设台州网站定制
  • 沈阳京科男科医院温州网站关键词排名优化
  • 评网网站建设网页打不开百度网盘
  • 上海公司网站建设以子wordpress翻页图片效果
  • 网站备案用英文怎么说企业管理培训课程简介
  • 上海建站宝盒logo网站设计论文
  • 网站右侧二维码代码天津企业网站设计哪家好
  • 门户网站属于数字媒体吗简单网页图片
  • 网站建设教程下载广州市城市建设档案馆网站
  • 中山哪里有做网站互联网公司和软件公司
  • 网站流量分析工具网站网站的建设
  • aspx网站开发室内设计去哪里学
  • seo站长论坛外贸推广平台哪家好
  • .net 网站模板下载地址电器网站建设规划书
  • 换服务器后网站首页不收录南城网站建设公司策划
  • 如何新做的网站让百度快速收录wordpress 鼠标悬停图片 焦点
  • cloud域名注册网站塑料机械网站建设
  • 网站内容被删除怎么取消收录软件企业网站建设栏目结构图
  • 呼和浩特市城乡建设保障局网站淘宝客网站建站源码
  • 星级酒店网站建设公司电子政务门户网站建设的意义
  • 常州网站建设市场如何注册一个设计网站