What about Servlets and Threads?
- The Servlet will executed new Threads for each request so no data should be saved related to the client (user) that making the request.
How to Make Your Servlets Thread-Safe?
- Use method variables for request-specific data.
- As far as possible, use the member variables of the servlet only for data that does not change.
- When you are using member variables or context attributes that can be changed by a request, you need to synchronize access to that data so that different threads aren’t changing the data simultaneously.
- If your servlet accesses outside resources, consider protecting access to that resource. You must include code that manages synchronized access to this resource.
How Not to Make Your Servlets Thread-Safe?
public class MyServlet implements SingleThreadModel
- Synchronize service(), doPost(), or doGet(). This attempt at making the servlet thread-safe is even worse than attempting to use SingleThreadModel. If you override service() and make it synchronized at the same time, you have limited your to handling only a single request at a time. As the number of requests increases, your clients are more and more likely to spend their time watching the little progress icon go around and around.
© 2010 by Finnesand Data. All rights reserved.
This site aims to provide FREE programming training and technics.
Finnesand Data as site owner gives no warranty for the
correctness in the pages or source codes.
The risk of using this web-site pages or any program
codes from this website is entirely at the individual user.