目录
一:请求转发
forward() 当客户端向服务器端发送一个请求,请求地址没有发生变化,服务器调用内部的方法直接将请求转发给新地址进行处理,然后将处理结果反馈给客户端。采用请求转发的方式,客户端和服务器端只执行了一次请求和一次响应,且浏览器地址栏显示的是第一次访问的原地址 (request对象不变)”。
请求转发特点:
- 请求转发地址栏不发生改变
- 用户一次请求
- 请求转发过程中参数不会丢失
- 请求转发无法跳转到服务器以外的资源
- 请求转发可以跳转WEB-INF目录内的站点
- 访问成功返回状态码200
图示:
二: 网页重定向
sendRedirect() 重定向是指页面定位到某个新地址,之前的request失效,进入一个新的request (相当于两次请求),通过request对象的sendRedirect()方法实现。
网页重定向特点:
- 重定向地址栏发生改变
- 重定向需要进行二次跳转
- 重定向过程中参数会丢失
- 重定向可以跳转到服务器以外的资源
- 重定向无法跳转WEB-INF目录内的站点
- 重定向一次跳转后返回302码及location
图示:
三:请求跳转示例
先创建三个 servlet类(参数正常传递,URL地址并没有跳转)
@WebServlet(name = "Forward01", value = "/Forward01")
public class Forward01 extends HttpServlet {
private static final long serialVersionUID = -2245044846253497804L;
@Override
protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
System.out.println("我是Forward01");
// 1.向request域中存储数据 与request绑定
request.setAttribute("slogan","桃李不言下自成蹊");
//forward(request,response) 中的request,response 是转发对象
//2.请求转发
request.getRequestDispatcher("/Forward02").forward(request,response);
}
}
@WebServlet(name = "Forward02", value = "/Forward02")
public class Forward02 extends HttpServlet {
private static final long serialVersionUID = -1262209307277647624L;
@Override
protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
System.out.println("我是Forward02");
//forward(request,response) 中的request,response 是转发对象
request.getRequestDispatcher("/Forward03").forward(request,response);
}
}
@WebServlet(name = "Forward03", value = "/Forward03")
public class Forward03 extends HttpServlet {
private static final long serialVersionUID = 3896982895610295687L;
@Override
protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
System.out.println("我是Forward03");
String slogan = (String) request.getAttribute("slogan");
System.out.println(slogan);
}
}
请求转发无法跳转到服务器以外的资源
@WebServlet(name = "ForwardOuter", value = "/ForwardOuter")
public class ForwardOuter extends HttpServlet {
private static final long serialVersionUID = 5856096467944349435L;
@Override
protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
System.out.println("我是 ForwardOuter");
// 请求无法跳转到站外站点
request.getRequestDispatcher("https://space.bilibili.com/480308139").forward(request, response);
}
}
请求转发可以跳转WEB-INF目录内的站点
@WebServlet(name = "ForwardInner03", value = "/ForwardInner03")
public class ForwardInner03 extends HttpServlet {
private static final long serialVersionUID = -5730328858279765970L;
@Override
protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
System.out.println("我是 ForwardInner03");
request.getRequestDispatcher("/WEB-INF/forward.jsp").forward(request, response);
}
}
四:重定向跳转示例
先创建三个 servlet类(参数会丢失,URL地址发生了跳转)
@WebServlet(name = "Redirect01", value = "/Redirect01")
public class Redirect01 extends HttpServlet {
private static final long serialVersionUID = -5096088127501474424L;
@Override
protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
System.out.println("我是 Redirect01");
request.setAttribute("slogan","桃李不言下自成蹊");
// 网页重定向
response.sendRedirect(request.getContextPath() + "/Redirect02");
}
}
@WebServlet(name = "Redirect02", value = "/Redirect02")
public class Redirect02 extends HttpServlet {
private static final long serialVersionUID = -1046620871038602664L;
@Override
protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
System.out.println("我是 Redirect02");
// 网页重定向
response.sendRedirect(request.getContextPath() + "/Redirect03");}
}
@WebServlet(name = "Redirect03", value = "/Redirect03")
public class Redirect03 extends HttpServlet {
private static final long serialVersionUID = -1514803687180205194L;
@Override
protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String slogan = (String) request.getAttribute("slogan");
System.out.println("slogan >>> " + slogan);
System.out.println("我是 Redirect03");
}
}
请求转发可以跳转到服务器以外的资源
@WebServlet(name = "RedirectOuter", value = "/RedirectOuter")
public class RedirectOuter extends HttpServlet {
private static final long serialVersionUID = 6941511209459670904L;
@Override
protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
System.out.println("我是 RedirectOuter");
// 网页重定向
response.sendRedirect("https://blog.csdn.net/qq_24330181");
}
}
请求转发不可以可以跳转WEB-INF目录内的站点
@WebServlet(name = "RedirectInner03", value = "/RedirectInner03")
public class RedirectInner03 extends HttpServlet {
private static final long serialVersionUID = -7795455934058880876L;
@Override
protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
System.out.println("我是 RedirectInner03");
// 网页重定向
response.sendRedirect(request.getContextPath() + "/WEB-INF/redirect.jsp");
}
}
状态码会变为404