1. Definition
Absolute path: The absolute path is the real path on the hard disk of the file or directory on your home page, (URL and physical path) such as:
C:/xyz/test.txt represents the absolute path to the Test.txt file. Http://www.baidu.com/index.htm also represents a
The URL absolute path.
Relative path: The path relative to a base directory. Contains the relative path to the web (relative to the directory in HTML), for example:
In the servlet, "/" represents the root directory of the Web App. and the relative representation of the physical path. For example: "./" represents the current directory,
".. /"represents the parent directory. This similar representation also belongs to the relative path.
2, the relative path in Jsp/servlet
2.1 Relative path with "/"
Important: Determine whether a server call or a browser call
(1) Relative path with "/" on server side (server call)
Refers to the address of your Web project, which is resolved on the server side (different from the relative addresses in HTML and JavaScript, which are parsed by the client browser).
The relative address in the JSP and servlet should be relative to your Web project, that is, relative to http://localhost:8080/webapp/.
For example: Request.getrequestdispatcher ("/reg/reg.jsp"). Forward (Request,response)
The address here is the server-side call, "/" relative to the current Web application WebApp,
The absolute address is: http://localhost:8080/webapp/reg/reg.jsp.
(2) Relative path of the client with "/" (Browser Call)
Relative addresses in all HTML pages are relative to the server root directory (http://localhost:8080/).
Instead of (with the directory of the Web app under the directory) http://localhost:8080/webapp/.
The address of the Action property of the form form in HTML should be relative to the server root directory (http://localhost:8080/)
For example: if submitted to toreg.jsp: action= "/webapp/reg.jsp" or action= "<%=request.getcontextpath ()%>"/reg.jsp;
Submit to Servlet for actiom= "/webapp/regservlet"
JavaScript is also parsed on the client side, so its relative path is the same as the form form.
Therefore, in general, in the Jsp/html page and other references such as css,javascript.action attributes are best added
<%=request.getcontextpath ()%> To ensure that the referenced files belong to a directory in the Web App.
In addition, you should try to avoid the use of similar ".", "./", ". /.. /"such as relative path relative to the location of the file, so
When a file moves, it is prone to problems.
2.2 Relative path without "/"
For example, if the current page path is http://localhost:8080/myWebApp/login.jsp then accessing "main.jsp" on this page is Access http://localhost:8080/myWebApp/main.jsp
That is, using a relative path without "/" is the path to the file in the directory where the current page path belongs (this feature is applicable both on the server side and on the client)
3. Get the relative path and absolute path of the current app in Jsp/servlet
3.1 The absolute path to the current app's relative path and absolute path
root: Request.getrequesturi ()
Absolute path to file : Application.getrealpath (Request.getrequesturi ());
Absolute path to the current web app: Application.getrealpath ("/");
Get the upper-level directory of the requested file: New file (Application.getrealpath (Request.getrequesturi ())). GetParent ()
Obtaining the relative and absolute paths of the current application in the 3.2 servlet
Absolute path for root directory: Request.getservletpath ();
Absolute path to File: Request.getsession (). Getservletcontext (). Getrealpath
(Request.getrequesturi ())
Absolute path to the current web app: Servletconfig.getservletcontext (). Getrealpath ("/");
(ServletContext objects are available in several ways:
Javax.servlet.http.HttpSession.getServletContext ()
Javax.servlet.jsp.PageContext.getServletContext ()
Javax.servlet.ServletConfig.getServletContext ()
)
3.3 Get project root REQUEST at all levels of SSH
Java code
- 1.system.out.println (Servletactioncontext.getservletcontext (). Getrealpath ("/"));
- 2.ystem.out.println (Struts2utils.getparameter ("LoginName"));
- System.out.println (Servletactioncontext.getservletcontext (). Getrealpath ("/"));
- System.out.println (Struts2utils.getparameter ("LoginName"));
Using the Servletactioncontext class of STRUTS2
He's a thread-safe use of the threadlocal
or using Springside's Struts2utils class
He encapsulated the Servletactioncontext class of Struts2.
Java (web) relative path, absolute path