从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信息的各种方法比较的更多相关文章

  1. javascript获取url信息的常见方法

    先以"http://www.cnblogs.com/wuxibolgs329/p/6188619.html#flag?test=12345"为例,然后获得它的各个组成部分. 1.获 ...

  2. jQuery 获取 URL信息

    jQuery获取URL信息有很多方法,但是使用这个插件就非常爽了. 托管地址在:http://github.com/allmarkedup/jQuery-URL-Parser // http: //l ...

  3. Request获取url各种信息的方法

    1.Request获取url各种信息的方法 测试的url地址:http://www.test.com/testweb/default.aspx, 结果如下: Request.ApplicationPa ...

  4. 一个用php实现的获取URL信息的类

    获取URL信息的类 使用这个类,你能获得URL的如下信息: - Host  - Path  - Statuscode (eg. 404,200, ...)  - HTTP Version  - Ser ...

  5. 获取url查询参数的方法

    /** * 获取url查询参数的方法 * @param name * @returns {null} * @constructor */ function GetQueryString(name) { ...

  6. vue不通过路由直接获取url中参数的方法示例

    vue不通过路由直接获取url中参数的方法示例 vuejs取得URL中参数的值地址:http://localhost:3333/#/index?id=128console.log(this.$rout ...

  7. request获取url的方法总结

    辣么多属性.方法  不用就忘了  ,当需要用的时候挠头也想不到,现在总结一下 以备用 例如:http://localhost/testweb/default.aspx 1.Request.Applic ...

  8. jsp request 获取路径

    这篇教程不错:http://zjutsoft.iteye.com/blog/1084260 自己试验如下: System.out.println("-----------------serv ...

  9. 微信第三方登陆,无需注册一键登录,获取用户信息,PHP实现方法

    今天讲讲利用微信oauth2实现第三方登陆的实现方法. 先说说前提吧! 首先你得是服务号,并且是经过认证的.这样微信会给你很多第三方接口的权限,如果是订阅号或者没有认证的服务号那就不用想了! 一开始你 ...

随机推荐

  1. Hadoop安装及配置

    一.系统及软件环境 1.操作系统 CentOS release 6.5 (Final) 内核版本:2.6.32-431.el6.x86_64 master.fansik.com:192.168.83. ...

  2. ACM water

    1000  纯属适应题 1003  做的时候花了很久,现在看好像也不难 1004  适应题,求下平均就行 1005  要读懂题就行 1007  逆序数,discuss方法 1046  全部暴搜一遍.. ...

  3. Ret2Libc 练习(1) -- ZwSetInformationProcess

    花了两个小半晚上的时间将0day安全这本书的绕过DEP的第一个实验做了,这里做些笔记. Ret2libc 我现在自己的理解就是在开启DEP保护的情况下,在程序的其他的可执行位置找到可以满足我利用要求的 ...

  4. Spell-DBC

    Spell.dbc 1  ID2  Attributes               属性3  AttributesEx             属性 4  AttributesExB         ...

  5. CSS盒子模型的一些理解

    盒子模型相当于把现实中的盒子形象化. 盒子模型的大小="内容(content)+内填充(padding)+边框(border)+外边距(margin)" 盒子模型方向为:top, ...

  6. Vim的分割窗口split命令

    显示两个不同的文件:或者同时显示一个文件的两个不同地方:又或者并排比较两个文件.这一切都可以通过分割窗口实现. 打开新窗口最简单的命令如下: :split (水平分割) || :vsplit (垂直分 ...

  7. LINQ to XML

    void Main() { string path = @"C:\Users\knife\Desktop\test.xml"; XDocument xml = XDocument. ...

  8. Maven新建webapp项目index.jsp报错

    最近用eclipse新建了一个maven项目,结果刚新建完成index.jsp页面就报错了,先把错误信息贴出来看看 后来就找资料,结果发现两种解决办法,希望可以帮助用得上的人! 第一种:直接在pom. ...

  9. delphi如何让程序最小化到任务栏(转)

    现在很多的应用程序都有这样一种功能,当用户选择最小化窗口时,窗口不是象平常那样最小化到任务栏上,而是“最小化”成一个任务栏图标.象FoxMail 3.0 NetVampire 3.0等都提供了这样的功 ...

  10. Rails : css或js文件无法成功预编译或调用jquery类插件时预编译问题

    调用bootstrap css框架时,将bootstrap文件夹放入 vendor/assets/下 bootstrap文件结构如下:    [shenma@localhost demo]$ ls v ...