We help IT Professionals succeed at work.

A question for JSP developers only.

Baraa
Baraa asked
on
Objects scope: session VS application !!!

I'm going through the JSP tutorial available in the following link: http://developer.java.sun.com/developer/onlineTraining/JSPIntro/contents.html

Seems really logical and I think I'm getting it except that when I try the exercises under "Using JavaBean Components", the first one, I get a contradicting outcome with what the concept describes.

The exercise contains a java bean declares an integer and "counter", has one method to increment the counter and one getter method to return it whenever required.

The exercise also include a jsp page that declares two variables with different scopes: session and application. Both use the counter bean. So whenever I refresh the browser I should get the session variable changing and the application variable not. When another browser instance is launched I should get the application variable incremented and the session variable reset to start all over. But what's hapenning is that both get incremented when I refresh the browser!!!

The bean code:
package com.jguru;
public class CounterBean {
int count;

public int getCount() {
return count;
}

public void increaseCount() {
count++;
}
}



The JSP code:
<%@ page import="com.jguru.CounterBean" %>
<jsp:useBean id="session_counter" class="com.jguru.CounterBean" scope="session" />
<jsp:useBean id="app_counter" class="com.jguru.CounterBean" scope="application" />

<% session_counter.increaseCount();
synchronized(page) {
app_counter.increaseCount();
}
%>
<h3>
Number of accesses within this session:
<jsp:getProperty name="session_counter" property="count" />
</h3>
<p>
<h3>
Total number of accesses:
<% synchronized(page) { %>
<jsp:getProperty name="app_counter" property="count" />
<% } %>
</h3>


Looking forward for your replies...
Comment
Watch Question

Commented:
don't you want to use synchronized(application) instead of synchronized(page)?

CJ

Commented:
I believe the session variable relies on cookies or something which misleads it into believing that 2 open browsers on 1 PC = 1 session.

Commented:
dorothy2 is right. 2 open browsers which are from same products on 1 PC share 1 session. but, if they are from different products (eg. 1 is Netscape and another is IE), both have their own session. You may try to use different product browsers

Author

Commented:
I discovered something.

The reason I was getting the session and the application counters incremented together and always get the same value for them from different ie5.0 browser instances, is that I was openning the new instance with the Ctrl&N combination of keys. The same didn't happen when I opened another instance with the normal way, that is from it's executable. Looks like following the first approach makes the new instance in away attached to the first one. Interesting isn't it? Try this is out and let me know what you think of such behaviour!

This is not any issue though, because for sure different clients will have totally different machines in the production environment and won't face such a problem.

cheekycj's comment interested me and I tried it out, but same behaviour encounted. I'll tell you the truth I'm new to JSP and learning the concepts. So how far Synchronizing application, session, page differe?! Is there any difference at all?

Thanks for your attempts to help anyway. I didn't get your comments on the exact time you posted them. I just got them now. Looks like EE got some problems!

Bara'a.

Commented:

Commented:
No comment has been added lately, so it's time to clean up this TA.
I will leave a recommendation in the Cleanup topic area that this question is:

- Points for cheekycj  

Please leave any comments here within the next seven days.
 
PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!
 
Venabili
EE Cleanup Volunteer

Explore More ContentExplore courses, solutions, and other research materials related to this topic.