• 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

Understanding SingleThreadModel

 
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Guys,

I'm try'n to understand the JSP.

If I have a instance variable in my JSP page. Do I have to implement the synchronization block on that variable or not?

I had also read in one of the reference that instance variable should not be used in JSP. To use declaration in JSP, create non-changing variable. What does non-changing variable means and how can we create it?

Ravinder
 
Ranch Hand
Posts: 186
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Running an application using the SingleThreadModel is pretty useless for most purposes - all it does is hurt your concurrency, its better to use syncronisation blocks when required. As for instance variables - avoid using them if you can because of threading issues, making the instance variable final and static may work for you, but remember for final object references you are only making the reference final not the object's properties.
 
Ravinder S Edhan
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Daniel,

One more thing I would like to know ... suppose I'm having following code ..

<%! int counter = 0; %>
<%= ++counter %>

Whenever I hit for new request ... the 'counter' get incremented. Shall we assume that all instance variable are 'static'.

Ravinder
 
Sheriff
Posts: 13411
Firefox Browser VI Editor Redhat
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
JSPs get compiled into Servlet code.
The container will create one instance of each servlet.
That one instance will handle all requests (seperate thread for each).
An instance variable in that servlet will be common to all threads.
That's why your int variable is being incremented.

Static means that all instances of that class will point to the same variable.
That instance variable will only be static if you declare it to be.
 
Ranch Hand
Posts: 429
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
With Tomcat I've encountered cases where a non-static instance counter would be incremented several times (less than 10 I believe), and then start from zero again, implying that Tomcat for some reason decided to create a second instance of the JSP Page's servlet.

-Yuriy
 
He's dead Jim. Grab his tricorder. I'll get his wallet and this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic