Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Newbie - Declarations

 
scottie zman
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
  • 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
 
Manjunath Reddy
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
  • 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
 
Bosun Bello
Ranch Hand
Posts: 1510
  • Mark post as helpful
  • send pies
  • 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
  • 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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic