Win a copy of Java Mock Exams (software) this week in the Programmer Certification (OCPJP) forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

is servlet/jsp thread safe?

 
Naresh Saw
Ranch Hand
Posts: 66
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi all

is servlet/jsp thread safe?
 
Narendra Dhande
Ranch Hand
Posts: 951
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
NO.

Thanks
 
Adeel Ansari
Ranch Hand
Posts: 2874
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes. But No.
It depends how you are doing it.
 
Alankar Yannam
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In general Servlet/JSP are not thread safe.

Following are the guidelines:

1. Application (ServletContext) attributes, session(HttpSession) attributes, Servlet Instance variables and Servlet class variables are "not" thread safe.

2. Local variables (variables defined in a servlet methods) and request (ServletRequest) attributes are thread safe.

3. If you implement SingleThreadModel interface in a servlet then you can make Servlet Instance variables also thread safe. (In a JSP you implement SingleThreadModel by specifying page directive <%@ page isThreadSafe="false" %>

Note: SingleThreadModel interface is deprecated in Servlet API 2.4.

Alankar Yannam
 
Naresh Saw
Ranch Hand
Posts: 66
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks all


but if somebody ask u how u make ur servlet/jsp thred safe, not explicitly asking u context, session, request attibute, what will be ur answer.

again the page directive <%@page isThreadSafe="true"%> which is by default say that jsp is thread safe.

again confusing thing.

any clarifiction abt it.
 
Bosun Bello
Ranch Hand
Posts: 1511
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, the default is true. But, that is assuming that you will take care of actually making it thread-safe. The most appropriate thing to do is synchronize on any process/logic that will make your servlets (including servlets resulting from JSP) NOT thread-safe. (i.e. any logic that can be accessed by more than one thread simultaneously)
 
What are you doing? You are supposed to be reading this tiny ad!
the new thread boost feature brings a LOT of attention to your favorite threads
https://coderanch.com/t/674455/Thread-Boost-feature
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!