Index.jsp
<a href="servlet/HelloServlet">servlet/HelloServlet</a><br> <a href="<%=request.getContextPath() %>/servlet/HelloServlet">HelloServlet</a> <a href="servlet/RouteServlet">路由跳转到test.jsp</a>
Servlet.java
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doPost(request,response); } protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { //request.getRequestDispatcher(request.getContextPath()+"/test.jsp").forward(request, response); /* 只要当服务器内部跳转(request), 斜线指项目的根目录 */ // request.getRequestDispatcher("/test.jsp").forward(request, response); /* 相对路径 */ // request.getRequestDispatcher("../test.jsp").forward(request, response); // response.sendRedirect(request.getContextPath()+"/test.jsp"); }