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" "http://www.w3.org/TR/html4/loose.dtd">
<html>
   <head>
      <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
      <title>Insert title here</title>
   </head>
   <body>
      <center>
   <h2>Hello javabeat</h2>

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

   </center>
   </body>
</html>

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:
jsp_autorefresh_demo

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

Comments

comments

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.

Comments

  1. sunil tariyal says:

    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.

Speak Your Mind

*