阿里云服务器免费领卷啦。

捡代码论坛-最全的游戏源码下载技术网站!

 找回密码
 立 即 注 册

QQ登录

只需一步,快速开始

搜索
关于源码区的附件失效或欺骗帖, 处理办法
查看: 1859|回复: 0

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

[复制链接]

4208

主题

210

回帖

12万

积分

管理员

管理员

Rank: 9Rank: 9Rank: 9

积分
126168
QQ
发表于 2017-3-8 15:03:09 | 显示全部楼层 |阅读模式
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;
复制代码




00

捡代码论坛-最全的游戏源码下载技术网站! - 论坛版权郑重声明:
1、本主题所有言论和图片纯属会员个人意见,与本论坛立场无关
2、本站所有主题由该帖子作者发表,该帖子作者与捡代码论坛-最全的游戏源码下载技术网站!享有帖子相关版权
3、捡代码论坛版权,详细了解请点击。
4、本站所有内容均由互联网收集整理、网友上传,并且以计算机技术研究交流为目的,仅供大家参考、学习,不存在任何商业目的与商业用途。
5、若您需要商业运营或用于其他商业活动,请您购买正版授权并合法使用。 我们不承担任何技术及版权问题,且不对任何资源负法律责任。
6、如无法链接失效或侵犯版权,请给我们来信:jiandaima@foxmail.com

回复

使用道具 举报

*滑块验证:
您需要登录后才可以回帖 登录 | 立 即 注 册

本版积分规则

技术支持
在线咨询
QQ咨询
3351529868

QQ|手机版|小黑屋|捡代码论坛-专业源码分享下载 ( 陕ICP备15015195号-1|网站地图

GMT+8, 2024-4-25 02:28

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表