• Menu
  • Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

JavaBeat

Java Tutorial Blog

  • Java
    • Java 7
    • Java 8
    • Java EE
    • Servlets
  • Spring Framework
    • Spring Tutorials
    • Spring 4 Tutorials
    • Spring Boot
  • JSF Tutorials
  • Most Popular
    • Binary Search Tree Traversal
    • Spring Batch Tutorial
    • AngularJS + Spring MVC
    • Spring Data JPA Tutorial
    • Packaging and Deploying Node.js
  • About Us
  • Java
    • Java 7
    • Java 8
    • Java EE
    • Servlets
  • Spring Framework
    • Spring Tutorials
    • Spring 4 Tutorials
    • Spring Boot
  • JSF Tutorials
  • Most Popular
    • Binary Search Tree Traversal
    • Spring Batch Tutorial
    • AngularJS + Spring MVC
    • Spring Data JPA Tutorial
    • Packaging and Deploying Node.js
  • About Us

How To Refresh Servlet

February 8, 2014 //  by Krishna Srinivasan

  • Java EE Tutorials
  • Servlets Tutorials
  • Servlets Interview Questions

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

Category: Java EETag: Servlets Tutorials

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.

Previous Post: « How To Set PDF File Display In Servlet
Next Post: Request Headers In Servlet »

Primary Sidebar

Follow Us

  • Facebook
  • Pinterest

FEATURED TUTORIALS

How to Initialize an Array in Java

Introduction to Java Server Faces (JSF)

Introduction to Java 6.0 New Features, Part–1

Java 6.0 Features Part – 2 : Pluggable Annotation Processing API

Introduction to Java Server Faces(JSF) HTML Tags

JavaBeat

Copyright © by JavaBeat · All rights reserved
Privacy Policy | Contact