Respon_案例_重定向
案例:
完成重定向
服务器输出字符数据到浏览器
服务器输出字节数据到浏览器
验证码
图解:
案例:
@WebServlet(value = "/Servlet1")
public class Servlet1 extends HttpServlet {
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
this.doPost(request,response);
}
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//方法Servlet1,会自定跳转到responDemo2资源
//设置状态码302
response.setStatus(302);
//设置响应头
response.setHeader("location","Servlet/Servlet2");
}
}
@WebServlet( value = "/Servlet2")
public class Servlet2 extends HttpServlet {
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
this.doPost(request,response);
}
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
System.out.println("dome2....");
}
}
会给跳转到2
Respon_案例1_重定向_特点
重定向的特点:redirect
地址栏发送变化
重定向可以访问其他站点(服务器)的资源
重定向是两次请求,不能使用request对象来共享数据
转发的特点:forward
1,转发地址栏路径不变
2,转发只能访问当前服务器下的资源
3,转发是一次请求
标签:重定向,Respon,request,HttpServletResponse,案例,response From: https://www.cnblogs.com/ssr1/p/16587459.html