Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

number of page visited?  RSS feed

 
Kent de Guzman
Greenhorn
Posts: 14
Java MyEclipse IDE Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi guys.I'm new here.
How can I create a Servlet/Jsp that after login counts the number of how many times you visit it.
And If you try to reload it the counter would not be add. Thanks.
 
Raul Cosio
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You could take advantage of the session, because it will no be re-created each time the user reloads (unless cookies are disabled). You can create a Session listener with a class that implements HttpSessionListener and declare it in your web.xml with the <listener> tag, then the class can track when a new session is created.
 
Kent de Guzman
Greenhorn
Posts: 14
Java MyEclipse IDE Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Raul Cosio wrote:You could take advantage of the session, because it will no be re-created each time the user reloads (unless cookies are disabled). You can create a Session listener with a class that implements HttpSessionListener and declare it in your web.xml with the <listener> tag, then the class can track when a new session is created.

Hi! thanks for giving me an idea. Can you give me an exaple how it works? Advance Thanks.
 
Ishan Pandya
Ranch Hand
Posts: 228
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Raul Cosio wrote:You could take advantage of the session, because it will no be re-created each time the user reloads (unless cookies are disabled).


But what if a user log's off from the application?? The session will be destroyed and when he log's on again a new session will be created. How can you store the counter by using just an HttpSession?

@OP: I think that you will also need DB connection to persist the count of the user (on the basis of its user name stored in session or you may just need to fetch the username from request parameter). Or you may use serialization for the same.
 
Kent de Guzman
Greenhorn
Posts: 14
Java MyEclipse IDE Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ishan Pandya wrote:
Raul Cosio wrote:You could take advantage of the session, because it will no be re-created each time the user reloads (unless cookies are disabled).

@OP: I think that you will also need DB connection to persist the count of the user (on the basis of its user name stored in session or you may just need to fetch the username from request parameter). Or you may use serialization for the same.

^Thanks. It works now. I create a Db Connection to store the value of counter.
But what if the user close the page/tab and access it again does the counter will add again?or it just the same if you reload the page?
 
Ishan Pandya
Ranch Hand
Posts: 228
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You should initialize(or modify) the counter variable (according to user) that you store in database through sessionCreated() method of HttpSessionListener (follow as Raul Cosio said) and then store that into DB.
That will handle all your issues of closing or refreshing the page.

1) Store the username and counter in a table in DB.
2) If the user is new then insert into the table with its username and counter variable and if the username exists then retrieve the counter, increment it and store it again in DB.

Got it??
 
Raul Cosio
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello, I see that you have implemented something so the example might not be useful now, anyway:

YourClass.java:

web.xml:


At the end, it depends on why do you want to maintain a counter because there are a lot of variables involved that you can't control: If your count is based on sessions, each time a new session is created, it will be counted; you could look the IP address of the user, but there might be many users behind the same IP and that might not be accurate for you. You could also ask each user to register (the most accurate method) and keep a track of each login, but depending on the site, not everybody is going to register in your webpage. So the best implementation depends on your needs: If you maintain a counter for publicity purposes, then it might be valid to consider each page view as one event, even if it is the same user (like in TV where you see the same ad again and again), or maybe your page handles some kind of voting, and this implies that each user must be unique no matter how many times the page is visited, or you just want to know if your traffic is increasing over time, and the same user counts as one each day or week...
I guess that as you know more about jsp's/servlets/etc. you will change your code according to your needs. Start with the easiest option for you, don't worry if this is not perfect.
 
Kent de Guzman
Greenhorn
Posts: 14
Java MyEclipse IDE Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@Raul Cosio-Thank you very much. It helps me a lot. I used the SessionListener and works perfectly in my needs. :D
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!