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: To B
2) action in the form tag: