JSP - SPLessons

Chapter 26

JSP Auto Refresh

JSP Auto Refresh

JSP Auto Refresh – Consider a site page which is showing live diversion score or securities exchange status or cash trade proportion. For all such kind of pages, user would need to revive your page routinely utilizing invigorate or reload catch with your browser. JSP makes this occupation simple by giving you a system where you can make a website page in a manner that it would revive consequently after a given interim. The least complex method for reviving a website page is utilizing technique setIntHeader() of response object.

public void setIntHeader(String header, int headerValue)

This strategy sends back header “Refresh” to the program alongside a number worth which shows time interim in seconds.

Following is an example for JSP Auto Refresh.
index.jsp

<%@ page import="java.io.*,java.text.*,java.util.*"%>
<html>

<head>
<title>Auto Refresh Header Example</title>
</head>

<body bgcolor="skyblue">
<center>
<img src=E:/splessons.png></br></br>
Visit <a href="http://www.splessons.com">SPLessons.com</a>
<h2>Auto Refresh Header Example</h2>
<%
// Page will be auto refresh after 1 seconds
response.setIntHeader("Refresh", 1);

// Get Current Time
DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
Calendar cal = Calendar.getInstance();
out.println(dateFormat.format(cal.getTime()));
%></center>
</body>
</html>

Here just imported the packages such as java.io.*, java.text.*, java.util.* . Page will be auto refresh after 1 seconds as follows.

response.setIntHeader("Refresh", 1);

To get the Current Time.

DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");

The java.util.Calendar.getInstance() method gets a calendar using the specified time zone and specified locale. Declaration will be as follows.

public static Calendar getInstance(TimeZone zone,Locale locale)

dateFormat.format(cal.getTime() is used to set the time format.
Output
JSP Auto Refresh – Presently put the above code in index.jsp and attempt to get to it. This would show current framework time after 1 second. Simply run the JSP and hold up to see the outcome as follows.

Summary

  • Refresh is a predefined response header name.
  • response.setIntHeader("Refresh", 1); represent refreshing per 1 second.
  • getinstance() is called factory method.