Java Web path summary, Java Web path
This article is an article that I have always wanted to write. It mainly summarizes various paths in the web stage. I hope readers will have a clear understanding of paths after reading this article. First of all, I would like to declare that everything in the world is relative. For this reason, we believe that everyone is familiar with the relative movement of objects since junior high school, because this article is for the WEB stage, the following absolute paths and relative paths are for the entire internet.
Client path
It refers to the path running on the browser. Such as forms, hyperlinks, js (location. href), Ajax (url), CSS and JS introduction and redirection. The paths are divided into absolute and relative paths, and the relative paths are also divided into the relative host paths and the paths relative to the current request. It is useless, for example:
Example 1: assume that the path of a.html is http: // localhost: 8080/hello/pages/a.html. The contents of a.html are as follows:
<A href = "http: // localhost: 8080/hello/pages/B .html "> absolute path </a> <a href ="/hello/pages/B .html "> relative to the host path </a> <a href = "B .html"> Request Path </a>
Absolute path (path starting with the Protocol): The final request path is:Http: // localhost: 8080/hello/pages/B .html
Relative Path:
Relative to the host path (starting with "/"): relative to the current host (which can be simply understood as an IP address. If you want to learn more about tomcat, study the Server. xml file. Here is the path of localhost), and the final path of the request is:Http: // localhost: 8080/hello/pages/B .html
Relative to the Request Path (not starting with "/"): relative to the current request (page view request), if it is a direct a.html page, the current request is: http: // localhost: 8080/hello/pages/a.html.The final request path is:Http: // localhost: 8080/hello/pages/B .htmlIf the request http: // localhost: 8080/hello/aServlet is sent to the.html page in the server. The current request is http: // localhost: 8080/hello/aServlet. The final request path is http: // localhost: 8080/hello/B .html.
Example 2: assume that the current request is http: // localhost: 8080/hello/aServlet. The requested Servlet (or Controller) has the following redirection code:
// Absolute path response. sendRedirect ("http: localhost: 8080/hello/page/B .html"); // The relative path response starting. sendRedirect ("/hello/page/B .html"); // The relative path response that does not start. sendRedirect ("B .html ");
The absolute path starts with the Protocol and there is nothing to say.
Relative Path starting with "/" (relative to the current host path): The final request path is: http: // localhost: 8080/hello/pages/B .html
Path not starting with "/" (relative to the current request path): The final request path is: http: // localhost: 8080/hello/B .html
Note:
1. We recommend that you use the path relative to the host. This is irrelevant to the current request.
2. redirection is to set a 302 status code and a location response header on the server. After receiving the request, the browser resends the request according to the location response header. Because the redirected address is the address requested again by the client, the redirection belongs to the client path.
Server Path
The server path refers to the request running on the server, such as request Forwarding (commonly used) and request inclusion. There are two types of server paths: the path relative to the current application and the path relative to the current request.
For example, if the current request path is http: // localhost: 8080/hello/servlet/aServlet, the following code is available in the background:
// Starts with "/" and corresponds to the current application path request. getRequestDispatcher ("/servlet/B .html "). forward (request, response); // It does not start with "/", relative to the current request path request. getRequestDispatcher ("B .html "). forward (request, response );
Relative to the current application path (starting with "/"), the final forwarding address is http: // localhost: 8080/hello/servlet/B .html.
Relative to the current request path (not starting with "/"): The final forwarding address is http: // localhost: 8080/hello/servlet/B .html.
In addition, the underlying layers of all Web frameworks use the basic Filter (Struts) or Servlet (SpringMVC) of the Web. Requests are requests, and responses are response, therefore, the bottom layer of the forwarding or redirection of each Web layer framework uses request and response.
<Url-pattern> path
The path here only refers to the TAG content in the web. xml file. This path is a virtual path and only has a relative path (relative to the current application path). However, the relative path can be used for exact query and fuzzy query.
Example:
<! -The exact query is relative to the current application --> <url-pattern>/servlet/testPath </url-pattern> <! -Fuzzy query indicates that all files or requests under the servlet directory are matched. /* Indicates matching all --> <url-pattern>/servlet/* </url-pattern> <! -Fuzzy query indicates that all files or requests with the do suffix are matched --> <url-pattern> *. do </url-pattern>
In the web. xml file, the wildcard (*) can only be placed at the beginning or end of the path, but cannot be placed in the middle. For example, "/*. do" is invalid.
Note: The order of tomcat search paths: give priority to specific paths, followed by matching directories, and then suffix names. The last is the default path.
Obtain the resource path
There are three main paths for obtaining resources: ServletContext, Class, and ClassLoader. Among them, ServletContext is in the WEB stage, and Tomcat provides a way to obtain resources; Class and ClassLoader mainly obtain resources by means of a method provided by Java API, because this is provided by JDK, therefore, it is not limited to Web, but can also be used in common Java classes. It is mainly used to obtain file streams under the src directory and Its subdirectories.
The path to the resource obtained by ServletContext.
Here, the path for obtaining resources from ServletContext is relative to the absolute path of the system (in Windows, it is a drive letter and can be used to obtain the specific path for uploading or downloading files ).
Basic Syntax: servletContext. getRealPath ("path"); the path in the parameter must be a relative path. It can start with "/" or not start, however, whether "/" is used or not is relative to the current application path. We recommend that you start with "/" (this can be consistent as much as possible ).
In addition, the method for obtaining ServletContext is as follows:
Example: assume that the current tomcat path is D: \ SoftWare \ development \ atat7.0.52 and the project name is hello. The following code is available:
ServletContext servletContext = this.getServletContext();String path1 = servletContext.getRealPath("a.txt");String path2 = servletContext.getRealPath("/a.txt");
Path1 is the same as path2: D: \ SoftWare \ development \ tomcat7.0.52 \ webapps \ hello \ a.txt
The network path is http: // localhost: 8080/hello/a.txt.
Class
Class is used to obtain resources and read content as a configuration file written by itself.
Usage: clazz. getResourceAsStream ("path "). The path in the parameter can start with "/" or not. The representation with "/" is relativeCurrent classPath, which does not start "/"Directory of the current class.
Example: assume that the package structure of the current class is: com. wind-snow.test.path
// Obtain the Class Object Class clazz = this. getClass (); // path with "/" (relative to the current class path) InputStream stream1 = clazz. getResourceAsStream ("/a.txt"); // path without "/" (relative to the path of the current class directory) InputStream stream2 = clazz. getResourceAsStream ("a.txt ");
Stream1stands for the a.txt file under the srcdirectory, and after the release stands for the "/project name/WEB-INF/classes/a.txt" file.
Stream2 indicates the com/wind-snow/test/path/a.txt file under the src directory, the release represents the/project name/WEB-INF/classes/com/wind-snow/test/path/a.txt file.
Note:
1. If not. No exception is reported for this method. Returns null ;.
2. The underlying method uses the getResourceAsStream method of ClassLoader.
ClassLoader
It is similar to getting resources from Class. It's just a different way of writing. It's also used to get file streams.
Usage: classLoader. getResourceAsStream ("path "). The path in the parameter can start with "/" or not with "/" (recommended ). But none of them indicate the path relative to the current class.
Example: Assuming the path of the current class is: com. wind-snow.test.path
ClassLoader classLoader= this.getClass().getClassLoader();InputStream stream1 = classLoader.getResourceAsStream("/a.txt");InputStream stream2 = classLoader.getResourceAsStream("a.txt");
Stream1and stream2both represent a.txt files under the srcdirectory, and after the release, represent the "/project name/WEB-INF/classes/a.txt" file.
Note: if not. No exception is reported for this method. Returns a null value.
Summary:
1. The client carries "/" relative to the current host.
2. The server (including the preceding server, url-pattern path, and ServletContext path) without "/" is relative to the current application (recommended ).
3. The Class with "/" is relative to the current Class path. "/" Is relative to the current location.
4. ClassLoader is the current class path (recommended) No matter it does not contain ).
Attached: The following is a summary of the image, which is drawn using XMind. Original: https://files.cnblogs.com/files/wind-snow/web# .zip