The JavaTM Tutorial
Previous Page Lesson Contents Next Page Start of Tutorial > Start of Trail > Start of Lesson Search
Feedback Form

Trail: Servlets
Lesson: Interacting with Clients

Threading Issues

HTTP servlets are typically capable of serving multiple clients concurrently. If the methods in your servlet that do work for clients access a shared resource, then you can handle the concurrency by creating a servlet that handles only one client request at a time. (You could also synchronize access to the resource, a topic that is covered in this tutorial's lesson on threads.)

To have your servlet handle only one client at a time, have your servlet implement the SingleThreadModel(in the API reference documentation)interface in addition to extending the HttpServlet class.

Implementing the SingleThreadModel interface does not involve writing any extra methods. You merely declare that the servlet implements the interface, and the server makes sure that your servlet runs only one service method at a time.

For example, the ReceiptServlet accepts a user's name and credit card number, and thanks the user for their order. If this servlet actually updated a database, for example one that kept track of inventory, then the database connection might be a shared resource. The servlet could either synchronize access to that resource, or it could implement the SingleThreadModel interface. If the servlet implemented the interface, the only change in the code from the previous section is the one line shown in bold:

    public class ReceiptServlet extends HttpServlet
                                implements SingleThreadModel {

        public void doPost(HttpServletRequest request,
                           HttpServletResponse response)
	    throws ServletException, IOException {
                ...
        }
        ...
    }

Previous Page Lesson Contents Next Page Start of Tutorial > Start of Trail > Start of Lesson Search
Feedback Form

Copyright 1995-2002 Sun Microsystems, Inc. All rights reserved.