日期:2014-05-16  浏览次数:20368 次

在Servlet与JSP中取得当前文件所在的相对路径与绝对路径

System.out.println("根目录所对应的绝对路径:"?+?request.getServletPath()?+?"");?
String?strPathFile?
=?request.getSession().getServletContext().getRealPath(request.getRequestURI());?
System.out.println(
"文件的绝对路径:"?+?strPathFile?+?"");?
String?strDirPath?
=?new?File(request.getSession().getServletContext().getRealPat(request.getRequestURI())).getParent();?
System.out.println(
"目录的绝对路径:"?+?strDirPath?+?"");?
this.getServlet().getServletContext().getRealPath("/");//在struts的action中取得当前系统的根目录

 举例:http://localhost:7001/myservlet/somepath/test?someparam=somevalue
request.getPathInfo():返回
/somepath/test
request.getRequestURL():返回http:
//localhost:7001/myservlet/somepath/test
request.getRequestURI():返回/myservlet/somepath/test
request.getServletPath():返回
/myservlet
request.getQueryString():返回someparam
=somevalue
??? request.getContextPath();? 返回项目名/myservlet