In this section we will learn about the page counter.
JSP Page Count Example
In this section we will learn about the page counter.
Page counter means the number of times the page has visited by the user. Page counter may help you in to keep record of the how many times visitors have visited your website. So, to sort out this problem I have created a JSP page where I have taken an integer variable of which kept the default value 0. Then checked for the first time hit i.e. hit = = 0; and incremented the value by one that increments the visitor number visited the page. How many times you will execute this page its counter will be incremented by one. Here I am giving an example which will demonstrate you about how to keep track of number of times your page/website is visited.
Example :
jspPageCountExample.jsp
<%@ 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"> <title>JSP page count</title> </head> <body> <%! static int hit=0; %> <% if (hit == 0) { %> <h4><% hit ++; out.println("Welcome Visitor"); %> <br> <% out.println("Visitor Number : ");%> <%=hit %>.</h4> <% hit++; } else { %> <h4><% out.print("Welcome Visitor, Visitor Number : "); %> <%=hit %>.</h4> <% hit++; } %> </body> </html>
Output :
1. When you will execute the above JSP page first time you will get the output as follows :
2. When you will execute the above JSP page second time you will get the output as follows :
So, how many times you will execute the above page the number of visitor will be increment by your number of visiting time. But if you will stop the server and again you will execute the above page an increment will be reset.
[ 0 ] Comments