首页 > 代码库 > response小结(四)

response小结(四)

   1.发送http头,控制浏览器定时刷新网页(REFRESH)。

 1 package com.yyz.response; 2  3 import java.io.IOException; 4 import javax.servlet.ServletException; 5 import javax.servlet.http.HttpServlet; 6 import javax.servlet.http.HttpServletRequest; 7 import javax.servlet.http.HttpServletResponse; 8 //定时刷新 9 public class ResponseDemo extends HttpServlet {10 11    public void doGet(HttpServletRequest request, HttpServletResponse response)12             throws ServletException, IOException {13         response.setHeader("refresh", "5");14         response.setContentType("text/html;charset=UTF-8");15         response.setCharacterEncoding("UTF-8");16         response.getWriter().write("我是一个中国人!");17 }18     public void doPost(HttpServletRequest request, HttpServletResponse response)19             throws ServletException, IOException {20            doGet(request,response);21     }22 }

 

   2.发送http头,控制浏览器禁止缓存当前文档的内容,通过下面三个消息头均可,但不同的浏览器支持不同的设置方式,所以开发中一般三句都写:

     response.setHeader("Expires", "-1");
     response.setHeader("Cache-Control", "no-cache");
     response.setHeader("Pragma", "no-cache");

response小结(四)