use of - isThreadSafe in jsp

Hi All, There is something like below in jsp <%@ page language="java" contentType="text/html" isThreadSafe="true|false"%> what does this mean. Documentation says if it is true (which is by default), this page can service multilple request at a time. If it is false, then it is SingleThreadModel. Now i want to know how this will affect for a web site page. Means a page is simulteanously opened by many users, what will be the result. Do I have to make it SingleThreadModel. Though I am using bean like : <jsp:useBean id="id" class="BeansMultiLang.RandomNoVerifier" /> Whose scope is 'page' (by default). Moreover methods in this java class are synchronized. But still I am not sure what will happen with my site. Is it safe or not. Whether variables / results on a jsp page may be intermingled / wrong if it is not in single thread model. can any one explain me & remove my confusions & worries. Thanks, Manoj

I suspect this topic has been discussed in detail before in a previous post, but I will give you a brief answer. Keep in mind that writing a JSP is really just a convenient way of writing a Servlet. Your Web server converts the JSP into a Servlet and includes a service() method (actually named _jspService()). This method is invoked each time a request is made for your JSP. If multiple requests occur simultaneously, then the _jspService() is invoked in separate threads IF you have the isThreadSafe directive set to "true", which is the default behavior. If you have a variable that has page scope, then it is likely being shared amongst clients. If your page-scoped bean can't be shared, then you want to declare isThreadSafe as "false". This causes the resulting Servlet to implement the SingleThreadModel interface, in which case the Web server ensures that the service() method is not invoked in multiple threads. How? By instantiating new Servlet objects when multiple requests occur simultaneoulsy. I hope this helps. [ July 29, 2003: Message edited by: Rich Raposa ]

Here are some hints: Check each static field and method in your bean. Static methods have the possibility of being executed simultaneously from two threads, each processing a different request. Check any scriptlet code on your page between <%! and %>. Any objects declared or manipulated in between these marks can also be executed similtaneously across two threads. If your bean is truly page scope, and your scriptlets are all between <% and %>, then there might not be any need to mark the JSP as not thread safe.

Manoj Tyagi
Ranch Hand

Joined: Jun 11, 2002
Posts: 35

posted Jul 30, 2003 01:33:00

0

Dear Richa & Micheal, Thanks for your valuable reply. My beans are of page scope(which is by default) & my coding & varibales are in <% %> only. I have nothing in <%! %>. isTheradSafe is true, which is by default. Should i assume that my pages are safe for concurrent accesses (as Micheal wrote). Richa you told something about SingleThreadModel. My code is as below... <%@ page language="java" contentType="text/html" %> <%@ page import="java.util.*" %> <jsp:useBean id="id" class="BeansMultiLang.RandomNoVerifier" /> //id is by default of page scope <% String name = request.getParameter("name"); Connection conn = id.getConnection(); ................. All coding %> Now when _jspService is created , does it mean like........ void _jspService(HttpServletRequest rq, HttpResponse rs) { BeansMultiLang.RandomNoVerifier id = new BeansMultiLang.RandomNoVerifier(); Connection conn = id.getConnection(); String name = request.getParameter("name"); } Is my interpretation correct. Now I have to take care whether these resources are shared concurrently or not. Need your further explanation. Thanks, Manoj

Yes, that should be thread safe. Any declarations or statements inside <% .. %> get put into the body of _jspService() . Each thread that invokes your JSP gets its own separate invocation of that method, with its own local variables. Also, page-scoped beans are not shared across threads.