// 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 ("/");