How to make servlet thread safe?
Answers were Sorted based on User's Feedback
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 |
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 |
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 |
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 |
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 |
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 |
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 |
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 |
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 |
What is URL Encoding?
Why do we use sendredirect() method?
What is the procedure of invoking different servlet in a different application?
Write all the phases defined in servlet life cycle?
if we know the any consultancies, it will place in company work through consultancy(contract base). please help me I'm 2008 pass out java candidate...
When the methods init() and Distroy() will be called?
Explain the difference between get and post method in servlet?
How many ways to remove the session object in the container
How can we implement a jsp page?
What is the web server used for running the Servlets?
What is the difference between encodeRedirectUrl and encodeURL?
What exactly is a servlet?