aspose file tools*
The moose likes JSP and the fly likes help in jsp declaration and thread safety? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » JSP
Bookmark "help in jsp declaration and thread safety?" Watch "help in jsp declaration and thread safety?" New topic

help in jsp declaration and thread safety?

gurpeet singh
Ranch Hand

Joined: Apr 04, 2012
Posts: 923

i am reading Head first servlet and jsp book. i have learned that in a servlet instance variables are not thread safe and a well written servlet wont have any instance variables. in the chapter of jsp the book says that a jsp becomes a servlet. also using jsp declarations <%! > we can declare instance varialbes, static variables etc. so how does this thing fits into the earlier part that a well written servlet does not or should not have instance variables ? or are instance variables declared using jsp declarations thread-safe ?

OCPJP 6(100 %) OCEWCD 6(91 %) OCPJBCD(93%)
Bear Bibeault
Author and ninkuma

Joined: Jan 10, 2002
Posts: 60050

Java code in JSPs has been obsolete and discredited for over 10 years now -- why are you still using them?

But to answer the question, yes, creating declarations can make a JSP non-thread-safe. Don't do it.

[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Stefan Evans

Joined: Jul 06, 2005
Posts: 1016
It fits in exactly.

Using <%! scriptlet declaration tag %> you can declare an instance variable for your jsp/servlet.
It is exactly the same deal as declaring an instance variable in a servlet: they are not thread safe, and are thus their usage is discouraged.

In fact, scriptlets in general on a JSP page are discouraged. Instead you should be using EL and custom tag libraries (like JSTL)
gurpeet singh
Ranch Hand

Joined: Apr 04, 2012
Posts: 923

Thanks Bear and Stefan. and yes i know that scripting elements in jsp are not good and we should be using EL and JSTL. im preparing for ocewcd 6 certification and right now just at the start of jsp chapter whereby author is telling me various scripting elements and where they land in the container generated servltet.
I agree. Here's the link:
subject: help in jsp declaration and thread safety?
Similar Threads
What is thread safe?
Multiple User problem
Alternative to Single Thread model ?
Difference between Single and Multi Thread Model