Web应用中有各种获取path或URI,URL的方法,假设网页访问地址:

http://localhost:8080/tradeload/TestServlet

Web应用context: /tradeload

各路径鉴定如下:

  1. request.getContextPath()= /tradeload
  2. request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort()= http://localhost:8080
  3. request.getRequestURL() = http://localhost:8080/tradeload/TestServlet
  4. request.getRequestURI() = /tradeload/TestServlet
  5. request.getPathInfo() = null
  6. request.getServletPath() = /TestServlet
  7. getServletContext().getRealPath('/') = C:\server\glassfish\domains\domain1\applications\j2ee-modules\tradeload\

其中的servletpath就是web.xml中配置的url-pattern。

-------------------------------------------------------------------------------

假定你的web application 名称为news,你在浏览器中输入请求路径:
http://localhost:8080/news/main/list.jsp

则执行下面向行代码后打印出如下结果:

1、System.out.println(request.getContextPath());

打印结果:/news

2、System.out.println(request.getServletPath());

打印结果:/main/list.jsp

3、System.out.println(request.getRequestURI());

打印结果:/news/main/list.jsp

4、System.out.println(request.getRealPath("/"));

打印结果:F:\Tomcat 6.0\webapps\news\test

String path = request.getContextPath();

String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";

out.println("basePath:"+basePath);

out.println("<br/>");

out.println("getContextPath:"+request.getContextPath());

out.println("<br/>");

out.println("getServletPath:"+request.getServletPath());

out.println("<br/>");

out.println("getRequestURI:"+request.getRequestURI());

out.println("<br/>");

out.println("getRequestURL:"+request.getRequestURL());

out.println("<br/>");

out.println("getRealPath:"+request.getRealPath("/"));

out.println("<br/>");

out.println("getServletContext().getRealPath:"+getServletContext().getRealPath("/"));

out.println("<br/>");

out.println("getQueryString:"+request.getQueryString());

显示结果:

basePath:http://localhost:8080/test/

getContextPath:/test
getServletPath:/test.jsp
getRequestURI:/test/test.jsp
getRequestURL:http://localhost:8080/test/test.jsp
getRealPath:D:\Tomcat 6.0\webapps\test\
getServletContext().getRealPath:D:\Tomcat 6.0\webapps\test\
getQueryString:p=fuck

在一些应用中,未登录用户请求了必须登录的资源时,提示用户登录,此时要记住用户访问的当前页面的URL,当他登录成功后根据记住的URL跳回用户最后访问的页面:

String lastAccessUrl = request.getRequestURL() + "?" + request.getQueryString();

Web应用中request获取path,URI,URL的更多相关文章

  1. (转)WebSphere的web工程中怎么获取数据源

    原文:http://aguu125.iteye.com/blog/1694313 https://blog.csdn.net/bigtree_3721/article/details/44900325 ...

  2. js中如何获取页面的Url,域名和端口号

    有时候通过获取上个页面的Url来做一个跳转,获取域名防止非正常访问 获取上一个页面的一个URL,这个URL一般做一个页面的跳转 window.location.href <script>w ...

  3. JSP中request获取值

    获取项目名:request.getContextPath(); 获取IP:request.getServerName() 获取端口:request.getServerPort() pageContex ...

  4. Web API中如何获取相对地址的绝对地址 Server.MapPath

    var sPath = System.Web.Hosting.HostingEnvironment.MapPath("/FilePath/");

  5. web项目中各种路径的获取

    以工程名为/DemoWeb为例: 访问的jsp为:http://localhost:8080/DemoWeb/test/index.jsp 1 JSP中获得当前应用的相对路径和绝对路径 (1)得到工程 ...

  6. Request 获取Url

    1.获取页面,HttpContext.Current.Request也是Request //获取当前页面url string myurl = HttpContext.Current.Request.U ...

  7. C# Request 获取Url

    1.获取页面,HttpContext.Current.Request也是Request //获取当前页面url string myurl = System.Web.HttpContext.Curren ...

  8. Spring中如何获取request的方法汇总及其线程安全性分析

    前言 本文将介绍在Spring MVC开发的web系统中,获取request对象的几种方法,并讨论其线程安全性.下面话不多说了,来一起看看详细的介绍吧. 概述 在使用Spring MVC开发Web系统 ...

  9. 04.vue获取微博授权URL

    1.在Vue页面加载时动态发送请求获取微博授 权url 1.1 在 components\common\lab_header.vue 中写oauth动态获取微 博授权**URL // 获取微博登录地址 ...

随机推荐

  1. 按照in条件排序

    --按照in中条件排序 ,,,) order by instr('395,396,399,313',ghdm);

  2. 怎么让一个不定宽高的div垂直水平居中?

    方法一:使用CSS3 transform 父盒子设置:position:relative; div设置:position:absolute;transform:translate(-50%,-50%) ...

  3. 性能监控工具Munin

    实际场景 公司产品需要观察Ubuntu主机性能,以衡量客户现场的产品是否能满足高频使用需求 选型 在比较了诸多工具之后,考虑时间成本因素,用了比较简单的Munin 安装步骤 1. apt-get in ...

  4. C#和JS交互 WebBrowser实例

    本文实现了WebBrowser的简单例子 1.引用System.Windows.Froms.dll 2.引用WindowsFormsIntegration.dll 代码如下: public parti ...

  5. PHP的生成图片或文字水印的类

    ImageWatermark.php <?php /*********************************************************** 类名:ImageWat ...

  6. Windows10 +Ubuntu 18.04双系统安装详细教程

    前言: 由于开发需要,很多用户需要安装Linux环境.如果用虚拟机操作会消耗大量内存,一般的电脑会显得卡顿,从而影响开发效率.这里分享一些安装经验,希望能够帮助大家. 教程: 步骤1:下载镜像文件 从 ...

  7. PHP来控制Linux,ssh2来控制服务器端

    注意:我们用PHP来控制Linux,php环境可以在windows也可以在linux,但是我们要控制的机器是一台linux(被控制的linux关闭selinux和firewalld). 如果php在l ...

  8. 在Eclipse中添加Tomcat

    在Eclipse中开发web或开启web服务需要Tomcat的支持,在添加Tomcat之前要清楚你的Eclipse版本,如果你的Eclipse是javvEE版的就可以直接安装Tomcat,如果不是就需 ...

  9. linux下播放器设计和开发

    http://blog.csdn.net/henryjee/article/details/6737392 本文根据DawnLightPlayer的开发经验写成.DawnLithtPlayer是今天3 ...

  10. OFBiz 16.11.03的直接部署、eclipse部署和IDEA部署

    一.在OFBiz官网下载最新的发行版本,也就是16.11.03版本. 下载地址:http://ofbiz.apache.org/download.html   点击页面Apache OFBiz 16. ...