An introduction to methods based on obtaining various paths from request
- 2020-11-20 06:04:18
- OfStack
equest.getRealPath() is no longer recommended. Instead:
request.getSession().getServletContext().getRealPath()
this. getServletContect().getRealPath()
this.getServlet().getServletContext().getRealPath()
ServletActionContext. getRequest().getRealPath();
Each of the above three retrievals is the absolute path of the current running file on the server
Get the various path summaries from request
request.getRealPath("url"); // Virtual directories map to real directories
request.getRealPath("./"); // The directory where the page is located
request.getRealPath(".. / "); // One level above the directory where the page is located
request.getContextPath(); // The name of the web directory applied
Such as http: / / localhost: 7001 / bookStore /
/bookStore/ = > [contextPath] (request.getContextPath())
Gets the full path to the Web project
String strDirPath = request.getSession().getServletContext().getRealPath("/");
Take the project named TEST as an example:
(1) Get the full path of the current page containing the project name: ES77en. getRequestURI()
Results: / TEST test jsp
(2) Project name: ES87en. getContextPath()
Results: / TEST
(3) Get the full name under the directory of the current page: request. getServletPath()
Results: if the page in jsp directory/TEST jsp/test jsp
(4) Get the full path of the server where the page is located: ES107en.getRealPath (" page.jsp ")
Results: D: \ resin \ webapps \ TEST \ test jsp
absPath=new ES123en.io.File (application.getRealPath (request.getRequestURI ())).getParent ();
Results: D: \ resin \ webapps \ TEST
2. Get the path in the class:
(1) Absolute path of class: ES142en.class.getClass ().getResource ("/").getPath ()
Results: / D: / TEST/WebRoot/WEB - INF/classes pack /
(2) Get the path of the project: ES157en.ES158en (" ES159en.ES160en ")
Results: D: \ TEST
3. Get the path in Servlet:
(1) Project directory: ES169en.getSession ().getServletContext ().ES172en ("") parameters can be specific to the package name.
Results: E: \ Tomcat \ webapps \ TEST
(2) IE Address bar Address: ES183en. getRequestURL()
Results: http: / / localhost: 8080 / TEST/test
(3) Get the relative address: ES194en. getRequestURI()
Results: / TEST/test