JavaBeat

  • Home
  • 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
    • Join Us (JBC)
  • Privacy

How To Refresh Servlet

February 8, 2014 by Krishna Srinivasan Leave a Comment

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

[code lang=”java”]
public void setIntHeader(String headerName, int headerValue)
[/code]

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

[code lang=”java”]
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());
}
}
[/code]

Web.xml

[code lang=”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>
[/code]

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

Filed Under: Java EE Tagged With: 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.

Leave a Reply Cancel reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Follow Us

  • Facebook
  • Pinterest

As a participant in the Amazon Services LLC Associates Program, this site may earn from qualifying purchases. We may also earn commissions on purchases from other retail websites.

JavaBeat

FEATURED TUTORIALS

Answered: Using Java to Convert Int to String

What is new in Java 6.0 Collections API?

The Java 6.0 Compiler API

Copyright © by JavaBeat · All rights reserved