Relative and absolute paths (with examples explain)

Disclaimer: This article is a blogger original article, follow the CC 4.0 BY-SA copyright agreement, reproduced, please attach the original source link and this statement.
This link: https://blog.csdn.net/weixin_44296929/article/details/102543956

First, the path problem:

SpringMVC may be involved in the local routing problem: links, form submission, Redirects and Forwards

<a href=""></a>
<form action=""></form>
response.sendRedirect("")
request.getRequestDispatcher("")

Links, form submission, redirection and how to fill out the appropriate path forward:

(1): What is a relative path? Part Of Origin

Not to "/"the path beginning.
./Represents the current path, namely: action = "./ handler_login.do" and action = "handler_login.do" have said the current path.
../It represents the parent path of the current path.

Relative to the current resource path is located.
Relative paths are relative: HTTP: // host address: port number / project name / resource path /
Note: Due forwarding (url forwarding address change), so the path will change frequently, it will lead us to the path error, it is not recommended to use relative paths in development.

(2): What is the absolute path?

With "/"path beginning.

Absolute path divided into two types:
. A by the parsing browser absolute path, "/" represents the root directory of the server: http: // host address: port number /
(if the server is parsing the absolute path [path back] : the beginning of the slash indicates the current context path of the application).
Note: parsed by the browser only path to the root directory of the server, so we need to add our own project name.

. b by the server parses the absolute path, "/" represents the root directory of the project: http: // host address: port number / project name /
(if the browser is parsing the absolute path [Front Path]: beginning slash represents the root directory of the server).
Note: the path of the server resolved, we do not need to add their own project name. (Because we gave itself the root directory with the project name)

(3): How to write absolute path?

Links, form submission, redirected from the beginning to write the application name, application name after the forward from start writing.

Note: Do not write applications directly in the path name inside, you should use what method to obtain the name of the application in actual deployment: String request.getCOntextPath (); should use absolute paths in the actual development.

Let's explain some definitions above: Click for: forwarding and redirection
1, Q: Why are other paths began to write from the application name, and forwards (without write application name) after the application name from the beginning to write it?
A: Because forward in a single request, a web component is not completed to another web component to continue to fulfill the request process, the process of requesting the forwarding path unchanged forwarding process is resolved by the server itself, it is in the same a web project, so no need to add back the project name (root directory path has been with the project).

2, the forwarding of the resolution process: . Request.getRequestDispatcher ( "/ xxx / yyy") Forward (requst, the Response);
explanation: forwarding is server parses the process and look back path of the resource is within the server to find the backstage path slash "/" at the beginning of representation from the root of the current application to find out.

3, the redirection parsing process: Response.sendRedirect ( "/ xxx.jsp");
Response.sendRedirect (request.getContextPath () + "/ xxx.jsp");
Explanation: Redirection is a typical browser parses process , the redirection is initiated by the client once the request, and the server processing part and tell the client is redirected to another site or project continues to process the request, redirection process is initiated by the client request, the server returns the file loaded by content. Reception path with a slash "/" at the beginning of representation from the root servers to find out.

Two examples:
The following is the directory structure of several jsp:
Here Insert Picture Description
Example 1: Test hyperlink the relative path : <a href=""></a>
adding a hyperlink index.jsp page: use a relative path, click the jump user_info.jsp

<a href="../user/info.do">User_Info.Jsp页面</a>

According to the above file directory structure may know, we are currently resides index.jsp page, where the full access path is: http://localhost:8080/SPRINGMVC-02-USER/WEB-INF/index.jspif we want to use relative paths to jump, after the jump path should be: http://localhost:8080/SPRINGMVC-02-USER/user/info.doso my current directory It is http://localhost:8080/SPRINGMVC-02-USER/WEB-INF/by ../skip parent directory http://localhost:8080/SPRINGMVC-02-USER/and then coupled with user/info.dothat is the correct path to it.

Example 2: Test hyperlink of absolute path : <a href=""></a>
Add two hyperlinks in index.jsp page: test using an absolute path, click the jump user_info.jsp

<a href="/user/info.do">不带项目名称:User_Info.Jsp页面</a><br>
<a href="/SPRINGMVC-02-USER/user/info.do">带项目名称:User_Info.Jsp页面</a>

After deployment tomcat index.jsp page impressions:
Here Insert Picture Description
click on the first later: there have been 404, the path is: http: // localhost: 8080 / user / info.do
Here Insert Picture Description
click on the second: normal jump to the page user_info.jsp
Here Insert Picture Description
result visible, the hyperlink is added to the absolute path of the browser resolution process , if not the name of the project, access to resources is less than we would like to access.

Example 3: Test form submission of a relative path:<form action=""></form>
in login.jsp we add a form to allow users to submit a user name and password:

<form action="./handler_login.do" method="post">
	<fieldset>
		<legend>登录页面</legend>
		用户名:<input name="username"><br>
		密码:<input type="password" name="password"><br>
		<input type="submit" value="确定">
	</fieldset>
</form>

./It represents the current path: action="./handler_login.do"and action="handler_login.do"the same effect.
Here Insert Picture Description
Here Insert Picture Description
Landing page: Enter the path information click submit: http://localhost:8080/SPRINGMVC-02-USER/user/handler_login.do
Here Insert Picture Description
Because the login request is processing path http://localhost:8080/SPRINGMVC-02-USER/user/login.do, then enter login.jsp page, so the current directory is http://localhost:8080/SPRINGMVC-02-USER/user/, so just add in the action handler_login.docan be.

Example 4: Testing form submission to the absolute path:<form action=""></form>
the code to form the absolute path:

<form action="/SPRINGMVC-02-USER/user/handler_login.do" method="post"></form>

提交后的路径为:http://localhost:8080/SPRINGMVC-02-USER/user/handler_login.do,为正确的路径,这种情况下使用相对路径比较好一下,因为是项目内部的处理。
(错误的路径我就不演示了,因为表单提交也是浏览器解析,所以前面不加项目名也会出现404)。

例5: 测试 重定向路径问题:response.sendRedirect("")
先看一下目录结构:
Here Insert Picture Description
a、 我们先使用 相对路径:
我们在拦截器中添加对登录的拦截,拦截器的路径为:http://localhost:8080/SPRINGMVC-02-USER/cn.tedu.spring.interceptor/LoginInterceptor,所以我们如果想重定向到登录页面的话,我们要先使用../进入上级目录也就是根目录,然后在跳转到登录页,所以相对路径应为:response.sendRedirect("../user/login.do");

b、 使用 绝对路径:
因为重定向也是浏览器解析,所以要加上项目名称,即:绝对路径:response.sendRedirect("SPRINGMVC-02-USER/user/login.do");

例6: 测试 转发 路径问题:request.getRequestDispatcher("")
假设我们的拦截器拦截以后使用转发:

a, using a relative path: request.getRequestDispatcher ( "... / User / login.do")
B, using the absolute path: request.getRequestDispatcher ( "/ User / login.do")

In fact, examples are similar, as long as the understanding of the definition above on the line, focusing on ways to resolve those two absolute path.

That's it for all paths related article helpful if you do not forget the point of a like oh!

Guess you like

Origin blog.csdn.net/weixin_44296929/article/details/102543956