How does JSP process concurrent requests?


I guess my question is, a JSP is compiled into a single servlet instance that serve multiple requests. How do I make it threadsafe?


Servlets are meant to be immutable. Either no state exists outside of method calls (the servlet is stateless), or any such state will never change (so the state that each thread sees is always the same).

It's extremely simple to write a threadsafe servlet: <strong>never</strong> use instance variables. Use method-local variables.


Try this: <%@ page isThreadSafe="true" %>


Just don't assign request/session specific data as global/static variables. So as long as you don't use <em>scriptlet</em> declarations <%! %> which you assign with request/session specific data and you don't put request/session data in application scope (i.e. as attribute of ServletContext), then you're safe.

<h3>See also:</h3> <ul><li><a href="https://stackoverflow.com/questions/3177733/how-to-avoid-java-code-in-jsp-files" rel="nofollow">How to avoid Java code in JSP?</a></li> <li><a href="https://stackoverflow.com/questions/3106452/java-servlet-instantiation-and-session-variables" rel="nofollow">Servlet instantiation, session variables and threadsafety</a></li> </ul>


JSPs are compiled into servlets. All JSP variables are method local (stack) variables hence they are thread safe.

If you directly add a thread-unsafe attribute to a servlet class, it will cease to be thread safe.


  • In-memory caching in Azure function
  • Spring Security OAuth2 correct Authorization Manager
  • Are Java Spring services evil as they are singleton? [closed]
  • Is it a bad practice to rely on local objects get destructed in the reverse order of construction in
  • Feature Event Handler called multiple times for Farm level feature - sharepoint 2007
  • Assigning {pointer to function with C linkage} to {pointer to function with C++ linkage} and vice ve
  • JAR doesn't work with Absolute Layout
  • CDI: Injecting single instance works, but injecting Instance does not. Why?
  • How to lookup value with multiple criteria in excel 2007 and newer
  • SQL Server re-calculate or not?
  • Watson Conversation - Why is the ANYTHING ELSE node not chosen
  • Primefaces lazy datascroller calling load twice
  • How to add git credentials to the build so it would be able to be used within a shell code?
  • How to determine if there are bytes available to be read from boost:asio:serial_port
  • Groovy: Unexpected token “:”
  • Admob requires api-13 or later can I not deploy on old API-8 phones?
  • Unity3D & Android: Difference between “UnityMain” and “main” threads?
  • How to create a file in java without a extension
  • Spray.io: When (not) to use non-blocking route handling?
  • Does CUDA 5 support STL or THRUST inside the device code?
  • MySQL WHERE-condition in procedure ignored
  • Trying to switch camera back to front but getting exception
  • htaccess rewriting URLs with multiple forward slashes
  • Display Images one by one with next and previous functionality
  • Web-crawler for facebook in python
  • Calling of Constructors in a Java
  • Hibernate gives error error as “Access to DialectResolutionInfo cannot be null when 'hibernate.
  • Traverse Array and Display in markup
  • Transpose CSV data with awk (pivot transformation)
  • PHP: When would you need the self:: keyword?
  • A cron job substitute?
  • Free memory of cv::Mat loaded using FileStorage API
  • Why can't I rebase on to an ancestor of source changesets if on a different branch?
  • Angular 2 constructor injection vs direct access
  • How to get Windows thread pool to call class member function?
  • Programmatically clearing map cache
  • costura.fody for a dll that references another dll
  • Observable and ngFor in Angular 2
  • UserPrincipal.Current returns apppool on IIS
  • java string with new operator and a literal