使用Servlet实现自动刷新功能,每一秒钟在浏览器输出一个随机数字。

package chensi.com;

import java.io.IOException;
import java.util.Random; 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 Servlet实现自动刷新 功能(每秒钟在页面 输出一个数字)
*/
@WebServlet("/ResponseDemo9")
public class ResponseDemo9 extends HttpServlet {
private static final long serialVersionUID = 1L; protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//设置一下自动刷新 功能
response.setIntHeader("refresh", 1); //这个是设置1秒自动刷新 Random r = new Random();
System.out.println(r.nextInt(10)+1);
response.getWriter().write(r.nextInt(10)+1+"");
} protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
doGet(request, response);
} }
05-04 04:35