Home > Java > javaTutorial > Explanation of the path problem after Servlet jumps to JSP page

Explanation of the path problem after Servlet jumps to JSP page

巴扎黑
Release: 2017-07-17 13:49:26
Original
2329 people have browsed it

1. Phenomenon and Concept

1. Problem

When the Servlet is forwarded to the JSP page, the path of the Servlet is displayed on the browser address bar. , and if the hyperlink of the JSP page is still relative to the address of the JSP page and the Servlet and the JSP page are not in the same folder, there will be a path confusion problem.

2. Absolute path concept

The path relative to contextPath (the context of the current Web application).

Solution: Use absolute paths instead of relative paths for hyperlinks. If / represents the root directory of the site, add contextPath in front of it.

<a href="<%= request.getContextPath() %>/TestServlet">To B</a>
Copy after login

2. Summary of the meaning of '/' in JavaWeb development

1. The root path of the current Web application

'/' represents http://localhost:8080/contextPath/ : the address processed by the Servlet

1) Forwarding: request.getRequestDispatcher("/path /b.jsp").forword(request, response);

2) Mapping Servlet access path in web.xml file

<servlet-mapping><servlet-name>TestServlet</servlet-name><url-pattern>/TestServlet</url-pattern></servlet-mapping>
Copy after login

3) Various custom tags middle'/'.

2. The root path of the Web site

'/' represents http://localhost:8080/: the address processed by the browser

1) Hyperlink: /TestServlet">To B

2) Action in form tag:

/b.jsp">

3) Redirect: response.sendRedirect("/b.jsp");


3. Issues with site root directory and css path (jsp is a server-side program, the address changes, and the relative path to the site root directory is generally used when referencing)
We call a relative path like this /test/.... a relative path relative to the site root directory.
When introducing css into a jsp, if its relative path is relative to the current jsp file, and you forward the jsp in a servlet that is different from the path of the jsp, you will find that the css style does not work at all. . This is because the css path when forwarding in the servlet is the relative path to the servlet instead of the jsp path. So you cannot use such a path in jsp at this time: or HTML relative paths similar to href="one.css" and ../../one.css are relative to the file that references this css (a. jsp) relative path. When forwarding in a servlet, it is a relative path to the servlet. Because the jsp path and the servlet path are different, such a reference must be wrong.
So at this time, you need to use the site root directory, which is the directory relative to http://192.168.0.1/ and starts with "/".
Therefore, the above error should be corrected to a directory relative to the site root directory similar to href="/test/one.css". In this way, after the servlet is forwarded and in the jsp, the relative path is relative to the site root directory, and the defined css style can be used correctly.


Page jump problem:

Forward is high, Redirect is low, because the Redirect process is like this , request1 sent to server, server return back to client, and then

request2 then sent to server. But Forward is only processed on the server side and is transparent to the client side. Since Redirect has two transmissions, it is efficient Low.

Scope:
Because for request.setAttribute(), the survival range of the object it carries is only within the request, so the Redirect method will cause the object carried by the request to be lost.


The above is the detailed content of Explanation of the path problem after Servlet jumps to JSP page. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template