package ResponseDemo;
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 RefreshTest extends HttpServlet
{
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException
{
response.setHeader("refresh","3"); //控制浏览器每隔3秒刷新一次
String data = new Random().nextInt(10000) + "";
response.getWriter().write(data);
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException
{
doGet(request, response);
}
}
其在web.xml中的相关配置为:
<servlet>
<servlet-name>RefreshTest</servlet-name>
<servlet-class>ResponseDemo.RefreshTest</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>RefreshTest</servlet-name>
<url-pattern>/servlet/RefreshTest</url-pattern>
</servlet-mapping>