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

package javabeat.net.servlets;

import java.io.IOException;
import java.io.PrintWriter;
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.setContentType("text/plain");

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

		PrintWriter out = response.getWriter();

		Date d = new Date();

		out.println(d.toString());
	}
}

Web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
	xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
	id="WebApp_ID" version="3.0">

	<servlet>
	     <servlet-name>RefreshServletExample</servlet-name>
	     <servlet-class>javabeat.net.servlets.RefreshServletExample</servlet-class>
	</servlet>
        <servlet-mapping>
		<servlet-name>RefreshServletExample</servlet-name>
		<url-pattern>/RefreshServletExample</url-pattern>
	</servlet-mapping>
</web-app>

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

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.

Speak Your Mind

*