Back to: JSP Tutorials for Beginners and Professionals
Auto Page Refresh in JSP Application
In this article, we will learn how to develop Auto Page Refresh Application in JSP. Please read our previous article where we develop Page Hits Counter Application in JSP. Here, we are creating autoRefresh.jsp page. Here we are using setIntHeader() method to set Refresh Header to simulate the current time. This method sends the header “Refresh” back to the browser along with an integer value which indicates a time interval in seconds.
autoRefresh.jsp
<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%> <%@ page import="java.io.*,java.util.*"%> <!DOCTYPE html> <html> <head> <meta charset="ISO-8859-1"> <title>Insert title here</title> </head> <body> <form> <h2>Auto Refresh Example</h2> <% // Set refresh, autoload time as 1 seconds response.setIntHeader("Refresh", 1); // Get current time Calendar calendar = new GregorianCalendar(); String am_pm; int hour = calendar.get(Calendar.HOUR); int minute = calendar.get(Calendar.MINUTE); int second = calendar.get(Calendar.SECOND); if (calendar.get(Calendar.AM_PM) == 0) am_pm = "AM"; else am_pm = "PM"; String CT = hour + ":" + minute + ":" + second + " " + am_pm; out.println("Current Time: " + CT + "\n"); %> </form> </body> </html>
Output
Run your code to get the following output:
After few seconds it auto-refreshes the page and you can observe the difference in the current time.
In the next article, I am going to discuss How to develop Sending Email in JSP Applications. Here, in this article, we develop Auto Page Refresh Application in JSP and I hope you enjoy this Auto Page Refresh Application in JSP article.