Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

java

servlets

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.

like image 558
Maxim Veksler Avatar asked Oct 28 '10 12:10

Maxim Veksler


People also ask

Is a new servlet created for every request?

Each HTTP request creates a new thread but accesses the same instance of the Servlet. EDIT: In case of one server node, you will have the same Servlet instance on that node. In case of load balancing/many servers you will usually have one instance per Java VM.

Is javax servlet HTTP HttpServlet?

An abstract class that simplifies writing HTTP servlets. It extends the GenericServlet base class and provides an framework for handling the HTTP protocol. Because it is an abstract class, servlet writers must subclass it and override at least one method.

What method is used in the HttpServlet class to handle HTTP GET requests?

Methods of HttpServlet classprotected void doGet(HttpServletRequest req, HttpServletResponse resp): This method is called by servlet service method to handle the HTTP GET request from client.

Why we use HttpServlet instead of GenericServlet?

The main difference between GenericServlet and HttpServlet is that the GenericServlet is protocol independent that can be used with any protocol such as HTTP, SMTP, FTP, CGI etc. while HttpServlet is protocol dependent and is only used with HTTP protocol.


1 Answers

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.

like image 136
Bozho Avatar answered Sep 28 '22 00:09

Bozho