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?
Is it OK to place non-static atributes of the class into "global declaration" section
or should I confine them to being local variables of the "service()" method?
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? And suppose in one invocation the instance
modifies "depth" and "another" variables, would it reflect in the other invocation?
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.
In other words, is it appropriate to place non-static atributes of the JSP/Servlet in
<%! %> section?
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.
TYIA,
Nix.