错误
使用request进行请求转发后无效,并没有跳转页面。
错误代码
@WebServlet("/testServlet")
public class TestServlet extends HttpServlet {
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
request.getRequestDispatcher("/login.sjp");
}
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doPost(req, resp);
}
}
原因
犯了一个细节错误,request.getRequestDispatcher("跳转地址").forward(request,response);应该是这样的,而我的并没有后面的".forward(request,response)",并没有传过去。
解决
使用正确完整的跳转代码request.getRequestDispatcher("跳转地址").forward(request,response);
正确代码
@WebServlet("/testServlet")
public class TestServlet extends HttpServlet {
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
request.getRequestDispatcher("/login.sjp").forward(request,response);
}
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doPost(req, resp);
}
}