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 // 获取微博登录地址 ...
随机推荐
- vagrant简介
什么是vagrant? 简单理解,就是可以通过Vagrant这个工具管理虚拟机,比如说想创建一个centos环境的虚拟机,不需要安装系统这么麻烦,通过vagrant可以快速创建 官网地址:https: ...
- Oracle树查询
1.Oracle函数 sys_connect_by_path 语法: select sys_connect_by_path(column_name,'connect_symbo链接标志l') fro ...
- innerhtml outhtml innerText outText 区别
innerHTML获取标签内的HTML outerHTML获取标签及标签内的HTML innerText 设置或获取位于对象起始和结束标签内的文本 outerText 设置(包括标签)或获取(不包括标 ...
- Slackware网卡配置文件和配置工具
Slackware 有关网卡的配置文件是/etc/rc.d/rc.inet1.conf , 这个文件包括乙太网接口的网卡和无线网卡的配置.Slackware 还是比较纯净的,网络配置也较简单:在Sla ...
- ant详细介绍
Ant的概念 可能有些读者并不连接什么是Ant以及入可使用它,但只要使用通过Linux系统得读者,应该知道make这个命令.当编译Linux内核及一些软件的源程序时,经常要用这个命令.Make命令 ...
- cf round480D Perfect Groups
题意:给一个序列,对于每一个连续的区间,区间内的数至少分成几个组,使得每个组内的数任意2个相乘是一个完全平方数(包括0). 输出每个组数的个数. $n \leq 5000 , |a_i| \leq 1 ...
- git操作github指令
常用git命令: $ git clone //本地如果无远程代码,先做这步,不然就忽略 $ cd //定位到你blog的目录下 $ git status //查看本地自己修改了多少文件 $ git ...
- 全球城市群Megalopolis
Megalopolis From Wikipedia, the free encyclopedia (Redirected from Megalopolis (city type)) &quo ...
- phpExcel 操作示例
片段 1 片段 2 phpExcel 操作示例 <?php //写excel //Include class require_once('Classes/PHPExcel.php'); requ ...
- map的三种遍历方法!
map的三种遍历方法! 集合的一个很重要的操作---遍历,学习了三种遍历方法,三种方法各有优缺点~~ /* * To change this template, choose Tools | Te ...