Home > Java > javaTutorial > What should I do if there is a path problem when the Servlet forwards to the JSP page?

What should I do if there is a path problem when the Servlet forwards to the JSP page?

零下一度
Release: 2017-07-16 17:06:22
Original
1712 people have browsed it

ServletWhat should I do if there is a path problem when forwarding to the JSP page? Let's take a look at the solution

1. Phenomenon and concept

1. Problem

When the Servlet forwards to the JSP page, the browser address bar What is displayed is the path of the Servlet, but 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 localhost:8080/contextPath/: by Address processed by Servlet

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

2) Mapping in web.xml file Servlet access path

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

3) '/' in various custom tags.

2. The root path of the Web site

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

1) Hyperlink: To B

2) action in the form tag:

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

The above is the detailed content of What should I do if there is a path problem when the Servlet forwards to the 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