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 // 获取微博登录地址 ...
随机推荐
- Nginx 扫盲
layout: post title: Nginx-扫盲 category: Nginx tags: [代理] Nginx 基本安装和配置文件讲解 简介 轻量级web服务器.反向代理服务 负载均衡策略 ...
- history-之前发生了什么
查看一下之前服务器上执行过的命令.看一下总是没错的,加上前面看的谁登录过的信息,应该有点用.另外作为admin要注意,不要利用自己的权限去侵犯别人的隐私哦. 到这里先提醒一下,等会你可能会需要更新 H ...
- Liferay 7.1发布啦
下载地址: https://cdn.lfrs.sl/releases.liferay.com/portal/7.1.0-m1/liferay-ce-portal-tomcat-7.1-m1-20180 ...
- leetcode 350 easy
350. Intersection of Two Arrays II class Solution { public: vector<int> intersect(vector<in ...
- HR招聘_(一)_招聘意识
最近接触到一点HR的工作,贯穿始终,故有点心得,与众人分享.言辞不尽之处,万望指点一二.不胜感激. HR招聘_(一)_招聘意识HR招聘_(二)_招聘方法论(招聘原因及原则) HR招聘_(三)_招聘方法 ...
- Codeforces 451D
题目链接 D. Count Good Substrings time limit per test 2 seconds memory limit per test 256 megabytes inpu ...
- hdu 1358 Period(KMP入门题)
Period Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Subm ...
- Linux iptables开放特定端口
如果系统已安装则调过安装步骤 查找安装包 yum list | grep iptables 安装iptables yum install iptables-services 重启防火墙使配置文件生效 ...
- 【水滴石穿】douban-movies-react-native
这个项目的话,倒是可以做一个支架页面,就是你需要什么东西,你就可以在里面加,不过也是比较难的地方 就是数据流,数据处理的部分.react可以处理数据的方式很多,没有见过类似于古老的vue时候可以使用的 ...
- scorllview嵌套gridview和listview的兼容问题
ScrollView嵌套GridView或ListView,由于这两款控件都自带滚动条,当他们碰到一起的时候便会出问题,即GridView会显示不全. 解决办法:自定义一个GridView控件 pac ...