Hi, I have a simple jsp here that I can't quite comprehend: <jsp:useBean id="session_counter" class="CounterBean" scope="session"/> <jsp:useBean id="app_counter" class="CounterBean" scope="application"/> <%session_counter.increment(); 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> Can someone help me understanding what is the object page? And why is it synchronized for application counter?
page: synonym for the "this" operator, as an HttpJspPage I think the code is flawed. It should be synchronizing the app_counter bean itself and not the page.
Hi Cameron, By using synchronized(page) you are actually locking the instance of your JSP/Servlet class.If you look at the generated Servlet source, you would find
Hence, this represents the instance of your JSP page. By locking the instance, you are providing serialized access to yor page.So if 2 clients access your JSP page, the count would be 2.Had you not synchronized it, probably the count would have been wrongly calculated, hence the synchronization. Hope this helps. Regards,
Oracle JDeveloper Rel. 3.0 - Develop Database Applications with Java Scored 56 out of 59
IBM Enterprise Connectivity with J2EE Scored 72 per cent
Enterprise Development on the Oracle Internet Platform Scored 44 out of 56
<b>Sandeep</b> <br /> <br /><b>Sun Certified Programmer for Java 2 Platform</b><br /> <br /><b>Oracle Certified Solution Developer - JDeveloper</b><br /><b>-- Oracle JDeveloper Rel. 3.0 - Develop Database Applications with Java </b><br /><b>-- Object-Oriented Analysis and Design with UML</b><br /> <br /><b>Oracle Certified Enterprise Developer - Oracle Internet Platform</b><br /><b>-- Enterprise Connectivity with J2EE </b><br /><b>-- Enterprise Development on the Oracle Internet Platform </b>
Bhupinder Dhillon
Ranch Hand
Joined: Oct 12, 2000
Posts: 124
posted
0
Originally posted by Madhav Lakkapragada: [B] re your comment abv, is the "this" variable avail in a JSP. ie; can I have a JSP like:
Thanks. - satya[/B]
Yes, it's completely valid.
Cameron Park
Ranch Hand
Joined: Apr 06, 2001
Posts: 371
posted
0
Is it like single thread model?
Bhupinder Dhillon
Ranch Hand
Joined: Oct 12, 2000
Posts: 124
posted
0
Originally posted by Cameron Park: Is it like single thread model?
Not really, because the jsp container can still call process the code that is outside of synchronized block. [This message has been edited by Bhupinder Dhillon (edited April 25, 2001).]
Madhav Lakkapragada
Ranch Hand
Joined: Jun 03, 2000
Posts: 5040
posted
0
Thanks Bhupinder. (Also, on a personal note, I might already know you...does Prasad (a friend of mine) ring any bells? ) No, it is NOT single thread. It is the exact opposite. Single thread allows ONLY one thread to run the JSP at a time. On the other hand synchronised allows many threads to run the JSP safely but ONE at a time (one after the other). hope you get the point. OOPS, looks like I was competing with Bhupinder.... regds. - satya
[This message has been edited by Madhav Lakkapragada (edited April 25, 2001).]
Bhupinder Dhillon
Ranch Hand
Joined: Oct 12, 2000
Posts: 124
posted
0
Originally posted by Madhav Lakkapragada: Thanks Bhupinder. (Also, on a personal note, I might already know you...does Prasad (a friend of mine) ring any bells? )
No actually I live in Calgary, Canada and don't know anyone by that name.
Single thread allows ONLY one thread to run the JSP at a time.
Don't forget that the container may choose to create a pool of jsp instances and spread out the workload. That's why you still need to synchronize a shared object like a file on the server.
Madhav Lakkapragada
Ranch Hand
Joined: Jun 03, 2000
Posts: 5040
posted
0
Yup, I agree. The only other alternative is to use static fields. However, instance fields SHOULD be synchronized even with SignleThread impl. Thanks Bhupinder. I saw, on your profile that you are in Calgary, but then I thought maybe you moved. Well, anyways! - satya
Originally posted by Madhav Lakkapragada: The only other alternative is to use static fields. However, instance fields SHOULD be synchronized even with SignleThread impl.
Why? Or did you mean to say that static fields should be synchronized even with SingleThreadModel? - Peter
Madhav Lakkapragada
Ranch Hand
Joined: Jun 03, 2000
Posts: 5040
posted
0
ummm....the way I understand is: if you have instance fields and you are implementing SingleThread interface still you need to synchronize the instance fields using some object. if you have static/class fields and you are implementing SingleThread interface then you need not synchronize the static/class fields. Hope I am clear this time..... regds. - satya
[This message has been edited by Madhav Lakkapragada (edited April 26, 2001).]
Desai Sandeep
Ranch Hand
Joined: Apr 02, 2001
Posts: 1157
posted
0
Hi Madhav,
if you have static/class fields and you are implementing SingleThread interface then you need not synchronize the static/class fields.
I think, you would still need to synchronize it.If you refer to the API, for the SingleThreadModel, it says:
However, this interface does not prevent synchronization problems that result from servlets accessing shared resources such as static class variables or classes outside the scope of the servlet.
This means if you have a static field say
You would need to synchronize access to the con object, as SingleThreadModel doesnot take care of static class variables defined outside the scope of the servlet. Hope this helps. Regards,
Oracle JDeveloper Rel. 3.0 - Develop Database Applications with Java Scored 56 out of 59
IBM Enterprise Connectivity with J2EE Scored 72 per cent
Enterprise Development on the Oracle Internet Platform Scored 44 out of 56
Peter den Haan
author
Ranch Hand
Joined: Apr 20, 2000
Posts: 3252
posted
0
Originally posted by Madhav Lakkapragada: if you have instance fields and you are implementing SingleThread interface still you need to synchronize the instance fields using some object. if you have static/class fields and you are implementing SingleThread interface then you need not synchronize the static/class fields.
Exactly the wrong way around. If you are implementing SingleThreadModel, the servlet engine contract guarantees that no more than one single thread will ever be executing your jsp service method. This makes it safe to leave instance variables un-synchronized. Look at it this way, if you'd properly synchronize them your jsp would be threadsafe! On the other hand, the servlet engine may create multiple instances of your JSP servlet to handle high loads. All of those instances will compete for access to the static variables. For that reason, static resources will still have to be threadsafe. - Peter
Desai Sandeep
Ranch Hand
Joined: Apr 02, 2001
Posts: 1157
posted
0
Absolutely correct, Peter if you implement SingleThreadModel, you need not worry about the instance variables, as every instance variable is local to the Thread.But the static variables like the Connection, HttpSession, which would be shared by all the servlets in the web application,so they need to be synchronized, for serial access. Regards, Sandeep Desai vgdesai@bom3.vsnl.net.in
Sun Certified Java ProgrammerScored 93 per cent
Oracle JDeveloper Rel. 3.0 - Develop Database Applications with Java Scored 56 out of 59
IBM Enterprise Connectivity with J2EE Scored 72 per cent
Enterprise Development on the Oracle Internet Platform Scored 44 out of 56
[This message has been edited by Desai Sandeep (edited April 26, 2001).]