Absolute path, relative path, virtual path and resource path

Generally during the jump and redirect resources that they will use the resources of the path, the path is divided into relative and absolute paths, recorded about the characteristics and usage of these two paths


A relative path

Resources can not be determined uniquely relative paths
example: ./ index.html
Relative path usage rules
To start with "./" ,
when used to determine the relative position relationship between resources and the resources of the current target
if the two resources located in the same directory level : ./+ target resource name
./ can not add, there ./ default
if the target resources located in the parent directory : ../+ target resource name
How to determine the relationship between the current source and target resources
For resources under src directory , you can use request.getRequestURL () to get the location of the resource
URI java resources under src are virtual path + name of the resource regardless of the level at which the java file is located under the src directory, as long as src directory, all the forms.
Here that is not the actual resource names of java class name, but @WebServlet resources in the name server to find the actual class by the resource name.
For resources under the web directory , if directly in the web directory, then the URI of the resource is the virtual path + / file name if other directory in the web directory, then the URI of the resource is the virtual path + / directory name + / file name , there are several stages to write a few directory level directory.

Second, the absolute path

The only resource is determined by the absolute path
for example: http: // localhost / response /or / response / responseDemo2
protocol name + ip address port number + + + virtual path to resource file

Third, the two routes using rules

Rule: path is determined to be defined to whom used, i.e., if the request issued in the future from which
To the client browser to use
Need to add virtual directory (access path of the project, view the project xml configuration file) For example: <\ a> tag, <\ form>, redirection sent from the browser, you need to add virtual path
To use server
Do not need to add virtual directories such as: resource request is forwarded to jump inside the server, do not need to add virtual path, write directly to the resource name on it

Fourth, the virtual path

Virtual path represents the location of the actual deployment of the project, the server can be mapped to the location of the actual deployment of the project through the xml file to view the project can be found via the virtual path:
<\ Context path = "/ the Response" docBase = "F: \ IDEA \ Tomcat_and_Servlet \ out \ artifacts \ response_war_exploded "/ > this is the configuration file content, we entered in the browser is Contextpath, which is the virtual directory of the project, the server directory mapped to the real behind the project deployment through the virtual directory
The benefits of a virtual directory
  • Physical directory name is usually the name of the virtual directory than easy to remember and therefore more user-friendly access.
  • Virtual Directory can enhance security because the client does not know the actual physical location of the file on the server, you can not use this information to modify the target file server.
  • Use the virtual directory makes it easier to move the site in the directory, you change the mapping between the physical location of the virtual directory, without changing the URL of the directory.
  • Use the virtual directory can publish content in multiple directories, and you can control access to each virtual directory individually. Using Virtual Directories can load balance Web server because the site resources from multiple different servers to avoid overloading a single server, the response is slow.

Fifth, the resource path

Resource path is not the true path to the file, there is really a mapping relationship between it and the path, the server can find files by path resource
[link] (https://blog.csdn.net/weixin_42387411/article/details/100191979)
which articles mention how to configure the Servlet, one is configured through the web.xml, in this way it is easy to understand the relationship between resource mapping the path and file path, file server mapped to the actual path through resources, by the same principle @WebServlet configuration
发布了28 篇原创文章 · 获赞 1 · 访问量 2123

Guess you like

Origin blog.csdn.net/weixin_42387411/article/details/100584642