Reputation: 5042
JSP is thread safe by default but What is the meaning when we say
a JSP is thread-safe
?
Upvotes: 4
Views: 565
Reputation: 2070
JSP is not thread safe in general! JSP is compiled into servlet and one instance can be used to serve multiple requests, so every class field (modified during the request execution) is considered not being thread safe. How can you declare class field in JSP? Using JSP declaration:
<%! private Object notThreadSafe = new Object(); %>
(BTW you can even declare methods in JSP declarations).
JSP can be thread safe, if you don't use these JSP declarations. Everything else (html/jsp tags, scriptlet code <% /* some code*/ %>
, jsp expression <%= "some expression" %>
,... ) is being compiled into jspService method as Koitoer has mentioned.
Upvotes: 1
Reputation: 19533
When a jsp is create is become a servlet in the application server. All the logic runs from jspService method, all the references or variables you have in your jsp become local variables, that is why jsp can be considered thread safe by default.
Check this link to review the jsp lifecycle .
At the end all the code that you have in the jsp will be within the _jspService method. All the content of your JSP will be inside.
public void _jspService(HttpServletRequest request,
HttpServletResponse response)
throws java.io.IOException, ServletException {
Upvotes: 2