简体   繁体   中英

Should a servlet container create new javax.servlet.http.HttpServlet instance for each incoming request?

I have a class public class GAE_SERVLETREQUESTServlet extends HttpServlet {

Not sure what the spec says about recycling of the HTTPServlet: Should the servlet container create new instance of this class on each incoming request or can the implementation reuse classes between requests?

I'm investigating a funny issue where it seems that a Map created on the GAE_SERVLETREQUESTServlet instance maintains state between requests.

For the general case - non-distributed, multi-threaded, it is guaranteed that there will be only one instance of the servlet. From the Servlet 3.0 specification:

2.1 Request Handling Methods

The basic Servlet interface defines a service method for handling client requests. This method is called for each request that the servlet container routes to an instance of a servlet. The handling of concurrent requests to a Web application generally requires that the Web Developer design servlets that can deal with multiple threads executing within the service method at a particular time. Generally the Web container handles concurrent requests to the same servlet by concurrent execution of the service method on different threads.

2.2 Number of Instances

The servlet declaration which is either via the annotation as described in Chapter 8, “Annotations and pluggability” or part of the deployment descriptor of the Web application containing the servlet, as described in Chapter 14, “Deployment Descriptor”, controls how the servlet container provides instances of the servlet. For a servlet not hosted in a distributed environment (the default), the servlet container must use only one instance per servlet declaration . However, for a servlet implementing the SingleThreadModel interface, the servlet container may instantiate multiple instances to handle a heavy request load and serialize requests to a particular instance.

In the case where a servlet was deployed as part of an application marked in the deployment descriptor as distributable, a container may have only one instance per servlet declaration per Java Virtual Machine (JVM™)1. However, if the servlet in a distributable application implements the SingleThreadModel interface, the container may instantiate multiple instances of that servlet in each JVM of the container.

If you are saving data that is relevant to each user, you should store it in the HTTP Session. As stated by skaffman, do not store data in the servlet class that you expect to be different for each each user. Here is a quick example.


class MyServlet extends HttpServlet
{
    private Object ThisIsTheWrongPlaceToStorePerUserData;

    ... stuff ... doPut(HttpServletRequest httpRequest, ... more stuff ...)
    {
        Object iAmGood = new Object();
        HttpSession session = httpRequest.getSession(true);

        session.setAttribute("GoodPlaceToStorePerUserData", iAmGood);

        ... stuff ...
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM