Obtain various paths in a web project, and obtain the paths of a web project
Take the project name/DemoWeb as an example:
The accessed jsp is: http: // localhost: 8080/DemoWeb/test/index. jsp.
1. Obtain the relative and absolute paths of the current application in JSP.
(1) obtain the Project name: request. getContextPath ()
Result:/DemoWeb
(2) obtain the full path of the current page containing the Project name: request. getRequestURI ()
Result:/DemoWeb/test/testpath. jsp
(3) obtain the address in the IE Address Bar: request. getRequestURL ()
Result: http: // localhost: 8080/DemoWeb/test/testpath. jsp
(4) obtain the full name in the directory where the current page is located: request. getServletPath ()
Result:/test/testpath. jsp
(5) obtain the full path (actual path) of the server where the page is located: application. getRealPath ("testpath. jsp ")
Result: D: \ Develop Files \ apache-tomcat-5.5.15 \ apache-tomcat-5.5.15 \ webapps \ DemoWeb \ testpath. jsp
D: \ Develop Files \ apache-tomcat-5.5.15 \ apache-tomcat-5.5.15 is the installation path of tomcat.
(6) There are various methods for obtaining paths, Uris, and URLs in Web applications. Assume that the webpage access address is:
Http: // localhost: 8080/tradeload/TestServlet
Web application context:/tradeload
- Request. getContextPath () =/tradeload
- Request. getScheme () + ": //" + request. getServerName () + ":" + request. getServerPort () = http: // localhost: 8080
- Request. getRequestURL () = http: // localhost: 8080/tradeload/TestServlet
- Request. getRequestURI () =/tradeload/TestServlet
- Request. getPathInfo () = null
- Request. getServletPath () =/TestServlet
- GetServletContext (). getRealPath ('/') = C: \ server \ glassfish \ domains \ domain1 \ applications \ j2ee-modules \ tradeload \
2. Methods for obtaining relative paths and absolute paths in java Class
(1) absolute path of the class:
System. out. println (TestPath. class. getResource ("/"). getPath ());
Result:/E:/workspace/workspace_tcc/DemoWeb/WebRoot/WEB-INF/classes/
System. out. println (TestPath. class. getResource (""));
Result: file:/E:/workspace/workspace_tcc/DemoWeb/WebRoot/WEB-INF/classes/demo1/
(2) obtain the project path: System. getProperty ("user. dir ")
Result: E: \ workspace \ workspace_tcc \ DemoWeb
(3) obtain the absolute path for project deployment:
// Obtain the name of the running project
WebApplicationContext wac = ContextLoader. getCurrentWebApplicationContext ();
ServletContext SC = wac. getServletContext ();
String projectName = SC. getContextPath ();
// Obtain the running project path, that is, the project path under tomcat (by default)
// Tomcat conf server. if the xml configuration file has the configuration project path <Context path = "" docBase = "D: \ seed" reloadable = "false"/>, It is the docBase path.
WebApplicationContext webApplicationContext = ContextLoader. getCurrentWebApplicationContext ();
ServletContext servletContext = webApplicationContext. getServletContext ();
String projectPath = servletContext. getRealPath ("/"). replace ("\\","/");