Knowledgebase: Java/JSP Hosting Plans
Preventing Concurrent Requests to a Servlet
Posted by Customer Service on 25 May 2005 12:46 AM
By default, a servlet container will use a servlet instance to concurrently process multiple requests. Although, this behavior burdens the servlet developer to properly synchronize shared state, this ability to handle multiple requests allows for a more scalable architecture. However, if it is necessary to disable this ability, the servlet should implement the SingleThreadModel interface:
    // This servlet can only handle a single request at a time.
public class MyServlet extends HttpServlet implements SingleThreadModel {
public void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException {
// Handle GET request...
However, forcing the servlet container to route requests one at a time through a servlet does not prevent the container from creating multiple instances of the servlet. Hence, the servlet developer must still properly synchronize shared state. Effectively, the main benefit of implementing the SingleThreadModel is not having to synchronize the servlet's instance variables.
(548 vote(s))
Not helpful

Comments (0)