概述
response的API
代码一【请求重定向】
是一种资源跳转方式,可以在服务器之间进行资源跳转
@WebServlet("/servlet/ResponseDemo2")
public class ResponseDemo2 extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.sendRedirect("https://www.tmooc.cn/");
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doPost(request, response);
}
}
代码二【定时刷新】
@WebServlet("/servlet/ResponseDemo1")
public class ResponseDemo1 extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=utf8");
response.getWriter().write("恭喜注册成功等待3秒钟跳转到首页!!!");
response.setHeader("refresh","3;url=https://www.tmooc.cn/");
}
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doPost(request, response);
}
}
控制不使用缓存【每次拿到的都是新的东西】
@WebServlet("/servlet/ResponseDemo1")
public class ResponseDemo1 extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setDateHeader("Expires",System.currentTimeMillis());
response.setHeader("Cache-control","max-Age=5");
Date date=new Date();
String time = date.toLocaleString();
response.getWriter().write(time);
}
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doPost(request, response);
}
}
|