// Obtain the relative path and absolute path of the current file in Servlet and JSP.
// In JSP
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/> ");
// Servlet
// 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 path of the file:" 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 ("/");