How to make servlet thread safe?

Answers were Sorted based on User's Feedback



How to make servlet thread safe?..

Answer / tulasi vani

There are two different ways of making a servlet thread
safe namely

1.By implementing SingleThreadModel.

By implementing a SingleThreadModel it will be possible to
create a Thread safe servlet.There can only be one user at
a given point of time.

2.Synchornize the part of sensitive code.

We can allow a single user at a given point of time by
making that part of the code which is sensitive as
synchronized.

Is This Answer Correct ?    127 Yes 6 No

How to make servlet thread safe?..

Answer / shakir khan

There are situations where we want to protect your servlet
member variables from being modified by different
clients.In this case you can have your servlet by
implementing the marker interface SigleThreadModel.

Everytime a client makes request to a servlet by
implementing this interface,servlet engine will create a
new instance of servlet.

For performance reason,servlet engine can also maintain a
instance pool,handing out instances as they are needed.Or
it could also serialize client request executing one after
another.

Is This Answer Correct ?    49 Yes 9 No

How to make servlet thread safe?..

Answer / snehal

requests to your webpage may and probably will occur concurrently which means multiple threads will be running your code simultaneously. This means you have to take care that one thread do not interfere with processing of other threads, therefore thread-safety is an important issue in web application. Developers should be aware of this issue and should make sure their code works in a thread-safe way.

import javax.servlet.*;
import javax.servlet.http.*;

public class IamThreadSafeServlet extends HttpServlet
implements SingleThreadModel {

/*SingleThreadModel is an Marker Interface which we
have to implement to make a servlet thread safe*/

private ServletConfig config;

public void init (ServletConfig config)
throws ServletException {
this.config = config;
}

public void doGet (HttpServletRequest req,
HttpServletResponse res ) throws ServletException, IOException {

res.setContentType( "text/html" );
PrintWriter out = res.getWriter();
out.println( "<html>" );
out.println( "<head>" );
out.println( "<title>This is A Thread safe Servlet</title>" );
out.println( "</head>" );
out.println( "<body>" );
out.println( "<h1>A Sample Servlet</h1>" );
out.println( "</body>" );
out.println( "</html>" );
out.close();
}
}

Is This Answer Correct ?    24 Yes 2 No

How to make servlet thread safe?..

Answer / sanjay

to make servlet as threas safe we have three approaches
1.do not instance variables into our servlet we use only
local variables
2.by using synchronized methods
3.synchronized blocks
above three ways synchronized blocks is the best way

Is This Answer Correct ?    12 Yes 1 No

How to make servlet thread safe?..

Answer / nagababu

implementing SingleThreadModel. If a Servlet class provides
implementation of SingleThread model interface, the web
container creates multiple objects.

Is This Answer Correct ?    12 Yes 7 No

How to make servlet thread safe?..

Answer / surjit

We can make a servlet thread by implementing the SingleThreadModel interface.
i.e
Public class EmployeeTest extends HttpServlet implement SingleThreadModel{

Service(){
write yor code..........
}

}

Is This Answer Correct ?    2 Yes 0 No

How to make servlet thread safe?..

Answer / bhaskar padala

The servlet programmer should implement SingleThreadModel interface to ensure that servlet can handle only one request at a time. It is a marker interface, means have no methods.


This interface is currently deprecated since Servlet API 2.4 because it doesn't solves all the thread-safety issues such as static variable and session attributes can be accessed by multiple threads at the same time even if we have implemented the SingleThreadModel interface. So it is recommended to use other means to resolve these thread safety issues such as synchronized block etc.

Is This Answer Correct ?    0 Yes 1 No

How to make servlet thread safe?..

Answer / a.srinivs rao

in order to make a servlet thread safe we have to declare
the non-sharable variales inside the init method,and
sharble at the servlet class leve.

Is This Answer Correct ?    2 Yes 31 No

How to make servlet thread safe?..

Answer / monkyspiderpig

The only thing you need to do is to define only static
variables at Class level. When a method is called each
thread gets a new variable.

Is This Answer Correct ?    8 Yes 41 No

Post New Answer

More Servlets Interview Questions

Who is responsible to create the object of servlet?

0 Answers  


Why do we use sendredirect() method?

0 Answers  


What are the different types of servlets?

0 Answers  


How to prevent browser from caching the page content?

1 Answers  


What do you mean by servlet?

0 Answers  






How to generate the server side programming and the advantages of it over the other languages?

0 Answers  


What is the purpose of inter-servlet communication?

0 Answers  


Explain the working of service() method of a servlet.

0 Answers  


give the syntax of doGet() and doPost()?

2 Answers  


What do you mean by the servlet chaining?

0 Answers  


What is difference between GenericServlet and HttpServlet?

0 Answers  


What's the servlet interface?

0 Answers  


Categories