Auto Refresh a Web Page In JSP

Instead of refreshing the web page all the time, JSP includes such a code in which refreshing is done automatically in the web page. setIntHeader method of response object (HttpServletResponse) can be used for refreshing the page.

Signature of this method setIntHeader is as shown below:

Public void setIntHeader(String name, int headerValue)

This method set an integer value for their corresponding header name. if the values is already set, then this method overrides the value.

The following example shows us how we have used auto refresh here.

Listing 1: example.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "">
      <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
      <title>Insert title here</title>
   <h2>Hello javabeat</h2>

   <% response.setIntHeader("Refresh", 60);%>
      Todays date:<%= new java.util.Date()%>


Here <% response.setIntHeader(“Refresh”, 60);%> is used to do Auto refresh the program after 60 ms. Execute the example.jsp. Right click on example.jsp and select Run > Run As. Following output would be seen:

Previous Tutorial : How To Debug JSP || Next Tutorial : Upload a File Using JSP



About Krishna Srinivasan

He is Founder and Chief Editor of JavaBeat. He has more than 8+ years of experience on developing Web applications. He writes about Spring, DOJO, JSF, Hibernate and many other emerging technologies in this blog.

  • sunil tariyal

    sir, when i send a form data from servlet and data inserted into data base and servlet dispatch the response back. but when we refresh the page it again send the request for that servlet with form value and data inserted again..
    please give me solution for that.

    • Krishna Srinivasan

      Can you post the sample code you have tried?