package cn.itcast.response;
import java.io.IOException;
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 ResponseDemo extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
//实现的自动跳转技术
String message = "<meta http-equiv='refresh' content='3;url=http://localhost:8080/ServletDemo/index.jsp'>恭喜你,登录成功,奔浏览器将在3秒后,跳到首页,如果没有跳,请点击<a href=''>超链接</a>";
this.getServletContext().setAttribute("message", message);
this.getServletContext().getRequestDispatcher("/MyJsp.jsp").forward(request, response);
}
private void test2(HttpServletResponse response) throws IOException {
// 假设这是一个用于处理登录的Servlet
// 假设程序运行到此,用户登录成功
response.setCharacterEncoding("UTF-8");
response.setContentType("text/html;charset=UTF-8");
response.setHeader("refresh",
"3;url='http://localhost:8080/ServletDemo/index.jsp'");
response.getWriter().write(
"恭喜你,登录成功,奔浏览器将在3秒后,跳到首页,如果没有跳,请点击<a href=''>超链接</a>");
}
private void test1(HttpServletResponse response) throws IOException {
response.setHeader("refresh", "3");
String data = new Random().nextInt(100000) + "";
response.getWriter().write(data);
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
}
}