isThreadSafe JSP page directive with Example

isThreadSafe JSP page directive with Example


"isThreadSafe" is one of the 14 attributes page directive support. This attribute takes boolean values of true or false indicating the JSP page can be safe in multithreaded environment or not. The default value is true.
Syntax of isThreadSafe JSP

<%@ page isThreadSafe="true" %>

When set to true, the same JSP page can honour multiple clients. What does it mean? For better understanding, I explain through Servlets because a JSP file is internally converted to a Servlet. When a client requests a JSP file, the container loads the JSP file, translates it to a Servlet, compiled and then executed. For multiple clients’ requests also, the container creates multiple objects of the same Servlet and honours. For each Servlet object, a separate service() method is created. That is, each client will have a separate service() method. For this reason, Servlets and JSP are implicitly thread safe.

Then what is the meaning of isThreadSafe="false" when Servlets and JSP are thread safe automatically?

If the Programmer would like to create only one object for the Servlet/JSP (and not multiple objects), then declare false. That is, multiple clients request the same JSP (or Servlet), multiple files of the same JSP are loaded and executed. This decreases the performance. This type coding is required when the data is very important and critical like money transfer operations in banks or honoring military top secret data etc.

Synopsis:

1. isThreadSafe="true", creates multiple objects for the same JSP file when requested by multiple clients. Each client is served with a separate _jspService() method (with only one JSP file loaded).

2. isThreadSafe="false", allows the container to create one Servlet object for each client requesting the same JSP. Multiple clients will have multiple Servlet objects created by the container to honour all the clients.

This is implemented in Servlets using SingleThreadModel interface.

Note: isThreadSafe is understood and interpreted by many Programmers differently (just in opposite way). I stick to this.

14 Page Attributes and Examples

  1. import
  2. contentType
  3. errorPage, isErrorPage, exception
  4. pageEncoding
  5. buffer
  6. autoFlush
  7. info
  8. session
  9. isThreadSafe
  10. language
  11. extends
  12. isELIgnored

Note: No 3 attribute contains 3 attributes.

Leave a Reply

Your email address will not be published. Required fields are marked *