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?

零下一度
零下一度Original
2017-07-16 17:06:221713browse

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>

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>

3) '/' in various custom tags.

2. The root path of the Web site

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

1) Hyperlink: c9567b802d727b5fa0738d72ffb351c1To B5db79b134e9f6b82c0b36e0489ee08ed

2) action in the form tag: d4014e4af148c0acceb99a42373bbbca

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!

Statement:
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