![]() |
The Java Developers Almanac 1.4Order this book from Amazon. |
e1041. Preventing Concurrent Requests to a ServletBy 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 theSingleThreadModel 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.
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. |