There are many kinds of path jumps for servlets. Let's briefly list them today.
The first is to use hyperlink path to access directly, which is divided into relative path and absolute path.
public class RecServlet extends HttpServlet { protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html"); PrintWriter out = response.getWriter(); out.println("-Post"); System.out.println("Post..."); } protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html;charset = utf-8"); PrintWriter out = response.getWriter(); out.println("-Get"); System.out.println("Get..."); } }
<servlet> <servlet-name>RecServlet</servlet-name> <servlet-class>servlet.RecServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>RecServlet</servlet-name> <url-pattern>/servlet/RecServlet</url-pattern> </servlet-mapping>
This is simple servlet code and configuration file code of web.xml.
The jump codes of relative path and absolute path are as follows. This is a direct url access to the servlet, using doGet().
< a href = "servlet / recservlet" > relative path get link < / a > < br > < br > < a href = "<% = path% > / servlet / recservlet" > absolute path</a>
In addition, form forms can be used to jump
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // request redirections //response.sendRedirect(request.getContextPath() + "test.jsp"); //Adopt server internal jump. Relative path at present //request.getRequestDispatcher("test.jsp").forward(request,response); //This is the absolute path request.getRequestDispatcher("/test.jsp").forward(request,response); } protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doPost(request,response); }
<servlet> <servlet-name>Servlet</servlet-name> <servlet-class>servlet.Servlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>Servlet</servlet-name> <url-pattern>/servlet.do</url-pattern> </servlet-mapping>
The servlet code and configuration code are as above.
The code for the form is as follows.
<form method="post" action="servlet.do"> <input type="submit"> </form>
The jump of form is also divided into relative path and absolute path. If you know the specific configuration of servlet and the relationship between the current page, you should try to use the absolute path as much as possible. Simple pages are basically both OK, but if the relative path links in complex pages jump around easily, it is very difficult to find errors.
If the page to jump is in the project directory and the current page is in the subproject, you can use the following code to find the root directory first and then the link to jump.
request.getRequestDispatcher("../test.jsp").forward(request,response);
If there is any omission, welcome to add. I am also a novice, and I need to work harder.