How To Refresh Servlet

We can refresh the server in two ways

  • Through client side
  • and another through server side.

Consider the webpage containing the live score of the match or any live activities we want to see in web page in such cases we have to either refresh the web page or browse to know the current status. Servlet can help to get the solution for this problem. We can make a webpage in such a way that the webpage gets refreshed automatically after a given interval.

Refreshing A Servlet

Easiest way of refreshing the web is we can use the method setIntHeader() of the class javax.servlet.http.HttpServletResponseWrapper.
Syntax of this method is :

public void setIntHeader(String headerName, int headerValue)

For example setIntHeader(“refresh”, “10”) refreshes the web page every 10 second. This method sends back header “Refresh” to the browser along with an integer value. This indicates time interval in seconds.

Example Of Refreshing A Servlet

Listing 1: RefreshServletExample


import java.util.Date;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class RefreshServletExample extends HttpServlet {
	public void doGet(HttpServletRequest request, HttpServletResponse response)

	throws ServletException, IOException {


		response.setHeader("Refresh", "3");

		PrintWriter out = response.getWriter();

		Date d = new Date();



<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="" xmlns="" xmlns:web=""
	id="WebApp_ID" version="3.0">


Execute the RefreshServletExample and you will see that the page gets refershed every 3 second.

Advantages Of Refreshing Servlet

  1. Refreshing servlet may helps to know the current status of the web page For Example live cricket score.
  2. Refreshing servlet help to update the current web server or in the same tab we can change the web page by assigning the url

Previous Tutorial : How To Set PDF File Display In Servlet  || Next Tutorial : Request Headers In Servlet

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!