package am_cn.itcast.response; |
import java.io.IOException; |
import javax.servlet.ServletException; |
import javax.servlet.http.HttpServlet; |
import javax.servlet.http.HttpServletRequest; |
import javax.servlet.http.HttpServletResponse; |
/* |
* 演示 http 请求 常用的 api : setStatus(int sc); |
* |
*/ |
public class ResponseDemo1 extends HttpServlet { |
public void doGet(HttpServletRequest request, HttpServletResponse response) |
throws ServletException, IOException { |
// 设置 响应的状态 码 |
|
// response.setStatus(200); |
|
// 302 + location 响应头 实现 请求重定向 |
//response.setStatus(302 ); |
//response.setHeader("location", "http://www.baidu.com"); // location 表示 定位的 意思 . |
|
// 请求重定向的 特点 : |
// 1. 浏览器 地址栏 发生了 变化 ,指向的 是 重定向后的 地址 |
// 2. 请求重定向 的时候 会 发生 两次 请求 . |
|
//其 执行 过程是这样的 : |
// 在 访问 资源后 , 这个 目标 servlet 里 回送 响应的 过程 中, 会回写一个 302 + location 头, 浏览器 子啊 |
// 收到这个 302+ location 之后, 会自动的发送 第二次请求 去访问 location 头 对应 资源 |
|
// 注意 : 上面的 两行 代码 可以 被 这一行 代码 替代. |
response.sendRedirect( "http://www.itcast.cn" ); // 这个 方法 里 其 底层 是 调用了 |
|
} |
public void doPost(HttpServletRequest request, HttpServletResponse response) |
throws ServletException, IOException { |
doGet(request, response); |
} |
} |