How to implement a thread-safe JSP page?
A thread-safe JSP/servlet is one that works correctly when more than one thread is running at the same time. To make your JSPs thread-safe, you can implement the SingleThreadModel interface that prevents two threads from accessing the service method at the same time.
The JSP page by default is not thread safe, if you want to make it thread safe then you have add the following directive in your JSP page declaration:
<%@ page isThreadSafe="false" %>
With this, instead of a single instance of the servlet generated for your JSP page loaded in memory, you will have N instances of the servlet loaded and initialized, with the service method of each instance effectively synchronized. You can typically control the number of instances (N) that are instantiated for all servlets implementing SingleThreadModel through the admin screen for your JSP engine
Whether your JSP is thread-safe or not is a consequence of the way you implemented your JSP. Strictly speaking you cannot change that simply by modifying an attribute. JSPs are normally perfectly thread-safe, since your run of the JSP does not hold any state (member variables).
For example, if you happen to use <%! %>, this will place the code in class level and not in the _jspService method. You introduce class member into the JSP object that makes JSP becomes thread-unsafe.
If your JSP is not thread-safe you will have to add the isThreadSafe=false in order for things to work correctly. Such way, it does not make your JSP thread-safe but it does make your web application thread-safe by instructing the servlet container to cater for the thread-unsafety of your JSP:
- If
isThreadSafe=truethen the JSP container may choose to dispatch multiple outstanding client requests to the page simultaneously. Page authors usingtruemust ensure that they properly synchronize access to the shared state of the page. - If
isThreadSafe=falsethen the JSP container shall dispatch multiple outstanding client requests, one at a time, in the order they were received, to the page implementation for processing.
Note that even if the isThreadSafe attribute is false the JSP page author must ensure that accesses to any shared objects are properly synchronized., The objects may be shared in either the ServletContext or the HttpSession.
Most Recent jsp Faqs
- How to get the real requested URI from inside the error page?
- Can I define a method in a JSP page?
- What is the difference between request.getParameter() and request.getAttribute()?
- What is difference between page and pageContext in JSP pages?
- How to config a JSP file in web.xml?
- What is the difference between jsp:forward and response.sendRedirect?
- How to retrieve values from HTML form input elements in JSP?
Most Viewed jsp Faqs
- How to config a JSP file in web.xml?
- What is difference between page and pageContext in JSP pages?
- How to disable browser caching for a specific JSP?
- How to retrieve values from HTML form input elements in JSP?
- What is the difference between jsp:forward and response.sendRedirect?
- What is the difference between request.getParameter() and request.getAttribute()?
- How to get the current JSP page name?