File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Web Component Certification (SCWCD/OCPJWCD) and the fly likes is servlet/jsp thread safe? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Web Component Certification (SCWCD/OCPJWCD)
Bookmark "is servlet/jsp thread safe?" Watch "is servlet/jsp thread safe?" New topic

is servlet/jsp thread safe?

Naresh Saw
Ranch Hand

Joined: Sep 03, 2005
Posts: 66
hi all

is servlet/jsp thread safe?

naresh<br />SCJP 1.4(86%), SCWCD 1.4(78%)
Narendra Dhande
Ranch Hand

Joined: Dec 04, 2004
Posts: 951


Narendra Dhande
SCJP 1.4,SCWCD 1.4, SCBCD 5.0, SCDJWS 5.0, SCEA 5.0
Adeel Ansari
Ranch Hand

Joined: Aug 15, 2004
Posts: 2874
Yes. But No.
It depends how you are doing it.
Alankar Yannam

Joined: Oct 31, 2005
Posts: 6
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

Joined: Sep 03, 2005
Posts: 66
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

Joined: Nov 06, 2000
Posts: 1510
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)

So much trouble in the world -- Bob Marley
I agree. Here's the link:
subject: is servlet/jsp thread safe?
It's not a secret anymore!