JSP isThreadSafe by default

I am learning jsp. I have a lot of confusion about the isThreadSafe attribute in jsp. By default, Jsp is not thread safe, and then isThreadSafe= true

if we set isThreadSafe=false , then the JSP engine ensures that only one thread at a time runs your JSP.

I have confusion in the true or false. isThreadSafe = false means only muliple thread can access jsp using 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 do not have access to the jsp page at a time, is this correct?

+4
source share
4 answers

This attribute supports a flow maintainer to send multiple and simultaneous requests from a JSP container to a JSP page, if you specify a true attribute value, otherwise if you specify a false attribute value, then the JSP container can send only one request at a time. The default value for the attribute is true.

A threaded JSP / servlet is one that works correctly when more than one thread is running at a time. To make your JSP threads safe, you can implement the SingleThreadModel interface, which prevents two threads from simultaneously accessing the service method.

By default, the servlet container considers the JSP page code safe for reuse in a single instance by multiple request flows. If the page code is subject to inadvertent sharing of its state on simultaneous requests, the following directive will force the servlet container to use separate page instances in each request:

 <%@ page isThreadSafe="false" %> 

Moreover, instead of a single servlet instance generated for your JSP page loaded into memory, you will have N servlet instances loaded and initialized, with an efficient servicing method for each instance synchronized. You can usually control the number of instances (N) created for all servlets that implement SingleThreadModel through the admin screen for your JSP engine.

Regardless of whether your JSP is thread safe or not, this is a consequence of how you implemented your JSP. When the code in the JSP avoids holding state (such as member and session variables), the servlet container can rely on the default "true" of the isThreadSafe attribute to respond faster and with less memory.

For example, if you use <%! %> <%! %> , this will put the code at the class level, and not in the _jspService method. Representing class members in JSP opens up a way to lose thread safety when using a single instance.

If your JSP is not thread safe since one instance serving concurrent requests, you need to add isThreadSafe=false for everything to work correctly. This will keep the web application flow safe by instructing the servlet container to work with an unsafe JSP thread at a cost:

If isThreadSafe = true, then the JSP container can simultaneously send several outstanding client requests to the page. Authors of pages using true must ensure that they correctly synchronize access to the overall state of the page.

If isThreadSafe = false, then the JSP container should send several outstanding client requests, one in the order in which they were received, to the implementation of the page for processing.

Please note that even if the isThreadSafe attribute is false, the author of the JSP page must ensure that access to any shared objects is correctly synchronized. Objects can be shared either in ServletContext or in HttpSession.

+6
source

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

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

Using the SingleThreadModel Interface. When you declare it on the jsp page

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

this means that the jsp container will accept multiple requests, and only one request can send at a time.

But when you declare a variable in the same use:

"<%! DECLARATION%>"

This means that you are setting "isThreadSafe = true". means that this variable is not thread safe .. and the jsp container has no control over this variable. for this reason SingleThreadModel fails.

This is why SingleThreadModel is not recommended for normal use. There are many pitfalls, including the example above, unable to use <%! %>

+2
source

JSPs are not automatically streamed by default. If you have instance variables that have been modified from the JSP service section, they will be available for all requests that cause concurrency problems. For servlets, this is the same. As with servlets, there is a mechanism that should help make your JSPs more thread safe, this isThreadSafe property, which can be set in the JSP page directive. This method is similar to implementing the deprecated SingleThreadModel interface with a servlet. In fact, most containers implement thread-safe JSPs because the generated code implements this legacy interface.

For servlets, this was not the biggest idea, so SingleThreadModel is deprecated. This was not a good idea, because even with it you could write a servlet that is not thread safe.

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

Avoiding scenarios in general is currently the best practice. JSPs without scriptlets will not have instance variables, and you have a long way to go to ensure reliable JSP streams.

+1
source

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

-1
source

All Articles