728x90 AdSpace

Sunday, 6 April 2014

Servlet – Auto Page Refresh

In this tutorial we are going to see how to create auto page refresh program using servlet.
Servlet provides way to refresh the page using the method setIntHeader(String header, int value); of response object.
public void setIntHeader("Refresh", 10);

The method send back header Refresh along with integer value which indicate the time interval to refresh the page.

1. Create new Dynamic web project by choosing File –> New –> Dynamic Web Project .

2. Create the Project called ServletExample as given below.

Servlet-dynamic web project

3. Create package called com.javatutorialscorner.servlet under ServletExample.

4. Create Servlet called AutoRefreshServlet as shown in figure.

Create Servlet

http servlet 
5. Click Next it will show URL mapping.You can edit Servlet URL if you need.

6. Click Next it will show methods available  in HttpServlet. Select appropriate method you need.

http servlet Services

7. By default your servlet will be mapped in web.xml, if your servlet not mapped in your web.xml use the following configuration to map the servlet in your web.xml

Servlet Mapping

    <servlet-name>Your Servlet Name</servlet-name>
    <servlet-class>Fully Qulaified Servlet Class Name</servlet-class>
    <servlet-name>Your Servlet Name</servlet-name>
    <url-pattern>/URL to Call Servlet</url-pattern>


<?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">


8. Add the required code inside doGet() method.


package com.javatutorialscorner.servlet;

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;

 * Servlet implementation class AutoRefreshServlet

public class AutoRefreshServlet extends HttpServlet {
 private static final long serialVersionUID = 1L;

  * @see HttpServlet#HttpServlet()
 public AutoRefreshServlet() {
  // TODO Auto-generated constructor stub

  * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
  *      response)
 protected void doGet(HttpServletRequest request,
   HttpServletResponse response) throws ServletException, IOException {
  // TODO Auto-generated method stub
  response.setIntHeader("Refresh", 10);
  PrintWriter writer = response.getWriter();
  Date date = new Date();
  writer.write("<!DOCTYPE html PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\" \"http://www.w3.org/TR/html4/loose.dtd\">\n"
    + "<html><head><meta http-equiv=\"Content-Type\" content=\"text/html; charset=ISO-8859-1\"><title>Java Tutorials Corner - Servlet - Auto Page Refresher</title>"
    + "</head><body ><h1>Java Tutorials Corner - Servlet Auto Page Refresher</h1><table><tr>"
    + "<td>Current Date & Time  : </td><td>"
    + date
    + "</td></tr></table></body></html>");

  * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
  *      response)
 protected void doPost(HttpServletRequest request,
   HttpServletResponse response) throws ServletException, IOException {
  // TODO Auto-generated method stub


9. Now save and Run the servlet, Run As –> Run on Server – Select your web Server to run the servlet. ( see How to configure tomcat in eclipse) 

10.call the URL which is mapped in web.xml.



Annamalai Thangaraj

Annamalai is working as Technical Lead in Leading Telecom company with 5+ years experience in Identity and Access Management , Telecom and Networks, BigData, Java, Spring, Struts, Hibernate, AngularJS, and Enterprise Web Application Development.

Website: Java Tutorials Corner

Shop and help us

Flipkart Offer Snapdeal offer Amazon.in offer Amazon.com offer
  • Blogger Comments
  • Facebook Comments
  • Disqus Comments


Post a Comment

Item Reviewed: Servlet – Auto Page Refresh Rating: 5 Reviewed By: Annamalai Thangaraj