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

thread safe

Gopal Krishna

Joined: Sep 22, 2005
Posts: 10
Hi All,

how do i make our servelts are thread safe??

Ben Souther

Joined: Dec 11, 2004
Posts: 13410

Welcome to JavaRanch!

We're pleased to have you here with us in the servlets forum, but there are a few rules that need to be followed, and one is that proper names are required. Please take a look at the JavaRanch Naming Policy and adjust your display name to match it.

In particular, your display name must be a first and a last name separated by a space character, and must not be obviously fictitious.

You can change it here

Further, I can see that you've been asked at least twice before.
Please understand that accounts with invalid screen names are subject to being closed.
[ July 06, 2006: Message edited by: Ben Souther ]

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

Joined: Jun 14, 2006
Posts: 43
Implement Single thread model interface if you want to make your servlet thread safe. BUT BE CAUTIOUS, it makes performance of a servlet very slow. Until and unless its is unavoidable one should not go for Single Thread model.. The interface is available in javax.servlet package.
ak pillai
Ranch Hand

Joined: Feb 11, 2006
Posts: 288
SingleThreded model can adversely affect performance and hence depreceted as of spec 2.4.

You can synchronize on a block or method level where it is not thread-safe. Also can store your objects/ attributes in a session or database as opposed to as an instance variable in your servlet class.

java j2ee job interview questions with answers | Learn the core concepts and the key areas
Adam Asham

Joined: Jul 06, 2006
Posts: 13
You could also make your code thread safe by not using instance variables, for example. That is one step in the right direction.
I agree. Here's the link:
subject: thread safe
It's not a secret anymore!