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

Request Variable

Mike London
Ranch Hand

Joined: Jul 12, 2002
Posts: 1054
Is there a simple way to know how many times a JSP page has been called/loaded?
My goal is to conditionally execute code depending if the user has submitted that JSP page and it reloads.
I've looked for things like "Request.pageCount()", but it doesn't seem to be quite that easy.
Thanks for anybody's help or advice.
-- Mike
Rahul Mahindrakar
Ranch Hand

Joined: Jul 28, 2000
Posts: 1850
write a jspinit() method and put your conditional code there.
Here is the extract from the jsp specification
When the first request is delivered to a JSP page, a jspInit() method, if present,
will be called to prepare the page. Similarly, a JSP container may invoke a JSP’s
jspDestroy() method to reclaim the resources used by the JSP page at any time
when a request is not being serviced. This is the same life-cycle as for servlets.
[ January 01, 2003: Message edited by: Rahul Mahindrakar ]
Mike London
Ranch Hand

Joined: Jul 12, 2002
Posts: 1054
Cool, I remember reading about this a while back.
Thanks much for your reply.
-- Mike
Marty Hall
Author
Ranch Hand

Joined: Jan 02, 2003
Posts: 111
Is there a simple way to know how many times a JSP page has been called/loaded?

How about this:

The first line
creates a field (instance variable) called count, and fields only gets initialized when "new" gets called. But, JSP pages are really servlets behind the scenes, and only one instance of a servlet gets built for each URL.
Now, this simple approach has several limitations:
  • The count restarts when the server is rebooted.
  • It will not work in distributed Web applications, since there will be one servlet instance per JVM.
  • It technically suffers from race conditions. In principle, it is possible in very rare situations for two consecutive users to see the same hit count. I doubt that matters for a hit count, but if you were generating user IDs or something that had to be unique, you would want to synchronize.



  • I've looked for things like "Request.pageCount()", but it doesn't seem to be quite that easy.

    Hmm. What you said at the top made it sound like you wanted to know how many times anybody had accessed the page. But this makes it sound like you want to know how many times each user has accessed the page (ie separate counts for each user). If the latter is your goal, then store the count in the session object. (You have to store an Integer, not an int, though.)
    BTW, I saw a reply talking about jspInit. I confess I don't see what that has to do with keeping track of the number of requests, since jspInit only runs once, not for each request. (Although, if you periodically wrote the count to disk, you could use jspInit to restart the count next time where you left off this time).
    Cheers-
    - Marty
    Rahul Mahindrakar
    Ranch Hand

    Joined: Jul 28, 2000
    Posts: 1850

    BTW, I saw a reply talking about jspInit. I confess I don't see what that has to do with keeping track of the number of requests, since jspInit only runs once, not for each request

    Is there a simple way to know how many times a JSP page has been called/loaded?
    My goal is to conditionally execute code depending if the user has submitted that JSP page and it reloads.

    Hi Marty,
    Actually Mike wanted to know how many times a servlet jsp page is Loaded and thus the answer to write code that is static in nature in the jspInit() method call. It can be implemented like this
    <%!
    private static int count=0;
    public void jspInit(){
    count=count+1;
    }
    %>
    I slightly attribute a different meaning to loaded and called.
    I would say "called" is request based and the solution is provided by you.
    However when you talk of jsp "loaded", I think that it means going through the jsp life cycle.My solution is apt for this.
    [ January 05, 2003: Message edited by: Rahul Mahindrakar ]
     
    I agree. Here's the link: http://aspose.com/file-tools
     
    subject: Request Variable