The Java Developers Almanac 1.4


Order this book from Amazon.

   
Home > List of Packages > javax.servlet  [11 examples] > Requests  [6 examples]

e1041. Preventing Concurrent Requests to a Servlet

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.

 Related Examples
e1040. Getting a Request Parameter in a Servlet
e1042. Getting the Requesting URL in a Servlet
e1043. Getting a Request Header in a Servlet
e1044. Processing a HEAD Request in a Servlet
e1045. Getting the Client's Address in a Servlet


© 2002 Addison-Wesley.