Table of contents
1.
Introduction
2.
How does it work?
3.
Working Example
4.
Frequently Asked Questions
5.
Key Takeaways
Last Updated: Mar 27, 2024

JSP Auto Refresh

Author Abhay Trivedi
0 upvote
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

Suppose a web page displays the live game score, stock market status, or currency exchange ratio. You would need to refresh the Web page regularly with your browser for all such pages. Luckily we have JSP Auto Refresh for that.

JSP or Java Server Pages is a Web-based server-side technology used for creating web applications & dynamic web content. JSP Auto Refresh makes this job easy by providing you with a mechanism to create a web page to refresh automatically in fixed intervals.

How does it work?

The easiest way of refreshing a Webpage in JSP is by using the setIntHeader() method of the response object.

public void setIntHeader(String header, int value)
You can also try this code with Online Java Compiler
Run Code

Working Example

To better understand the use of the setIntHeader() method to set Refresh header, Let's simulate a digital clock with the given example.

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

<html>
   <head>
      <title>JSP Auto Refresh Example</title>
   </head>
   
   <body>
      <center>
         <h2>JSP Auto Refresh Example</h2>
         <%
            // Set refresh with autoload time 1 seconds
            response.setIntHeader("Refresh", 1);
            
            // Get current time
            Calendar calendar = new GregorianCalendar();
            
            int hour = calendar.get(Calendar.HOUR);
            int min = calendar.get(Calendar.MINUTE);
            int sec = calendar.get(Calendar.SECOND);
            
            String meridiem;
            if(calendar.get(Calendar.AM_PM) == 0)
               meridiem = "AM";
            else
               meridiem = "PM";
            String time = hour+":"+ min +":"+ sec +" "+ meridiem;
            out.println("Crrent Time: " + time + "\n");
         %>
      </center>
   
   </body>
</html>

Output:

In this example, we set autoload time to 1 second such that the webpage will auto-reload in every second, giving a sensation of the clock ticking. Next, we use GregorianCalendar() Class to create a calendar class object. With the help of the calendar object, we use the get() method to get the current time. After this, we make a string of present time and print it on the screen.


You can also read about the JSP Architecture and Lifecycle.

Frequently Asked Questions

1. What is JSP?

JSP or Java Server Pages is a Web-based server-side technology used for creating web applications & dynamic web content. We use JSP tags to insert JAVA code into HTML pages. JSP is an advanced version of Servlet Technology that helps create dynamic and platform-independent web pages by inserting Java code in HTML/ XML pages or both. The JSP container first converts JSP into a servlet and processes the client’s request.

2. What is the difference between JSP and HTML?

JSP is a server-side technology that we use to create dynamic web applications. HyperText markup language (HTML) is used to create the structure of web pages. JSP allows placing the custom tag or third-party tag. It does not permit the placement of the custom tag or third-party tag.

3. What is JSP Auto Refresh?

JSP Auto Refresh is a feature of JSP which makes the job of constantly refreshing the website easy by providing you with a mechanism to create a web page to refresh automatically in fixed intervals.

4. What is setIntHeader() method?

HttpServletResponse interface provides a method setIntHeader() to set an integer value to the response header. setIntHeader method sets the integer value to the header and overwrites the new value if the value of the specified header exits.

5. Is JSP secure?

JavaServer Pages or JSP and servlets make several mechanisms available to Web developers to secure their applications. Resources are protected declaratively by recognizing them in the application deployment descriptor and allocating their role.

Key Takeaways

This article teaches about JSP Auto Refresh and how we use them. We saw why JSP Auto Refresh could be beneficial for a developer to learn. Click here to read about Java Interview Questions.

Click here to see other related blogs on JAVA.

Related Articles:

What is Servlet

Why is Java Platform Independent 

Check out our Web development course and blogs on Backend Web Technologies.

If you are preparing for your DSA interviews then, Coding Ninjas Studio is a one-stop destination. This platform will help you acquire effective coding techniques and overview student interview experience in various product-based companies.

 

Happy Learning!

Live masterclass