Servlets - Auto Page Refresh


Consider a site page which is showing live amusement score or securities exchange status or cash trade proportion. For all such kind of pages, you would need to revive your site page routinely utilizing invigorate or reload catch with your browser.

Java Servlet makes this activity simple by giving you a component where you can make a site page so that it would invigorate naturally after a given interval.

The easiest method for reviving a site page is utilizing strategy setIntHeader() of reaction object. Following is the mark of this strategy −

public void setIntHeader(String header, int headerValue)

This technique sends back header "Revive" to the program alongside a whole number esteem which demonstrates time interim in seconds.

Auto Page Refresh Example

This model shows how a servlet performs auto page invigorate utilizing setIntHeader() technique to set Refresh header.

//Import required java libraries 

import java.io.*; 

import javax.servlet.*; 

import javax.servlet.http.*; 

import java.util.*; 

//Extend HttpServlet class 

public class Refresh extends HttpServlet { 

//Method to deal with GET strategy request. 

public void doGet(HttpServletRequest request, HttpServletResponse response) 

throws ServletException, IOException { 

//Set invigorate, autoload time as 5 seconds
    response.setIntHeader("Refresh", 5); 

//Set reaction content type 

response.setContentType("text/html"); 

//Get present time 

Calendar timetable = new GregorianCalendar(); 

String am_pm; 

int hour = calendar.get(Calendar.HOUR); 

int minute = calendar.get(Calendar.MINUTE); 

int second = calendar.get(Calendar.SECOND); 

if(calendar.get(Calendar.AM_PM) == 0) 

am_pm = "AM"; 

else 

am_pm = "PM"; 

String CT = hour+":"+ minute +":"+ second +" "+ am_pm; 

PrintWriter out = response.getWriter(); 

String title = "Auto Page Refresh utilizing Servlet"; 

String docType = 

"<!doctype html open \"-/w3c//dtd html 4.0 " + "transitional//en\">\n"; 

out.println(docType + 

"<html>\n" + 

"<head><title>" + title + "</title></head>\n"+ 

"<body bgcolor = \"#f0f0f0\">\n" + 

"<h1 adjust = \"center\">" + title + "</h1>\n" + 

"<p>Current Time is: " + CT + "</p>\n" 

); 

} 

//Method to deal with POST technique request.
    
    public void doPost(HttpServletRequest request, HttpServletResponse response) 

throws ServletException, IOException { 

doGet(request, response); 

} 

}

Now let us assemble the above servlet and make the accompanying sections in web.xml

.... 

<servlet> 

<servlet-name>Refresh</servlet-name> 

<servlet-class>Refresh</servlet-class> 

</servlet> 

<servlet-mapping> 

<servlet-name>Refresh</servlet-name> 

<url-pattern>/Refresh</url-pattern> 

</servlet-mapping> 

.... 

Now call this servlet utilizing URL http://localhost:8080/Refresh which would show current framework time after like clockwork as pursues. Simply run the servlet and hold on to see the outcome −

Auto Page Refresh utilizing Servlet

Current Time is: 9:44:50 PM







© Javacodegeeks 2018 -
All Right Reserved and you agree to have read and accepted our term and condition.

Python 3