5

I learn jsp. I have great confusion in isThreadSafe attribute in jsp. By default Jsp is not thread safe then isThreadSafe= true

if we set isThreadSafe=false means the JSP engine makes sure that only one thread at a time is executing your JSP.

i have confusion in true or false. isThreadSafe = false means only muliple thread can access jsp by means of isThreadSafe

what is the meaning of isThreadSafe

     The isThreadSafe option marks a page as being thread-safe.
 By default, all JSPs are considered thread-safe. If you set the isThreadSafe option to false, the JSP engine makes sure that only one thread at a time is executing your JSP.

        The following page directive sets the isThreadSafe option to false:
    <%@ page isThreadSafe="false"  %>

thread-safe means multiple threads ca not access the jsp page at a time is it correct?

jackyesind
  • 3,343
  • 14
  • 47
  • 74
  • 1
    SingleThreadModel interface has been deprecated in the Servlet Specification release 2.4: http://stackoverflow.com/questions/3572712/how-can-i-create-thread-safe-jsp-page. – Vadzim Jan 12 '16 at 21:03

4 Answers4

7

This attribute support the facility of maintaining thread for sending multiple and concurrent requests from the JSP container to the JSP page if you specify the true value of the attribute otherwise if you specify the false value of the attribute then the JSP container can send only one request at one time. The default value of the attribute is true.

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.

By default, the servlet container considers the JSP page code safe for reuse in a single instance by multiple request threads. If the page's code risks unintentional sharing its state across simultaneous requests, the following directive will cause the servlet container use separate instances of the page in each request:

<%@ 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. When the code in the JSP avoids holding a state (such as member and session variables), the servlet container can rely on the default "true" value of the "isThreadSafe" attribute to reply faster and with a smaller memory footprint.

For example, if you happen to use <%! %>, this will place the code in the class level and not in the _jspService method. Introducing class members into the JSP opens a way to lose thread safety in the single instance usage.

If your JSP is not thread-safe as a single instance serving parallel requests you will have to add the isThreadSafe=false in order for things to work correctly. This will preserve the web application's thread safety by instructing the servlet container to work around the thread-unsafety of the JSP, at a cost:

If isThreadSafe=true then the JSP container may choose to dispatch multiple outstanding client requests to the page simultaneously. Page authors using true must ensure that they properly synchronize access to the shared state of the page.

If isThreadSafe=false then 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.

eel ghEEz
  • 1,186
  • 11
  • 24
Freak
  • 6,786
  • 5
  • 36
  • 54
  • 1
    So in one way does setting "isThreadSafe=false" mean saying to the conaitner - "Dear jsp container, i am not sure about the thread safety of my jsp page. please, dispatch client request one at a time" ? – Dexter May 12 '16 at 14:11
  • Note `SingleThreadModel` has been deprecated since Servlet 2.4 and is being removed in Servlet 6.0. This has repercussions on JSP and `isThreadSafe` as it becomes a no-op for the future (and that JSP directive is slated for removal as well) – Joakim Erdfelt Sep 21 '21 at 14:20
3

By default jsp pages are not thread safe. By default:

"<%@ page isThreadSafe="true" %>"

By implementing the SingleThreadModel interface. When you declare this in jsp page

"<%@ page isThreadSafe="false" %>"

it means jsp container will take of multiple requests, and only one request can send at one time.

But when you declare any variable in the same using:

"<%! DECLARATION %>"

It means you set "isThreadSafe = true". means this variable is not thread safe .. and jsp container doesn't have control over this variable. due to this reason SingleThreadModel gets failed.

thats why SingleThreadModel is not recommended for normal use. There are many pitfalls, including the example above of not being able to use <%! %>

User_86
  • 265
  • 1
  • 4
  • 13
1

JSPs by default are not automatically thread safe. If you have instance variables that are modified from within the service section of a JSP, they will be shared by all requests causing concurrency issues. This is the same for servlets. Like with servlets, there is a mechanism that was supposed to help make your JSPs more thread safe this is the isThreadSafe property which can be set in a JSP page directive. This technique is akin to implementing the deprecated SingleThreadModel interface with a servlet. In fact, most containers implement thread safe JSPs by having the generated code implement this deprecated interface.

For servlets, this turned out not to be the greatest idea which is why SingleThreadModel is deprecated. It was not a good idea because, even when using it, it was possible to write a servlet that is not thread safe.

So, in short, you should avoid relying on the threadSafe directive in JSPs for the same reason that you should avoid using the deprecated SingleThreadModel interface in your servlets.

Avoiding scriptlets altogether is the currently accepted best practice. JSPs with no scriptlets will have no instance variables and will take you a long way toward having thread safe JSPs.

PSR
  • 39,804
  • 41
  • 111
  • 151
-1

in jsp your using local variables always jsp's are thread safe supose you are using instance variables jsp,s are not thread safe. you must specify isThreadSafe

vijay
  • 1