File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Servlets and the fly likes Newbie - Declarations Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "Newbie - Declarations" Watch "Newbie - Declarations" New topic

Newbie - Declarations

scottie zman
Ranch Hand

Joined: Aug 08, 2001
Posts: 40
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

Joined: Jul 26, 2001
Posts: 60
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 )
Bosun Bello
Ranch Hand

Joined: Nov 06, 2000
Posts: 1510
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.


So much trouble in the world -- Bob Marley
scottie zman
Ranch Hand

Joined: Aug 08, 2001
Posts: 40
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?
scottie zman
I agree. Here's the link:
subject: Newbie - Declarations
It's not a secret anymore!