Auto Refresh Page using Servlet

Auto Refresh Page using Servlet

In this example, we are going to discuss Auto Refresh Page using Servlet. Please read our previous article where we discussed how to display Images using Servlet. The Servlet web page can be automatically refreshed after some time. We can do so by using Servlet setIntHeader() method. We can refresh it in two ways:

  1. Through client-side
  2. And another through server-side

Easiest way of refreshing the servlet web page is use setIntHeader() method of the class javax.servlet.http.HttpServletResponseWrapper.

Syntax: public void setIntHeader(String headerName, int headerValue)

Example: setIntHeader(“refresh”,”10″)

Here, it refreshes the webpage every 10 seconds. This method sends back the header “Refresh to the browser along with an integer value which indicates the time value in seconds.

This example shows how a servlet performs auto page refresh using setIntHeader() method to set the Refresh header. Refresh.java is used to auto-refresh the web page automatically every 5 seconds. web.xml is a deployment descriptor containing information about servlet.

Refresh.java
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class Refresh extends HttpServlet {
 public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  HttpSession session = request.getSession(true);
  response.setIntHeader("Refresh", 5);
  response.setContentType("text/html");
  PrintWriter out = response.getWriter();
  Integer count = new Integer(0);
  String head;
  if (session.isNew()) {
   head = "This is the New Session";
  } else {
   head = "This is the old Session";
   Integer oldcount = (Integer) session.getValue("count");
   if (oldcount != null) {
    count = new Integer(oldcount.intValue() + 1);
   }
  }
  session.putValue("count", count);
  out.println("<HTML><BODY BGCOLOR=#FDF5E6>" + "<H2 ALIGN=CENTER>" + head + "</H2>"
    + "<TABLE BORDER=1 ALIGN=CENTER>" + "<TR BGCOLOR=#FFAD00>" + "  <TH>Information Type<TH>Session Count "
    + "<TR>" + " <TD>Total Session Accesses" + "<TD>" + count + "</TABLE>" + "</BODY></HTML>");
 }
}
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd" id="WebApp_ID" version="4.0">
 <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>  
 
</web-app>
Output

Run your code to get the following output:

Auto Page Refresh using Servlet

After few seconds, the web page will be automatically refreshed and you will get the following output:

Auto Page Refresh using Servlet

In the next article, I am going to discuss the most frequently asked 50 Java Servlet Interview Questions and Answers. Here, in this article, we develop an application to Auto Page Refresh using Servlet and I hope you enjoy How to Auto Refresh a Page using Servlet article.

Leave a Reply

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