How to count number of visitors for website in jsp

How to count the number of visitors to the website in JSP. In this article, we will learn How to count the number of visitors to the website in JSP.

Count the number of visitors to website in JSP

To count the number of visitors on any page in JSP. We need to use of application Implicit object and methods getAttribute() and setAttribute().

  • This object is a representation of the JSP page through its complete lifecycle.
  • This object is created when the JSP page is initialized and it will be removed when the JSP page is removed by the jspDestroy() method.

How to set a variable at the application level?

 application.setAttribute(String Key, Object Value);

How to read the variable set by the previous method?

 application.getAttribute(String Key);

When a user hits the application. You can read the current value by application.getAttribute(String Key); and Increment that value by one like a counter and again set the value for future use.

Below is the complete example of the task on How to count the number of visitors for the website in JSP.

JSP code to count the number of visitors for the website in JSP

Step 1: Create a Dynamic Web Project in Eclipse IDE.

Step 2: Now, we will create the header.jsp file for our application.

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<!-- CSS only -->
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-1BmE4kWBq78iYhFldvKuhfTAU6auU8tT94WrHftjDbrCEXSU1oBoqyl2QvZ6jIW3" crossorigin="anonymous">
<!-- JavaScript Bundle with Popper -->
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/js/bootstrap.bundle.min.js" integrity="sha384-ka7Sk0Gln4gmtz2MlQnikT1wXgYsOg+OMhuP+IlRH9sENBO0LRn5q+8nbTov4+1p" crossorigin="anonymous"></script>
<title>Insert title here</title>
</head>
<body>
<nav class="navbar navbar-expand-lg navbar-dark bg-primary">
  <div class="container-fluid">
    <a class="navbar-brand" href="#">Codebun</a>
    <button class="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-target="#navbarSupportedContent" aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation">
      <span class="navbar-toggler-icon"></span>
    </button>
    <div class="collapse navbar-collapse" id="navbarSupportedContent">
      <ul class="navbar-nav me-auto mb-2 mb-lg-0">
        <li class="nav-item">
          <a class="nav-link active" aria-current="page" href="#">Home</a>
        </li>
        <li class="nav-item">
          <a class="nav-link active" href="#">About Us</a>
        </li>
        <li class="nav-item">
          <a class="nav-link active">Contact Us</a>
        </li>
        <li class="nav-item">
          <a class="nav-link active">Careers</a>
        </li>
      </ul>
    </div>
  </div>
</nav>
</body>
</html>

Step 3: Now, create countvisitor.jsp file as shown below

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
    <%@include file="header.jsp" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<hr>
<div align="center">
<%
Integer counter = (Integer)application.getAttribute("counter");
if (counter ==null || counter == 0)
{
out.println("Welcome to my website!");
counter = 1;
}
else
{
out.println("Welcome back to my website!");
counter++;
}
application.setAttribute("counter", counter);
out.println("Total Number of visitors :  "+counter);
%>
</div>
<hr>
</body>
</html>

Step 4: Now, Run the Application on the Server and see the following output.

Thus, in this way, we can count the number of visitors in a JSP file.