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 |
Servlet Chaining? How do you do the Filtering in Servlets?
How do you design microservices?
What are the life cycle methods of the servlet?
What is servlet attributes and their scope?
Hello, My project requirement is like I need to create a web page using MVC pattern. I hava a bean class, jsp page, servlet, service and dao. My jsp has two fields. One is dropdown list. The option values has to get populated from the database table. The other one is a text box and its value has to come from database table. As of now I have defined the fields in bean class, got the values from database using arraylists in dao class and I called this from service class. Can anyone please tell me the workflow of how the servlet will get this arraylist and populate the arraylist values as dropdown options in jsp page? Also I would like to know the role of bean class in MVC pattern? Thanks in advance!
What is the difference between the servlets and cgi programs?
Difference between doget and dopost?
Servlet is pure java object or not?
What is a generic servlet?
What is the life cycle of a servlet?
How we can check in particular page the session will be alive or not
What is servlet and its use?