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.
<%@ 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>
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.
About the Author: Pranaya Rout
Pranaya Rout has published more than 3,000 articles in his 11-year career. Pranaya Rout has very good experience with Microsoft Technologies, Including C#, VB, ASP.NET MVC, ASP.NET Web API, EF, EF Core, ADO.NET, LINQ, SQL Server, MYSQL, Oracle, ASP.NET Core, Cloud Computing, Microservices, Design Patterns and still learning new technologies.