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. python 线性代数模块linalg

  2. Vue--过滤器(私有和公有)

    一.过滤器的基本使用 <!DOCTYPE html> <html lang="en"> <head> <meta charset=&quo ...

  3. 数组的方法之(Array.prototype.filter() 方法)

    filter() 方法创建一个新的数组,新数组中的元素是通过检查指定数组中符合条件的所有元素.     注意: filter() 不会对空数组进行检测.     注意: filter() 不会改变原始 ...

  4. sql server 创建视图添加表时出现从其他数据库导入的表未显示出来

    创建视图添加表时出现从其他数据库导入的表未显示出来,通过数据库刷新,也不能解决.关闭SQL server management studio 后,再次进入,在创建视图的时候添加表的列表就出现了新导入的 ...

  5. QT加载qss

    QString CommonHelper::setStyle(const QString &style) { QByteArray str; QFile qss(style); qss.ope ...

  6. Python 模块chardet安装过程(windows环境)

    最近需要一个txt文件的批量转码功能,在网上找到一段批量处理java源文件的py程序如下: #-*- coding: utf-8 -*- import codecs import os import ...

  7. VUE中的style 样式处理的Scope (<style scope>)

    在VUE组件中,为了让样式私有化,不对全局造成污染,可以在style标签上添加scoped属性以表示它的只属于当下的模块. 但是这样的话,就会导致无法修改其他第三方组件样式,或者是内嵌的样式,解决方案 ...

  8. MaxCompute助力小影短视频走向全球化

    数字时代,中国已经成为世界互联网的中心,小影(海外版称作为VivaVideo,后简称VivaVideo)作为国内首批短视频出海企业,借助统一的云计算平台快速实现全球业务的线上部署,已经让每一行代码都获 ...

  9. HDU 4193

    本题思路:用sum[]数组维护前缀和, 当然这里需要把原数组扩大为原来的两倍. 然后对于任意一个长度为n的区间 k.....k+n-1,如果有该区间内的最小值大于等于sum[k-1]那么该种情况就符合 ...

  10. AtCoder Beginner Contest 079 D - Wall【Warshall Floyd algorithm】

    AtCoder Beginner Contest 079 D - Wall Warshall Floyd 最短路....先枚举 k #include<iostream> #include& ...