Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JSP isThreadSafe default

Tags:

jsp

jakarta-ee

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?

like image 225
jackyesind Avatar asked Mar 01 '13 10:03

jackyesind


2 Answers

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.

like image 193
Freak Avatar answered Sep 21 '22 11:09

Freak


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 <%! %>

like image 33
User_86 Avatar answered Sep 18 '22 11:09

User_86