Tracking number of JSPs a user visits in a session

Hi everyone:

My coworkers and I need to track the number of pages a user visits while visiting our site.

We set a cookie in the login servlet. If they're not logged in and they visit 5 pages, we want to redirect their request for a 6th JSP to "pleaseLogIn.jsp." Here's the problem:

1 -- We want to avoid the following solution: Set a cookie with an int, retrieve that int on

each page, increment it, and reset the cookie with the incremented int. Then, if the int==5,

redirect. This solution would mean putting (or including) the same code onto each page.

2 -- We can't extend our JSP loader (which, in this case, is BEA Weblogic 5.1's loader --

weblogic.servlet.JSPServlet) to track and redirect, because this servlet is private and unsupported.

Given these 2 limitations, does anyone out there have a clever solution? Thanks a million -- Matt

[913 byte] By [met209] at [2007-9-26 2:01:58]
# 1

I would avoid using the cookie approach and use the session object.

Integer pagesVisited = (Integer) session.getAttribute("pagesVisited");

if (pagesVisited == null)

pagesVisited = new Integer(0);

pagesVisited = new Integer(pagesVisited.intValue());

if (pagesVisited.intValue() > 5)

response.sendRedirect("login.jsp");

Of course, I would put this into an included page and include it in every page.

In the login.jsp page, you probably want to reset the counter.

jpardi at 2007-6-29 8:42:52 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...