There are many methods to obtain the absolute path and relative path in jsp, which are mainly divided into Servlet and jsp code acquisition methods. If you need them, you can refer to them.
// Obtain the relative path and absolute path of the current file in Servlet and JSP.
JSP
Absolute path corresponding to the root directory: request. getRequestURI ()
Absolute path of the file: application. getRealPath (request. getRequestURI ());
Absolute path of the current web application: application. getRealPath ("/");
Obtain the upper-level directory of the request File: new File (application. getRealPath (request. getRequestURI (). getParent ()
3.2 obtain the relative and absolute paths of the current application in Servlet
Absolute path corresponding to the root directory: request. getServletPath ();
Absolute path of the file: request. getSession (). getServletContext (). getRealPath
(Request. getRequestURI ())
Absolute path of the current web application: servletConfig. getServletContext (). getRealPath ("/");
The Code is as follows: |
Copy code |
Out. println ("absolute path corresponding to the root directory:" + request. getRequestURI () + "<br/> ");
String strPathFile = application. getRealPath (request. getRequestURI ()); Out. println ("absolute path of the file:" + strPathFile + "<br/> "); Out. println (application. getRealPath (request. getRequestURI ())); String strDirPath = new File (application. getRealPath (request. getRequestURI (). getParent (); Out. println ("absolute directory path:" + strDirPath + "<br/> ");
|
Program
The Code is as follows: |
Copy code |
String classPath = this. getClass (). getClassLoader (). getResource ("/"). getPath ();
Try { ClassPath = URLDecoder. decode (classPath, "gb2312 "); } Catch (UnsupportedEncodingException e ){ // TODO Auto-generated catch block E. printStackTrace (); }
|
Example
The Code is as follows: |
Copy code |
<Body> Physical path of the current WEB application: <% = application. getRealPath ("/") %> <BR> The physical path of the JSP file you requested: <% = application. getRealPath (request. getRequestURI () %> <BR> <% String path = application. getRealPath (request. getRequestURI ()); String dir = new File (path). getParent (); Out. println ("physical path of the directory where the current JSP file is located" + dir + "</br> "); String realPath1 = "http: //" + request. getServerName () + ":" + request. getServerPort () + request. getContextPath () + request. getServletPath (). substring (0, request. getServletPath (). lastIndexOf ("/") + 1 ); Out. println ("web URL path:" + realPath1 ); %> </Body> |
Get the absolute path of the project in servlet
String filePath = this. getServletConfig (). getServletContext (). getRealPath ("/");
Absolute path corresponding to the root directory: request. getServletPath ();
Absolute path of the file: request. getSession (). getServletContext (). getRealPath (request. getRequestURI ())
Absolute path of the current web application: servletConfig. getServletContext (). getRealPath ("/");
ServletContext objects can be obtained in the following ways:
The Code is as follows: |
Copy code |
Javax. servlet. http. HttpSession. getServletContext () Javax. servlet. jsp. PageContext. getServletContext () Javax. servlet. ServletConfig. getServletContext () Absolute path of the file: request. getSession (). getServletContext (). getRealPath (request. getRequestURI ()) Absolute path of the current web application: servletConfig. getServletContext (). getRealPath ("/"); |
Example
// The application Object in JSP is the ServerContext in Servlet, so it is obtained in Servlet as follows
// Import java. io. File;
System. out. println ("absolute path corresponding to the root directory:" + request. getServletPath () + "<br/> ");
String strPathFile = request. getSession (). getServletContext (). getRealPath (request. getRequestURI ());
System. out. println ("absolute file path:" + strPathFile + "<br/> ");
String strDirPath = new File (request. getSession (). getServletContext (). getRealPath (request. getRequestURI (). getParent ();
System. out. println ("absolute directory path:" + strDirPath + "<br/> ");
The file name cannot contain the following characters :/:*? "<> |
Obtain the full path of a Web project
String strDirPath = getServletContext (). getRealPath ("/");