jsp Request获取url信息的各种方法比较
从Request对象中可以获取各种路径信息,以下例子:
假设请求的页面是index.jsp,项目是WebDemo,则在index.jsp中获取有关request对象的各种路径信息如下
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
String remoteAddress=request.getRemoteAddr();
String servletPath=request.getServletPath();
String realPath=request.getRealPath("/");
String remoteUser=request.getRemoteUser();
String requestURI=request.getRequestURI();
out.println("path:"+path+"<br>");
out.println("basePath:"+basePath+"<br>");
out.println("remoteAddr:"+remoteAddress+"<br>");
out.println("servletPath:"+servletPath+"<br>");
out.println("realPath:"+realPath+"<br>");
out.println("remoteUser:"+remoteUser+"<br>");
out.println("requestURI:"+requestURI+"<br>");
结果:
path:/WebDemo
basePath:http://localhost:8683/WebDemo/
remoteAddr:127.0.0.1
servletPath:/index.jsp
realPath:D:\apache-tomcat-6.0.13\webapps\WebDemo\
remoteUser:null
requestURI:/WebDemo/index.jsp
从上不难看出request各个对应方法所代表的含义
从request获取各种路径总结:
request.getRealPath("url");//虚拟目录映射为实际目录
request.getRealPath("./");//网页所在的目录
request.getRealPath("../");//网页所在目录的上一层目录
假定你的web application(web应用)名称为news,你的浏览器中输入请求路径:http://localhost:8080/uploading/load.jsp
request.getContextPath() => /uploading
request.getServletPath() => /load.jsp
request.getRequestURL() => http://localhost:8080/uploading/load.jsp
request.getRealPath("/") => F:\learn\.metadata\.plugins\org.eclipse.wst.server.core\tmp0\wtpwebapps\uploading\
现在request.getRealPath("/") 这个方法已经不推荐使用了
可以使用
ServletContext.getRealPath(java.lang.String) instead.
request.getSession().getServletContext().getRealPath() 得到工程文件的实际物理路径,也就是绝对地址
//Returns the part of this request's URL from the protocol name up to the query string in the first line of the HTTP request
// eg. /manage/editExam.domethod=goExamSet&type=U
String url = request.getRequestURI();
//The returned URL contains a protocol, server name, port number, and server path, but it does not include query string parameters
//eg. http://127.0.0.1:8080/manage/editExam.domethod=goExamSet&type=U
StringBuffer url_buffer = request.getRequestURL();
HttpServletRequest 的这两种方法都只能得到不包含参数的请求url,区别如下:
1 前者返回相对路径,后者返回完整路径
2 前者返回string ,后者返回stringbuffer
得到完整请求url可以通过如下方法,getQueryString()得到的是url后面的参数串,和前者相加就是带参数的请求路径了
String queryString = request.getQueryString();
ring fullPath = url + queryString; // 或者是url_buffer.toString()+queryString;
jsp Request获取url信息的各种方法比较的更多相关文章
- javascript获取url信息的常见方法
先以"http://www.cnblogs.com/wuxibolgs329/p/6188619.html#flag?test=12345"为例,然后获得它的各个组成部分. 1.获 ...
- jQuery 获取 URL信息
jQuery获取URL信息有很多方法,但是使用这个插件就非常爽了. 托管地址在:http://github.com/allmarkedup/jQuery-URL-Parser // http: //l ...
- Request获取url各种信息的方法
1.Request获取url各种信息的方法 测试的url地址:http://www.test.com/testweb/default.aspx, 结果如下: Request.ApplicationPa ...
- 一个用php实现的获取URL信息的类
获取URL信息的类 使用这个类,你能获得URL的如下信息: - Host - Path - Statuscode (eg. 404,200, ...) - HTTP Version - Ser ...
- 获取url查询参数的方法
/** * 获取url查询参数的方法 * @param name * @returns {null} * @constructor */ function GetQueryString(name) { ...
- vue不通过路由直接获取url中参数的方法示例
vue不通过路由直接获取url中参数的方法示例 vuejs取得URL中参数的值地址:http://localhost:3333/#/index?id=128console.log(this.$rout ...
- request获取url的方法总结
辣么多属性.方法 不用就忘了 ,当需要用的时候挠头也想不到,现在总结一下 以备用 例如:http://localhost/testweb/default.aspx 1.Request.Applic ...
- jsp request 获取路径
这篇教程不错:http://zjutsoft.iteye.com/blog/1084260 自己试验如下: System.out.println("-----------------serv ...
- 微信第三方登陆,无需注册一键登录,获取用户信息,PHP实现方法
今天讲讲利用微信oauth2实现第三方登陆的实现方法. 先说说前提吧! 首先你得是服务号,并且是经过认证的.这样微信会给你很多第三方接口的权限,如果是订阅号或者没有认证的服务号那就不用想了! 一开始你 ...
随机推荐
- Java数据类型、变量、运算符、语句。
数据类型:整型 int long short byte小数 double float 字符 char 转义字符:\'(单引号字符) \\(反斜杠字符) \n(换行) \r(回车) \t(水平制表符,相 ...
- linux下安装nodejs
之前安装过windows下的node,感觉还是很方便的,不成想今天安装linux下的坑了老半天,特此记录. 1. 下载node.js,官方有提供源码版本和编译版的,方便起见我使用编译版的,下载后解压缩 ...
- HDU 1403-Longest Common Substring (后缀数组)
Description Given two strings, you have to tell the length of the Longest Common Substring of them. ...
- eclipse的tomcat内在溢出
在最前或最后加上: -Xms256m -Xmx512m -Xss2024K -XX:PermSize=128m -XX:MaxPermSize=256m
- Longest Valid Parentheses
Given a string containing just the characters '(' and ')', find the length of the longest valid (wel ...
- Serializable unordered set
Serializable unordered set 可序列化哈希set #include <boost/algorithm/string/predicate.hpp> #include ...
- OC 相关
1.OC runtime的理解[转载] http://www.csdn.net/article/2015-07-06/2825133-objective-c-runtime/1
- Node.JS 学习路线图
转载自:http://www.admin10000.com/document/4624.html 从零开始nodejs系列文章, 将介绍如何利Javascript做为服务端脚本,通过Nodejs框架w ...
- Windows下面如何建立多个Django虚拟环境
Linux 安装请见另一篇博客 http://www.cnblogs.com/zhaoyingjie/p/6180758.html 1.安装 virtualenv 2.安装virtualenvwrap ...
- JS 进制转换
十进制转换成其他进制 objectname.toString([radix]) objectname 必选项.要得到字符串表示的对象. radix 可选项.指定将数字值转换为字符串时的进制. 例如 ...