my dog learned polymorphism*
The moose likes JSP and the fly likes Are JSPs thread safe by default or not? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » JSP
Bookmark "Are JSPs thread safe by default or not?" Watch "Are JSPs thread safe by default or not?" New topic
Author

Are JSPs thread safe by default or not?

Arvind Mahendra
Ranch Hand

Joined: Jul 14, 2007
Posts: 1162
http://www.geekinterview.com/question_details/60948
[ June 21, 2008: Message edited by: Bear Bibeault ]

I want to be like marc
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

Arvind,

Do you have a question or a comment about this issue or did you just want to share the link with us?


Java API J2EE API Servlet Spec JSP Spec How to ask a question... Simple Servlet Examples jsonf
Arvind Mahendra
Ranch Hand

Joined: Jul 14, 2007
Posts: 1162
The posters in the link claimed JSPs were thread unsafe by default. But I thought they were thread safe by default.
Joe Harry
Ranch Hand

Joined: Sep 26, 2006
Posts: 9345
    
    2

Originally posted by Arvind Birla:
The posters in the link claimed JSPs were thread unsafe by default. But I thought they were thread safe by default.


Ask yourself, are servlets thread safe? If yes, then a JSP which also is a servlet is thread safe.


SCJP 1.4, SCWCD 1.4 - Hints for you, Certified Scrum Master
Did a rm -R / to find out that I lost my entire Linux installation!
Jimmy Clark
Ranch Hand

Joined: Apr 16, 2008
Posts: 2187
A better question would be, "Is all information about Java posted on the Internet accurate?" "geekinterview.com" ... sounds like a nightmare ...lol
[ June 20, 2008: Message edited by: James Clark ]
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

Short answer, no, JSPs by default are not automatically thread safe.
If you have instance variables that are modified from within the service section of a JSP, they will be shared by all requests causing concurrency issues. As Jothi implied, this is the same for servlets. Like with servlets, there is a mechanism that was supposed to help make your JSPs more thread safe this is the isThreadSafe property which can be set in a JSP page directive. This technique is akin to implementing the deprecated SingleThreadModel interface with a servlet. In fact, most containers implement thread safe JSPs by having the generated code implement this deprecated interface.

For servlets, this turned out not to be the greatest idea which is why SingleThreadModel is deprecated. It was not a good idea because, even when using it, it was possible to write a servlet that is not thread safe.

So, in short, you should avoid relying on the threadSafe directive in JSPs for the same reason that you should avoid using the deprecated SingleThreadModel interface in your servlets.

Avoiding scriptlets altogether is the currently accepted best practice.
JSPs with no scriptlets will have no instance variables and will take you a long way toward having thread safe JSPs.
Deepak Bala
Bartender

Joined: Feb 24, 2006
Posts: 6661
    
    5

Local variables in a JSP cannot be read across instances. Instance variables however are. This is why it is usually not a good practice to have instance variables in a JSP, since they are not thread safe


SCJP 6 articles - SCJP 5/6 mock exams - More SCJP Mocks
Jimmy Clark
Ranch Hand

Joined: Apr 16, 2008
Posts: 2187
How do you create an instance variable in a JSP page?
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

Anything inside the <% ... %> symbols will be re-created inside the service method of the generated servlet.

Anything inside the <%! ... %> symbols will be re-created outside of any methods (the initialization block) in the generated servlet.
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

I probably should have mentioned this before:
A JSP is neither thread-safe nor un-thread-safe.

Just as you can't delare a hammer, knife, or bicycle to be safe or unsafe (unless, of course you're talking about a defective one), you can't say that the technology, JSP is or isn't thread-safe. It's what you do with it that makes it safe or unsafe.

JSP will happily give you enough rope to hang yourself.
Deepak Bala
Bartender

Joined: Feb 24, 2006
Posts: 6661
    
    5

JSP will happily give you enough rope to hang yourself.


hehe. Its been a long day and that made me laugh enough to sleep well.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Are JSPs thread safe by default or not?
 
Similar Threads
Passed Part I - 85%
SCEA 1 : Design Pattern sample questions
SSL question
Scriptlets
How many times onreadystatechange property of XMLHTTPREQUEST would be get called?