页面定时跳转
package cn.edu.Response; import java.io.IOException; import java.io.PrintWriter; import java.util.Random; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; public class ResponseDemo5 extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { //假设这是一个处理用户登录的servlet //假设程序运行到此,用户登陆成功了 String message="<meta http-equiv='refresh' content='3;url=/day06/index.jsp'>恭喜你,登陆成功,本页面将在3秒内跳转,若没有跳转,请点击<a href='/day06/index.jsp'>超链接</a>"; this.getServletContext().setAttribute("message",message); this.getServletContext().getRequestDispatcher("/message.jsp").forward(request, response); } private void text2(HttpServletResponse response) throws IOException { //假设这是一个处理用户登录的servlet //假设程序运行到此,用户登陆成功了 response.setCharacterEncoding("UTF-8"); response.setContentType("text/html;charset=UTF-8"); response.setHeader("refresh", "3;url='/day06/index.jsp'"); response.getWriter().write("恭喜你,登陆成功,本页面将在3秒内跳转,若没有跳转,请点击<a href='/day06/index.jsp'>超链接</a>"); } private void text1(HttpServletResponse response) throws IOException { response.setHeader("refresh", "3"); String data = new Random().nextInt(10000) + ""; response.getWriter().write(data); } public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response); } }
控制浏览器缓存
package cn.edu.Response; import java.io.IOException; import java.io.PrintWriter; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; //控制浏览器缓存 public class ResponseDemo6 extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setDateHeader("expires", System.currentTimeMillis()+1000*3600); String data="aaaaaaaaaaaa"; response.getWriter().write(data); } public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request,response); } }
时间: 2024-10-31 08:49:56