<%@ page contentType="text/html; charset=utf8" language="java" import="java.io.*" errorPage="" %> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf8"> <title>Get Paths</title> </head> <body> 当前WEB应用的物理路径:<%=application.getRealPath("/")%><BR><hr> 当前你求请的JSP文件的物理路径:<%=application.getRealPath(request.getRequestURI())%><BR><hr> <% String path=application.getRealPath(request.getRequestURI()); String dir=new File(path).getParent(); out.println("当前JSP文件所在目录的物理路径"+dir+"<br><hr><p>"); String realPath1 = "http://" + request.getServerName() + ":" + request.getServerPort() + request.getContextPath()+request.getServletPath().substring(0,request.getServletPath().lastIndexOf("/")+1); out.println("web URL 路径:"+realPath1+"<br><hr>"); String realPath2 = request.getContextPath(); out.println("getContextPath: "+realPath2+"<br><hr>"); String realPath3 = request.getServletPath().substring(0,request.getServletPath().lastIndexOf("/")+1); out.println("getServletPath: "+realPath3); %> </body> </html>
执行结果:
当前WEB应用的物理路径:F:\115Synchronization\code save\JSP\
当前你求请的JSP文件的物理路径:F:\115Synchronization\code save\JSP\web\web\src\fundation\getPath.jsp
当前JSP文件所在目录的物理路径F:\115Synchronization\code save\JSP\web\web\src\fundation
web URL 路径:http://localhost:8080/web/web/src/fundation/
getContextPath: /web
getServletPath: /web/src/fundation/