The following code example demonstrates how to write a Program to Display Hit Counter in JSP.

Here is an example of a JSP program that displays a hit counter:

<%@ page language="java" contentType="text/html; charset=UTF-8"
<!DOCTYPE html>
<meta charset="UTF-8">
<title>Hit Counter Example</title>
    Integer hits = (Integer) session.getAttribute("hits");
    if (hits == null) {
      hits = new Integer(1);
    } else {
      hits = new Integer(hits.intValue() + 1);
    session.setAttribute("hits", hits);
  <h1>Hit Counter</h1>
  <p>This page has been hit <%= hits %> time(s).</p>

In this example, the JSP page uses a scriptlet to maintain a hit counter in the user’s session. Each time the JSP page is requested, the scriptlet retrieves the current value of the hit counter from the session, increments it, and stores the updated value back in the session. The updated value is then displayed on the page.

Further Reading

Understanding Enterprise Java Beans

Java Practice Exercise