jsp Request获取url信息的各种方法比较

  1. 从Request对象中可以获取各种路径信息,以下例子:
  2. 假设请求的页面是index.jsp,项目是WebDemo,则在index.jsp中获取有关request对象的各种路径信息如下
  3. String path = request.getContextPath();
  4. String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
  5. String remoteAddress=request.getRemoteAddr();
  6. String servletPath=request.getServletPath();
  7. String realPath=request.getRealPath("/");
  8. String remoteUser=request.getRemoteUser();
  9. String requestURI=request.getRequestURI();
  10. out.println("path:"+path+"<br>");
  11. out.println("basePath:"+basePath+"<br>");
  12. out.println("remoteAddr:"+remoteAddress+"<br>");
  13. out.println("servletPath:"+servletPath+"<br>");
  14. out.println("realPath:"+realPath+"<br>");
  15. out.println("remoteUser:"+remoteUser+"<br>");
  16. out.println("requestURI:"+requestURI+"<br>");
  17. 结果:
  18. path:/WebDemo
  19. basePath:http://localhost:8683/WebDemo/
  20. remoteAddr:127.0.0.1
  21. servletPath:/index.jsp
  22. realPath:D:\apache-tomcat-6.0.13\webapps\WebDemo\
  23. remoteUser:null
  24. requestURI:/WebDemo/index.jsp
  25. 从上不难看出request各个对应方法所代表的含义
  1. 从request获取各种路径总结:
  2. request.getRealPath("url");//虚拟目录映射为实际目录
  3. request.getRealPath("./");//网页所在的目录
  4. request.getRealPath("../");//网页所在目录的上一层目录
  5. 假定你的web application(web应用)名称为news,你的浏览器中输入请求路径:http://localhost:8080/uploading/load.jsp
  6. request.getContextPath()  =>  /uploading
  7. request.getServletPath()  =>  /load.jsp
  8. request.getRequestURL()  =>  http://localhost:8080/uploading/load.jsp
  9. request.getRealPath("/")  =>   F:\learn\.metadata\.plugins\org.eclipse.wst.server.core\tmp0\wtpwebapps\uploading\
  10. 现在request.getRealPath("/") 这个方法已经不推荐使用了
  11. 可以使用
  12. ServletContext.getRealPath(java.lang.String)  instead.
  13. request.getSession().getServletContext().getRealPath() 得到工程文件的实际物理路径,也就是绝对地址
    1. //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
    2. //  eg.     /manage/editExam.domethod=goExamSet&type=U
    3. String url = request.getRequestURI();
    4. //The returned URL contains a protocol, server name, port number, and server path, but it does not include query string parameters
    5. //eg.      http://127.0.0.1:8080/manage/editExam.domethod=goExamSet&type=U
    6. StringBuffer url_buffer = request.getRequestURL();
    7. HttpServletRequest 的这两种方法都只能得到不包含参数的请求url,区别如下:
    8. 1 前者返回相对路径,后者返回完整路径
    9. 2 前者返回string ,后者返回stringbuffer
    10. 得到完整请求url可以通过如下方法,getQueryString()得到的是url后面的参数串,和前者相加就是带参数的请求路径了
    11. String queryString = request.getQueryString();
    12. ring fullPath = url + queryString;   // 或者是url_buffer.toString()+queryString;
时间: 2024-08-02 14:51:26

jsp Request获取url信息的各种方法比较的相关文章

javascript获取url信息的常见方法

先以"http://www.cnblogs.com/wuxibolgs329/p/6188619.html#flag?test=12345"为例,然后获得它的各个组成部分. 1.获取页面完整的url var a=location.href; console.log(a); // "http://www.cnblogs.com/wuxibolgs329/p/5261577.html#flag?test=12345" 2.获取页面的域名 var host = windo

jQuery 获取 URL信息

jQuery获取URL信息有很多方法,但是使用这个插件就非常爽了. 托管地址在:http://github.com/allmarkedup/jQuery-URL-Parser // http: //localhost:19090/home/index?id=1 var source = $.url.attr("source"); // http://localhost:19090/home/index?id=1 var protocol = $.url.attr("proto

Request获取url各种信息的方法

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

在JSP页面获取当前项目名称的方法

在JSP页面获取当前项目名称的方法: 方法1: <%= this.getServletContext().getContextPath() %> 方法2: 使用EL表达式(如果不清楚EL表达式是什么,大家可以百度一下) ${pageContext.request.contextPath} source: http://blog.csdn.net/zhengqiqiqinqin/article/details/8485307

thinkphp5.0 获取URL信息

获取URL信息 $request = Request::instance(); // 获取当前域名 echo 'domain: ' . $request->domain() . '<br/>'; // 获取当前入口文件 echo 'file: ' . $request->baseFile() . '<br/>'; // 获取当前URL地址 不含域名 echo 'url: ' . $request->url() . '<br/>'; // 获取包含域名的

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

获取URL信息的类 使用这个类,你能获得URL的如下信息: - Host  - Path  - Statuscode (eg. 404,200, ...)  - HTTP Version  - Server  - Content Type  - Date  - The whole header string of the URL 复制代码 代码如下: <? /** * Class for getting information about URL's * @author    Sven Wage

request获取url的方法总结

辣么多属性.方法  不用就忘了  ,当需要用的时候挠头也想不到,现在总结一下 以备用 例如:http://localhost/testweb/default.aspx 1.Request.ApplicationPath;  获取服务器上ASP.NET应用程序的虚拟应用程序根路径 2.Request.AppRelativeCurrentExecutionFilePath;  获取应用程序根的虚拟路径,使用~使成为相对路径3. Request.CurrentExecutionFilePath;  获

jsp request 获取路径

这篇教程不错:http://zjutsoft.iteye.com/blog/1084260 自己试验如下: System.out.println("-----------------servlet-----------------"); System.out.println("realpath null:"+request.getRealPath("")); System.out.println("realpath index.jsp:

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

今天讲讲利用微信oauth2实现第三方登陆的实现方法. 先说说前提吧! 首先你得是服务号,并且是经过认证的.这样微信会给你很多第三方接口的权限,如果是订阅号或者没有认证的服务号那就不用想了! 一开始你需要进入微信公众平台开启开发模式,并且填写oauth2的回调地址,地址填写你项目的域名就可以了.比如:www.baidu.com或zhidao.baidu.com.如果你的项目在二级域名就写二级域名 前端url授权地址,在url中填写appid与你项目中方法中的oauth的地址,具体在下面的代码中可