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标签:String,url,request,Request,getRealPath,jsp,println,out From: https://blog.51cto.com/u_15950441/6039062
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
10. 得到完整请求url可以通过如下方法,getQueryString()得到的是url后面的参数串,和前者相加就是带参数的请求路径了
11. String queryString = request.getQueryString();
12. ring fullPath = url + queryString; // 或者是url_buffer.toString()+queryString;