In JSP, the isThreadSafe attribute of page directive is used to indicate if your code is thread safe or not.
The isThreadSafe attribute can be given either of the two boolean values: true or false.
Syntax of isThreadSafe:
<%@ page isThreadSafe = "true|false" %>
isThreadSafe = "true"
As you know that every JSP page eventually converts to its equivalent Servlet. Hence, when the isThreadSafe attribute is set to true,
it indicates that your JSP code is thread safe and can be simultaneously accessed by multiple threads.
Note - Setting the isThreadSafe attribute to trueonly indicates but doesn't make a JSP page thread safe.
In order to ensure a thread safe access to a JSP page, the programmer will still have effectively use the synchronized keyword
to ensure synchronized access of the JSP page
by multiple threads that are wishing to execute it simultaneously.
isThreadSafe = "false"
When the isThreadSafe attribute is assigned a false boolean value, the servlet resulting from the JSP page will implement
the SingleThreadModel interface. It indicates that your code is not thread
safe and hence it should not be concurrently accessed by multiple threads.
Advertisement
isThreadSafe example
In the upcoming example, we are setting the isThreadSafe attribute to false to indicate that our JSP page is not thread safe.
ThreadSafe.jsp
<!–– Example of isThreadSafe attribute of page directive of JSP -->
<html>
<head>
<title>JSP isThreadSafe Attribute</title>
</head>
<body>
<%@ page isThreadSafe = "false" %>
<b>Setting isThreadSafe attribute to false indicates that this JSP page is not thread safe
and should not be concurrently accessed by multiple threads.</b>.
</body>
</html>
By executing the above mentioned JSP web page will indicate the container that JSP page is not thread safe.
Note
By default, the value of isThreadSafe Attribute is true, which indicates that your JSP web page is thread safe.