The processHttpRequest
method and methods called from it can be executed by multiple threads within the same servlet instance, and
state changes to the instance caused by these methods are, therefore, not threadsafe.
This is due to the servlet container creating only one instance of each servlet (javax.servlet.http.HttpServlet
) and attaching a
dedicated thread to each incoming HTTP request. The same problem exists for org.apache.struts.action.Action
but with different
methods.
To prevent unexpected behavior, avoiding mutable states in servlets is recommended. Mutable instance fields should either be refactored into local
variables or made immutable by declaring them final
.
Exceptions
- Fields annotated with
@javax.inject.Inject
, @javax.ejb.EJB
,
@org.springframework.beans.factory.annotation.Autowired
, @javax.annotation.Resource
- Fields initialized in
init()
or init(ServletConfig config)
methods