This week's book giveaway is in the Reactive Progamming forum.
We're giving away four copies of Reactive Streams in Java: Concurrency with RxJava, Reactor, and Akka Streams and have Adam Davis on-line!
See this thread for details.
Win a copy of Reactive Streams in Java: Concurrency with RxJava, Reactor, and Akka Streams this week in the Reactive Progamming forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Junilu Lacar
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Knute Snortum
  • Tim Cooke
  • Devaka Cooray
Saloon Keepers:
  • Ron McLeod
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Ganesh Patekar

Request Variable

 
Bartender
Posts: 1681
17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 1871
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Bartender
Posts: 1681
17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Cool, I remember reading about this a while back.
Thanks much for your reply.
-- Mike
 
Author
Posts: 111
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
    Posts: 1871
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator


    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 ]
     
    See ya later boys, I think I'm in love. Oh wait, she's just a tiny ad:
    Java file APIs (DOC, XLS, PDF, and many more)
    https://products.aspose.com/total/java
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!