Pr.Pg Next Pg

Auto Page Refresh tutorials

  • Websites over internet places dynamic content such as sports score, stock market or currency exchange that changes frequently. Every time you visit a website, you find the content continuously variable.

  • There are feeds and updates that constantly get updated and you need to reload the page manually every time to reach them.

  • Java Servlet makes this task easy by providing you a mechanism where you can make a webpage in such a way that it would refresh automatically after a given interval.

  • setIntHeader() method of response object provide the simplest way to achieve auto refresh of servlet page, below is signature of setIntHeader() method with parameters.

  • public void setIntHeader(String header, int headerValue);

  • This method sends back header "Refresh" to the browser along with an integer value which indicates time interval in seconds.

 

 

Example: Program of auto refresh Servlet

Sam Sir

//Program name AutoRefreshservlet.java

//Program of auto refresh Servlet

 

import java.io.IOException;

import java.io.PrintWriter;

import java.util.Date;

import javax.servlet.http.HttpServlet;

import javax.servlet.http.HttpServletRequest;

import javax.servlet.http.HttpServletResponse;

import javax.servlet.*;

public class AutoRefreshservlet extends HttpServlet implements Servlet {

 

private static final long serialVersionUID = 1L;

 

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

performTask(request, response);

}

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException,

IOException {

performTask(request, response);

}

private void performTask(HttpServletRequest request, HttpServletResponse response) throws ServletException,

IOException {

response.setContentType("text/html");

response.addHeader("Refresh", "8");

PrintWriter out = response.getWriter();

out.println("Servlet refresh at " + new Date());

}

}

Output

auto refresh page

 

auto refresh page

 

 

Example: Program of Auto refresh Servlet for limited number of time

Sam Sir

//Program name Autorefresh.java

// Program of Auto refresh Servlet for limited number of time

import javax.servlet.ServletException;

import javax.servlet.http.HttpServlet;

import javax.servlet.http.HttpServletRequest;

import javax.servlet.http.HttpServletResponse;

import javax.servlet.http.HttpSession;

import java.io.*;

public class Autorefresh extends HttpServlet {

public void doGet(HttpServletRequest request,

HttpServletResponse response)throws ServletException,IOException {

HttpSession session = request.getSession();

Long times = (Long) session.getAttribute("times");

if (times == null)

session.setAttribute("times", new Long(0));

long value = 1;

if (times != null)

value = (times.longValue()) + 1;

if (value < 20)

response.addHeader("Refresh", "5");

response.setContentType("text/html");

PrintWriter out = response.getWriter();

out.println

("<html><head><title>Client Auto Refresh Example"

+"</title></head><body>");

out.println

("You've visited this page " + value + " times.");

session.setAttribute("times", new Long(value));

out.println("</body></html>");

}

}

Output

auto refresh

 

auto refresh page

Pr.Pg border                                              Next Pg