Is Servlet Class Thread safe?????? How to make servlet
Thread safe ???

Answers were Sorted based on User's Feedback



Is Servlet Class Thread safe?????? How to make servlet Thread safe ???..

Answer / samba

NO.
Servlet is not thread safe.it allows to access more than one
thread at a time.IF we want to make servlet as a thread safe
then make service method is synchronized or implement
SingleThreadModel interface.There is no methods in this
interface.

Is This Answer Correct ?    21 Yes 0 No

Is Servlet Class Thread safe?????? How to make servlet Thread safe ???..

Answer / venkat

by default every servlet is not Thread safe.

if we want to make Thread safe use these statements

1) use SingleThreadModel interface . but this interface is
deprecated in servlet 2.5 specification

2) use synchronized methods and blocks

for example
=========
use synchronized method
=======================


public synchronized void service(ServletRequest
request,ServletResponse response) throws
ServletException,IOException
{
.........
.......
}

use synchronized block
========================


public void service(ServletRequest request,ServletResponse
response) throws ServletException,IOException
{
synchronized(this)
{
.........
.......
}
}

Is This Answer Correct ?    5 Yes 0 No

Is Servlet Class Thread safe?????? How to make servlet Thread safe ???..

Answer / venkat

by default every servlet is not Thread safe.

if we want to make Thread safe use these statements

1) use SingleThreadModel interface . but this interface is
deprecated in servlet 2.5 specification

2) use synchronized methods and blocks

for example
=========
use synchronized method
=======================


public synchronized void service(ServletRequest
request,ServletResponse response) throws
ServletException,IOException
{
.........
.......
}

use synchronized block
========================


public void service(ServletRequest request,ServletResponse
response) throws ServletException,IOException
{
synchronized(this)
{
.........
.......
}
}

Is This Answer Correct ?    1 Yes 0 No

Is Servlet Class Thread safe?????? How to make servlet Thread safe ???..

Answer / santhosh kandula


Yes, Servlet is thread safe, because the process of
servlet is request and response. whenever the request comes
then the server can respond. suppose at a time there more
request are came to the server in that which request is the
server can respond, in that time threads will be created
and based on priority of the thread response will be given
for the request.
So, servlets are thread safe.By using thread
properties we make the servlet Thread safe.

Is This Answer Correct ?    2 Yes 23 No

Post New Answer

More Servlets Interview Questions

what is the advantage of using Servlets over CGI programming?

4 Answers  


What do you mean by scope object and what are its types?

0 Answers  


What are the types of protocols supported by httpservlet ?

0 Answers  


What do you mean by cgi and what are its drawbacks?

0 Answers  


When Servlet is unloaded?

0 Answers  






Some examples where generic servlet is used?

1 Answers  


i have 500 jsp in my application ,with same forward name,it lokks for appropritae jsp from 1 to 500,but i want to send response to 498 page,what should i do

2 Answers   Caritor, Globerian,


can it possible to validate form field before execution of a servlet service method if yes how??

6 Answers  


What is the use of servletconfig interface?

0 Answers  


How to pass JavaBeans data to JSP using Servlets?

2 Answers  


What is the effective way to make sure all the servlets are accessible only when user has a valid session?

0 Answers  


What is the difference between ExecuteUpdate and ExecuteQuery?

4 Answers  


Categories