> -----Original Message-----
> From: Nikola Milutinovic [mailto:[EMAIL PROTECTED]]
> Sent: Wednesday, December 26, 2001 8:45 AM
> To: Tomcat Users List
> Subject: Clarification needed, please
>
>
> Hi all.
>
> After some testing I have reached a certain conclusion and I
> would like to confirm it. This all emerged after my long
> trail on recursive servlets
>
> SHORT FORM
> ----------------
>
> If Tomcat gets two requests, handled by the same servlet,
> will the same instance of that servlet handle it?
Yes. Tomcat will always create exactly one instance of a servlet
for each unique URL that can access it (i.e. two servlet mappings
equals two instances).
> Is it OK to place non-static attributes of the class into
> "global declaration" section or should I confine them to
> being local variables of the "service()" method?
NOOOOOOOOOOOO!. If you don't want to receive threading problems that
become very difficult to diagnose, don't put anything as a static (or
instance for that matter) variable. Only use local variables unless you
know what you are doing (i.e. they can be used to store configuration
information, but be aware of issues involved when one thread changes
the configuration while another tries to use it).
>
> LONG FORM
> ---------------
>
> Let's say I have a servlet defined by the JSP, like this:
>
> <%@ page
> ...
> %>
> <%!
> Connection conn;
> Statement stat;
> ResultSet rs;
> int depth;
> %>
> <%
> int another;
> ..
> %>
>
> This translates to:
>
> public class myPage$jsp extends HttpJspBase {
> ...
> Connection conn;
> Statement stat;
> ResultSet rs;
> int depth;
> ....
> public void _jspService(HttpServletRequest request,
> HttpServletResponse response) {
> int another;
> ...
> }
> }
>
> OK. So, suppose two (simultaneous) requests arive for this
> URL. Will there be one instance of the class to handle them?
Yes.
> And suppose in one invocation the instance modifies "depth"
> and "another" variables, would it reflect in the other
> invocation?
This depends upon how many CPUs your machine has and when Java
would perform the context switch between the threads. Depth could
be the value for either thread, depending on when the switch occurs
or who was the last to modify it. another will always be the correct
value.
> Or, better yet, since, the class will open a JDBC
> connection, it will be opened twice, *but will be stored in
> the same variable*. Effectively, the first one will be
> deleted by the garbage collector and closed. I sense a
> dangerous situation here.
Yes.
>
> In other words, is it appropriate to place non-static
> atributes of the JSP/Servlet in <%! %> section?
No.
>
> You can imagine how this relates to a recursive Servlet.
> Right now I'm seeing this behavior in plain sight. I was just
> wandering what is the recomendation or correct practice.
You could define a method in the <%! %> and pass the entire context
in as parameters and then call that method from your <% %> code blocks.
You could also define a class in the <!% %> code blocks that could
hold your state and define the method to recurse on.
>
> TYIA,
> Nix.
>
--
To unsubscribe: <mailto:[EMAIL PROTECTED]>
For additional commands: <mailto:[EMAIL PROTECTED]>
Troubles with the list: <mailto:[EMAIL PROTECTED]>