So Craig is it okay to declare variables in a JSP (version .91) using <% int
var = 0; %>?? I want each thread to have its own vars.

Cheers

Marc

-----Original Message-----
From: Craig R. McClanahan [mailto:[EMAIL PROTECTED]]
Sent: Tuesday, 20 June 2000 12:40 PM
To: [EMAIL PROTECTED]
Subject: Re: runat="server"?


Marc Krisjanous wrote:

> Thats interesting,
>
> But in the case of the JSP when compiled by websphere my code is contained
> within the service method including the vars.  From my understanding of
your
> email I would have to make the service method synchronized. What are your
> thoughts??
>

Variables that are local to the service() method -- or any other method in
Java
classes, for that matter -- are stored on a per-thread stack.  This means
that
there is no conflict between the local variables when two or more users are
executing the same page at the same time.

Instance variables, on the other hand, exist once per page.  This is nice if
you
really want to share information between all requests that use this page,
but it is
not what you want when dealing with the variables related to a particular
request.

Note that when you create a bean variable with <jsp:useBean> in JSP 1.x (or
<bean>
in previous versions), this variable is local.  That's why you don't have
problems
with multiple users scribbling on it.

Craig McClanahan


>
> -----Original Message-----
> From: Jim Bailey [mailto:[EMAIL PROTECTED]]
> Sent: Tuesday, 20 June 2000 9:57 AM
> To: [EMAIL PROTECTED]
> Subject: Re: runat="server"?
>
> Yes, correct. That is why you shouldn't use that form of declaration for
> anything except thread safe variables. In the example I gave it is a
static
> final which is guaranteed thread safe since it is a constant. If you do
use
> class variables in your JSP/Servlet then you will probably have to use a
> single thread model of JSP which isn't supported well in Websphere 2.0x
and
> JSP 0.91 or you will have to synchronize any method that uses it and
> anyplace in the JSP that uses the variables.
>
> It isn't any better with the <%! form in JSP 1.x. As a developer you have
to
> be careful to make sure your JSP is thread safe. The easy way to do that
is
> to use the <%@ page isThreadSafe="false" %> switch. The problem with that
is
> it is very inefficient compared to the normal isThreadSafe="true"
> implementation. Basically your whole JSP is either one user at a time or
> each JSP invocation gets a new instance of the servlet, depending on the
> implementation of the servlet/JSP engine.
>
> -----Original Message-----
> From: Marc Krisjanous [mailto:[EMAIL PROTECTED]]
> Sent: Monday, June 19, 2000 7:44 PM
> To: [EMAIL PROTECTED]
> Subject: Re: runat="server"?
>
> but wouldn't that cause data corruption since the variable is an Instance
> class variable and all threads could access it, possibly at the same time.
> You would have to synchronized all methods that used the vars.
>
> Marc
>
> -----Original Message-----
> From: Jim Bailey [mailto:[EMAIL PROTECTED]]
> Sent: Tuesday, 20 June 2000 3:41 AM
> To: [EMAIL PROTECTED]
> Subject: Re: runat="server"?
>
> As far as I can tell, the first example shouldn't be valid. It should
create
> a syntax error: "illegal modifier on local variable" or something like
that.
>
> The second example is valid on JSP 0.91 and Websphere 2.0x for sure but I
> don't know how "standard" it is. It puts the variable myInt as a class
> static variable on the servlet generated from the .JSP file. It is
> equivalent to the following:
>
> public class _int_test__jsp extends HttpServlet
> {
>   private static final int myInt = 0;
>
>   public void
>   _jspService(HttpServletRequest request, HttpServletResponse response)
>         throws IOException, ServletException
>   {... etc
> }
>
>
===========================================================================
> 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
>
>
===========================================================================
> 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

===========================================================================
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

===========================================================================
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