JSP - SPLessons
SPLessons 5 Steps, 3 Clicks
5 Steps - 3 Clicks

JSP Auto Refresh

JSP Auto Refresh

shape Description

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. [java]public void setIntHeader(String header, int headerValue)[/java] This strategy sends back header "Refresh" to the program alongside a number worth which shows time interim in seconds.

shape Example

Following is an example for JSP Auto Refresh. index.jsp [html]<%@ 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>[/html] Here just imported the packages such as java.io.*, java.text.*, java.util.* . Page will be auto refresh after 1 seconds as follows. [java]response.setIntHeader("Refresh", 1);[/java] To get the Current Time. [java]DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");[/java] The java.util.Calendar.getInstance() method gets a calendar using the specified time zone and specified locale. Declaration will be as follows. [java]public static Calendar getInstance(TimeZone zone,Locale locale)[/java] 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

shape Key Points

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