"Wasetis, Ken" wrote:
>
> Unless you specify the page directive isThreadSafe to be 'false', the JSP
> context will create only one instance per page (for all threads to use), so
> if one thread changes an instance variable, all threads will see the same
> value.  If you specify isThreadSafe to be 'false', each thread will get its
> own instance for the page and therefore its own copy of the instance
> variables.  This isn't very efficient, though, obviously.  Consider that if
> you did it on a Logon page, every user trying to logon would create a new
> instance of the Logon.jsp- yikes.

Ken is right about why using <%! ... %> to declare a variable is dangerous,
and almost right about isThreadSafe. When you specify isThreadSafe as 'false',
the container guarantees that only one thread at the time executes the servlet
code corresponding to your page.

How it fulfills this guarantee varies though. It can serialize access to a
single servlet instance, with the effect that only one user at a time can access
the "page". It's common, though, that the container creates a number of
instances
of the servlet to be able to handle more than one request at the same time while
still fulfilling the guarantee. I have never seen a container that creates a
unique instance per request, like Ken suggests.

So while isThreadSafe="false" may help with the concurrency problem in this
particular case, it has performance degradation problems. More important,
there's no reason why the variable in the example that started this thread
should be an instance variable. The JSP spec may fool you to believe you
*have* to use <%! ... %> to declare variables but that's not the case. You can
declare local variables (unique per request, so no concurrency problems), which
is what you want in most cases, in a scriptlet block. Or even better in my
opinion,
avoid code in the page altogether and use the standard and custom actions with
beans instead.

Hans

> -----Original Message-----
> From: Frank Apap [mailto:[EMAIL PROTECTED]]
> Sent: Monday, May 15, 2000 2:45 PM
> To: [EMAIL PROTECTED]
> Subject:
>
> Hi, I am a little curious what you mean about the <%! tag.  I am currently
> using it and want to make sure I am not using it incorrectly.  In what cases
> would it be a problem.  I am now using it to declare my variables, so are
> you saying that if 1 user loads a jsp and the variable declared is set to
> something if another user loads the page he can change user1's variable? Im
> a little confused.
>
> - Frank
> ----- Original Message -----
> From: Hans Bergsten <[EMAIL PROTECTED]>
> To: <[EMAIL PROTECTED]>
> Sent: Monday, May 15, 2000 2:51 PM
>
> > "Wasetis, Ken" wrote:
> > >
> > > Or define it within <%! and %> tags which should make it an instance
> > > variable in the generated servlet.
> >
> > That's a dangerous solution; since <%! ... %> creates an instance
> variable,
> > the variable is shared by all threads (i.e. all concurrent accesses to the
> > page). In this case the variable value depends on request data, so if you
> > use an instance variable concurrent requests will overwrite each others
> > values.
> >
> > A better solution here would be to develop a bean that has a write
> > access property named "selComputers" (set from the request parameter
> > with the same name using <jsp:setProperty>) and read-only properties
> > named "computerName" and "computerDescription". Then all code can be
> > replaced with action elements:
> >
> >   <jsp:useBean id="compInfo" class="com.mycomp.SomeClass" >
> >     <jsp:setProperty name="compInfo" property="*" />
> >   </jsp:useBean>
> >
> >   <jsp:getProperty name="compInfo" property="computerName" />
> >   <jsp:getProperty name="compInfo" property="computerDescription" />
> >
> > Hans

--
Hans Bergsten           [EMAIL PROTECTED]
Gefion Software         http://www.gefionsoftware.com

===========================================================================
To unsubscribe: mailto [EMAIL PROTECTED] with body: "signoff JSP-INTEREST".
Some relevant FAQs on JSP/Servlets can be found at:

 http://java.sun.com/products/jsp/faq.html
 http://www.esperanto.org.nz/jsp/jspfaq.html
 http://www.jguru.com/jguru/faq/faqpage.jsp?name=JSP
 http://www.jguru.com/jguru/faq/faqpage.jsp?name=Servlets

Reply via email to