request获取各种路径

request获取各种路径

equest.getRealPath() 这个方法已经不推荐使用了,代替方法是:

request.getSession().getServletContext().getRealPath()

在servlet里用this.getServletContect().getRealPath()

在struts里用this.getServlet().getServletContext().getRealPath()

在Action里用ServletActionContext.getRequest().getRealPath();

以上三个获得都是当前运行文件在服务器上的绝对路径

 
 
结果:E:TomcatwebappsTEST 


(2)得到IE地址栏地址:request.getRequestURL() 
结果:http://localhost:8080/TEST/test 


(3)得到相对地址:request.getRequestURI() 
结果:/TEST/test
 
(4)得到项目的路径:request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+request.getContextPath()
结果:http://localhost:8080/TEST