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.

  • JSP 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 JSP 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.

 

 

JSP program for auto refreshing a web page in every 5 second for limited number of time

Sam Sir

<%-- Program name Refresh.jsp--%>

<%--JSP program for auto refreshing a web page in every 5 second for

limited number of time --%>

<%@page contentType="text/html" pageEncoding="UTF-8"%>

<html>

<head>

<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">

<title>Auto Refresh</title>

</head>

<body>

<%

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");

out.print("<h4>Client Auto Refresh Example</h4><br>");

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

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

%>

</body>

</html>

Output

refresh page

 

 

 

JSP program for auto refresh a web page

Sam Sir

<%--Refreshpage.jsp --%>

<%-- JSP program for auto refresh a web page--%>

<%@page import="java.util.Date" contentType="text/html" pageEncoding="UTF-8"%>

<html>

<head>

<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">

<title>Refresh Page</title>

</head>

<body>

<%

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

%>h3>

JSP page Refresh at <b><%=new Date()%></b>

</body>

</html>

Output

Auto refresh page

Pr.Pg border                                              Next Pg