1. Understanding of Basic concepts
Absolute path: Absolute path is the true path of the file or directory on your home page on your hard disk, (URL and physical path) For example: C:\xyz\test.txt represents the absolute path of the Test.txt file. Http://www.sun.com/index.htm also represents a URL absolute path.
Relative path: The path relative to a base directory. Contains the relative path of the web (relative directories in HTML), for example: In a servlet, "/" represents a Web application's directory. The relative representation of the physical path. For example: "./" represents the current directory, "... /"represents the parent directory. This similar representation is also a relative path. For additional information about Uri,url,urn, please refer to the RFC related documentation standard. RFC 2396:uniform Resource Identifiers (URI): Generic Syntax, (http://www.ietf.org/rfc/rfc2396.txt)
2. About relative paths and absolute paths in Jsp/servlet.
2.1 Server-side addresses
The relative address on the server side refers to the address of your Web application, which is resolved on the server side (unlike the relative addresses in HTML and JavaScript, which are parsed by the client browser). The relative addresses in the JSP and servlet should be relative to your Web application, that is, relative to the http://192.168.0.1/webapp/.
The place to use is: Forward:servlet in the Request.getrequestdispatcher (address), which is resolved on the server side, so you want to forward to a.jsp should write this: Request.getrequestdispatcher ("/user/a.jsp") this/relative to the current Web application WebApp, its absolute address is: http://192.168.0.1/webapp/user/a.jsp. Sendredirect: In JSP
2.2, the address of the client all the relative addresses in all HTML pages are relative to the server root (HTTP://192.168.0.1/) rather than the directory of the Web application under the directory http://192.168.0.1/webapp/. The address of the form's action attribute in HTML should be relative to the server root (HTTP://192.168.0.1/), so if you commit to a.jsp as: action= "/webapp/user/a.jsp" or "action=" "/USER/A.JSP;
Submit to Servlet for actiom= "/webapp/handleservlet"
JavaScript is also parsed on the client side, so the relative path is the same as the form form.
Therefore, in general, it is best to precede attributes such as jsp/html pages, such as Css,javascript.action, to ensure that the referenced files belong to a directory in the Web application. In addition, you should try to avoid using a similar ".", "./", ".. /.. /"A relative path relative to the location of the file, so that when the file is moved, it can easily go wrong.
3. Obtain the relative and absolute paths of the current application in Jsp/servlet
3.1 JSP obtains the absolute path of the current application's relative path and absolute path root: The absolute path of the Request.getrequesturi () file: Application.getrealpath ( Request.getrequesturi ());
Absolute path to the current Web application: Application.getrealpath ("/");
Gets the upper-level directory of the request file: New file (Application.getrealpath (Request.getrequesturi ())). GetParent ()
The 3.2 servlet obtains the absolute path of the current application's relative path and absolute path root: Request.getservletpath ();
Absolute path to File: Request.getsession (). Getservletcontext (). Getrealpath (Request.getrequesturi ())
Absolute path of the current Web application: Servletconfig.getservletcontext (). Getrealpath ("/");
(ServletContext objects get several ways: Javax.servlet.http.HttpSession.getServletContext () Javax.servlet.jsp.PageContext.getServletContext () Javax.servlet.ServletConfig.getServletContext ())