• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Newbie - Declarations

 
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello,
I'm trying to figure out the scope of declarations. Here's some JSP code:
------------------------------------
<%!int count = 0;%>
<% count++;%>
this page has been accessed <%=count%> times.
-------------------------------------
Whenever I resave the source file, the count starts over again at 1 and continues to increment every time I hit the page. Which makes sense. But when I switch browsers from IE to Opera, it starts again at 1.
So I'm trying to figure this out, is the scope of a declaration based on the session -- does the JSP sessionID get sent back and forth thru a cookie? Or am I seeing this because of a browser cache issue?
I was originally under the impression that a declared variable such as this is shared between all instances of the page for all users of the page? apparently this is incorrect -- can someone lay it down for me please??
thanks mucho
Scottie Zman
 
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Interesting Q...
From my experience, im pretty sure the count of the integer is more related to JVM than session or application. As long as the jvm is running and jsp file doesnt get recompiled(when u save), a copy of the variable exists for all sessions and every user across the application.
And i couldnt simulate the different browser initializing the counter to 1. (if it is... i might have to go back to understanding web fundamentals )
cheers,
mpr
 
Ranch Hand
Posts: 1514
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes, that varaible should be shared accross instances. I tried it, and it's working for me. i.e. One instance of the servlet is loaded, and the counter is incrementing accross both browsers. Verify that it's not recompiling when you load it up in the other browser. Also, make sure that you are not implementing the "single thread" model interface somehow.

Bosun
 
scottie zman
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What causes a jsp to recompile? Only if you edit and then resave it, and then when the page gets accessed, correct? Is there anything browser-related that could cause it to recompile? Is this feature of JSP dependable enough that I could store a big chunk of XML with, say, an threads index to a bbs?
thanks
scottie zman
 
Hold that thought. Tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic