The method is no longer recommended to use the issue.getRealPath() method, instead:
request.getSession().getServletContext().getRealPath()
Use this.getServletContect().getRealPath() in servlet
Use this.getServlet().getServletContext().getRealPath() in struts
Use ServletActionContext.getRequest().getRealPath() in Action;
The above three are all the absolute paths of the currently running file on the server
Get various path summary from request
request.getRealPath("url"); // The virtual directory is mapped to the actual directory
request.getRealPath("./"); // The directory where the web page is located
request.getRealPath("../"); // The previous directory of the directory where the web page is located
request.getContextPath(); // The name of the application web directory
For example, http://localhost:7001/bookStore/
/bookStore/ => [contextPath] (request.getContextPath())
Get the full path to the web project
String strDirPath = request.getSession().getServletContext().getRealPath("/");
Take the project name TEST as an example:
(1) Get the full path of the current page containing the project name: request.getRequestURI()
Results: /TEST/test.jsp
(2) Get the project name: request.getContextPath()
Results: /TEST
(3) Get the full name in the directory where the current page is located: request.getServletPath()
Result: If the page is in the jsp directory /TEST/jsp/test.jsp
(4) Get the full path of the server where the page is located: application.getRealPath("page.jsp")
Result: D:/resin/webapps/TEST/test.jsp
(5) Get the absolute path to the server where the page is located: absPath=new java.io.File(application.getRealPath(request.getRequestURI())).getParent();
Results: D:/resin/webapps/TEST
2. Get the path in the class:
(1) Absolute path to class: Class.class.getClass().getResource("/").getPath()
Results: /D:/TEST/WebRoot/WEB-INF/classes/pack/
(2) Get the path to the project: System.getProperty("user.dir")
Results: D:/TEST
3. Get the path in the servlet:
(1) Get the project directory: the request.getSession().getServletContext().getRealPath("") parameter can be specified in the package name.
Results: E:/Tomcat/webapps/TEST
(2) Get the IE address bar address: request.getRequestURL()
Results: http://localhost:8080/TEST/test
(3) Get the relative address: request.getRequestURI()
Results: /TEST/test