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

Leave a Reply

Your email address will not be published. Required fields are marked *

Pin It on Pinterest

Share This

Share this post with your friends!