As we all know, "/" indicates the root directory, but it is "/" in Servlet and JSP, But it points to different locations. Now let's assume that my applicationProgramThe name is MyApp. The MyApp directory contains files a. jsp and B. jsp. To switch from servlet to A. jsp, write as follows:
Requestdispatcher = Request. getrequestdispatcher ( " /A. jsp " );
Dispatcher. Forward (request, response );
This can be switched normally, that is, the "/" in the servlet starts under MyApp. If you want to use a link in a. jsp to point to B. jsp using an absolute path, the following statement cannot be used:
< A href = " /B. jsp " > B </ A >
In this way, the prefix of MyApp is lost after clicking the link, indicating that this "/" starts from the root of the server. To solve this problem, in addition to using relative paths, you can add the prefix as follows:
< A href = " <% = Request. getcontextpath () %>/B. jsp " > B </ A >
CodeIt will become ugly. Another case is that when static include is used in the JSP header, "/" contains MyApp. For example, B. jsp can be found in a. jsp:
< % @ Include File = "/B. jsp" % >
The reason for these differences is that after JSP is converted to servlet, It is partially or all out of the context of the application. That is to say, the servlet generated by JSP is part of the system. (This is my guess and I am not responsible for it)
I often mix up these cases myself, and the above is only tested in Tomcat. Other servers may have different results, so it is easy to view them here. Some other cases have not been written down, such as sendredirect In the servlet, and the result of using absolute paths in <JSP: Include>.
In short, I hope to find the most common solution. If you have any experiences, please give your comments.