Servlet实现定时刷新到另外一个页面response.setHeader("refresh", "3;url=/...")

想要实现,访问Responsedemo11的时候,3秒钟后,跳转到ResponseDemo10

用   response.setHeader("refresh", "3;url=/ResponseDemo/ResponseDemo10");   实现。  这个refresh是使用get方式跳转,url中可以携带参数和数据。

 

ResponseDemo11代码:

package chensi.com;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Servlet 实现3秒钟后跳转到另外一个页面 
 */
@WebServlet("/ResponseDemo11")
public class ResponseDemo11 extends HttpServlet {
    private static final long serialVersionUID = 1L;
       
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        response.getWriter().write("进入了demo11,接下来3秒钟后进入demo10");
        response.setHeader("refresh", "3;url=/ResponseDemo/ResponseDemo10");
    }

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
        doGet(request, response);
    }

}

ResponseDemo10(跳转后的页面)的代码:

package chensi.com;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Servlet 这个是用来被跳转的页面
 */
@WebServlet("/ResponseDemo10")
public class ResponseDemo10 extends HttpServlet {
    private static final long serialVersionUID = 1L;
       

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        response.getWriter().write("这个是被跳转后的页面       new page");
    }

    /**
     * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
        doGet(request, response);
    }

}

 

posted @ 2016-06-27 16:51  张昊亮  阅读(10096)  评论(0编辑  收藏  举报