This week's book giveaway is in the Spring forum.
We're giving away four copies of Spring in Action (5th edition) and have Craig Walls on-line!
See this thread for details.
Win a copy of Spring in Action (5th edition) this week in the Spring forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Devaka Cooray
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Knute Snortum
  • Junilu Lacar
  • paul wheaton
Saloon Keepers:
  • Ganesh Patekar
  • Frits Walraven
  • Tim Moores
  • Ron McLeod
  • Carey Brown
Bartenders:
  • Stephan van Hulst
  • salvin francis
  • Tim Holloway

is servlet/jsp thread safe?  RSS feed

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

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

Thanks
 
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.
 
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.
 
Ranch Hand
Posts: 1512
  • 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)
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!