首先呢,先了解重定向和请求转发的区别。
请求转发
a向b发起请求,b请求转发,给c,获得之后,在发给a。
是在一个请求里面,
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
System.out.println("rs1");
request.getRequestDispatcher("/r/rs2").forward(request,response);
}
重定向
a请求b,b说你去找c把,接着a就去找c。
是两个不同的请求,不在一个url里面。
重定向可以访问web资源以外的资源,而且也不会和原先的servletContext资源共享,和请求转发不一样。
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String username = request.getParameter("username");
String password = request.getParameter("password");
response.sendRedirect("/r/success.jsp");
}