File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Servlets and the fly likes Access counter for servlets Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "Access counter for servlets " Watch "Access counter for servlets " New topic
Author

Access counter for servlets

shivang sarawagi
Ranch Hand

Joined: Jun 19, 2008
Posts: 147
I have a doubt in the code I am having, does this gives a holistic counter of number of times a servlet is accessed. Will the counter variable retain the value even after the application is undeployed ??

If not please anyone tell me how to get the counter variable retain the value of number of times servlet is accessed.

Here is my code




Thanks
Cole Terry
Ranch Hand

Joined: Nov 23, 2011
Posts: 45
shivang sarawagi wrote:I have a doubt in the code I am having, does this gives a holistic counter of number of times a servlet is accessed. Will the counter variable retain the value even after the application is undeployed ??


Absolutely no, unless you persist the variable's value into a file or database.

shivang sarawagi wrote:
If not please anyone tell me how to get the counter variable retain the value of number of times servlet is accessed.


It always retains its value during servlet's life because it is instance variable and there is only one instance of the servlet per web app.
Kumar Raja
Ranch Hand

Joined: Mar 18, 2010
Posts: 518
    
    2

The other thing, which you can use if you really want to keep a count of active sessions at any point of time during the lifecycle of your web app is to add the counter increment/decrement logic in HttpSessionListener..

Is this what you are looking for?

And the values would not be persisted, if the application crashes or the web app is brought down. You need to explicitly persist it.


Regards
KumarRaja

shivang sarawagi
Ranch Hand

Joined: Jun 19, 2008
Posts: 147
Thanks for the reply guys Can anyone tell me how to explicitly persist the value of the counter variable ??

The information which is given at the bottom of webpages about the number of times the page is excessed, in that is the variable explicitly persisted ??
Abhay Agarwal
Ranch Hand

Joined: Feb 29, 2008
Posts: 1075
    
    1

Can anyone tell me how to explicitly persist the value of the counter variable ??


Two ways to persist the Counter value that I know

1. Save counter value to a flat file on disk

OR

2. Save counter value to a Database table


Assuming
a.you have Servlet/JSP application running on any Web container
b. you want to persist value of Counter

Your alogrithm can be

Step 1 -> Treat your counter as Application/ServletContext level variable (rather than any servlet class variable).

Step 2 ->

Implement ServletContextListener interface and override its two methods


Step 3 -> After creating the listener class, you register same in web.xml file as listener.
Sample code is shown below (CustomServletContextListener is the class which implements ServletContextListener )



Step 4 -> When ever you want to increase Counter value, just get current value from Servlet Context and , increase that value and set it back in Servlet Context. In this way, counter will always show current value on your JSP.



Oracle Java Web Service Developer (1z0-897), Oracle certified Java 7 Programmer, SCJA 1.0, SCJP 5.0, SCWCD 5.0, Oracle SQL Fundamentals I
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Access counter for servlets
 
Similar Threads
Counter using Session Tracking !!
Are context-parameters thread-safe?
ContextListener Problem
about threading in servlet.
Please help with a simple GUI.