Web应用中request获取path,URI,URL
Web应用中有各种获取path或URI,URL的方法,假设网页访问地址:
http://localhost:8080/tradeload/TestServlet
Web应用context: /tradeload
各路径鉴定如下:
- request.getContextPath()= /tradeload
- request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort()= http://localhost:8080
- request.getRequestURL() = http://localhost:8080/tradeload/TestServlet
- request.getRequestURI() = /tradeload/TestServlet
- request.getPathInfo() = null
- request.getServletPath() = /TestServlet
- 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的更多相关文章
- (转)WebSphere的web工程中怎么获取数据源
原文:http://aguu125.iteye.com/blog/1694313 https://blog.csdn.net/bigtree_3721/article/details/44900325 ...
- js中如何获取页面的Url,域名和端口号
有时候通过获取上个页面的Url来做一个跳转,获取域名防止非正常访问 获取上一个页面的一个URL,这个URL一般做一个页面的跳转 window.location.href <script>w ...
- JSP中request获取值
获取项目名:request.getContextPath(); 获取IP:request.getServerName() 获取端口:request.getServerPort() pageContex ...
- Web API中如何获取相对地址的绝对地址 Server.MapPath
var sPath = System.Web.Hosting.HostingEnvironment.MapPath("/FilePath/");
- web项目中各种路径的获取
以工程名为/DemoWeb为例: 访问的jsp为:http://localhost:8080/DemoWeb/test/index.jsp 1 JSP中获得当前应用的相对路径和绝对路径 (1)得到工程 ...
- Request 获取Url
1.获取页面,HttpContext.Current.Request也是Request //获取当前页面url string myurl = HttpContext.Current.Request.U ...
- C# Request 获取Url
1.获取页面,HttpContext.Current.Request也是Request //获取当前页面url string myurl = System.Web.HttpContext.Curren ...
- Spring中如何获取request的方法汇总及其线程安全性分析
前言 本文将介绍在Spring MVC开发的web系统中,获取request对象的几种方法,并讨论其线程安全性.下面话不多说了,来一起看看详细的介绍吧. 概述 在使用Spring MVC开发Web系统 ...
- 04.vue获取微博授权URL
1.在Vue页面加载时动态发送请求获取微博授 权url 1.1 在 components\common\lab_header.vue 中写oauth动态获取微 博授权**URL // 获取微博登录地址 ...
随机推荐
- SQL Sever实验三 视图与数据更新
一. 实验目的 1.掌握创建视图的 SQL 语句,数据更新的 SQL 语句. 2.了解使用创建视图向导创建视图的方法. 3.掌握使用 SQL 创建视图的方法,使用 SQL 更新数据的方法. 二. 实验 ...
- Shell中字符串、数值的比较
原文:http://apps.hi.baidu.com/share/detail/31263915 在shell中字符串与数值的比较方法是不同的,要注意区分 整数比较: -eq 等于 ...
- ubuntu上源码编译安装mysql5.7.27
一.查看操作系统环境和目录结构,并创建mysql用户和组,以及规划安装mysql所需要的目录. #cat /etc/issue 查看发行版本信息: #cat /proc/version 查看正在运行 ...
- JavaScript基本的使用
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- 精密MRAM芯片制造系统
MRAM是一种非常复杂的薄膜多层堆叠,由10多种不同材料和超过30层以上的薄膜与堆叠组成,部分薄膜层的厚度仅达数埃,比人类的发丝还要薄500000倍,相近于一颗原子的大小,如何控制这些薄膜层的厚度.沉 ...
- Android——app基础
Android Application基础 系统启动过程 APK文件介绍 APK是Android Package的缩写,即android安装包.APK 文件其实是zip 格式,但后缀名被修改为apk ...
- StringBuilder / StringBuffer类
StringBuilder类 和 StringBuffer 类的由来? 在Java中使用String 类可以描述所有的字符串数据,但是String类的对象一旦创建,则该对象的字符序列 就不可更改,当需 ...
- python第一天 :计算机基础(一)
1.什么是编程语言 答:人类与计算机交流的介质 2.什么是编程 答:利用编程语言控制计算机解决问题 3.为什么要编程 答:可以控制计算机做事,提高生产生活效率 4.计算机的五大组成部分分别有什么作用? ...
- python mooc 3维可视化<第一周第二&三单元>
小结: 创建 数据对象 structuredGrid grid 使用contourfilter con PolyDataMapper m Actor a 使用 MaskPoint3D mask gly ...
- go编程资料库
1.Go语言圣经(中文版) https://books.studygolang.com/gopl-zh/