Hi All,
In this post we will discuss how to find/count number of user hits/requests for a jsp page
One simple implementation is setting a variable to application scope and other is using a data base,text file etc.
We will implement this using jsp
Using JSP-
output at subsequent visits:-
For second visit for the page the output will be like below
Welcome to java-recent,this is your re-visit
Total number of visits: 2
The hit count will be increased by one for each request
In this post we will discuss how to find/count number of user hits/requests for a jsp page
One simple implementation is setting a variable to application scope and other is using a data base,text file etc.
We will implement this using jsp
Using JSP-
<%!int
pageHits=0; %>
<%
// setting pageHits as application attribute.
application.setAttribute("pageVisits",
pageHits);
pageHits=(Integer)application.getAttribute("pageVisits");
//For the first visit pageHits value will be zero
if(pageHits==0)
{
out.println("Welcome
to java-recent,this is your first visit");
pageHits=1;
}
else
{
out.println("Welcome
to java-recent,this is your re-visit");
pageHits++;
}
%>
<p>Total
number of visits: <%=
pageHits%></p>
Code
explanation:-
- We have declared pageHits variable in declarative element
- Setting pageHits as application level attribute
- For first visit pageHits variable value will be zero
output at first visit:-
Welcome to java-recent,this is your first visit
Total number of visits: 1output at subsequent visits:-
For second visit for the page the output will be like below
Welcome to java-recent,this is your re-visit
Total number of visits: 2
The hit count will be increased by one for each request
The count will be reset when we re-deploy/restart the application/server.
No comments:
Post a Comment