Hi all.

Some days ago I reported an error on JDBC and JSP in recursion. Just to remind 
everyone...

I had a JSP page make a JDBC connection to PostgreSQL database. The database contained 
a table which held a hierarchy (n-tree) and another that held "leaf nodes" (our 
company, it's organizational units and e-mail users). The JSP page is capable of 
displaying all e-mail users for a given org_unit, no problem there. Then I added 
option to display only partial HTML, just the portion of the table, with the idea to 
include it in another request. No problem there, either.

The problem occured when I tried to recursively include the same page in order to 
display the entire sub-tree for a given node. I was getting unexplicable error, mostly 
JDBC.

SOLUTION
-------------

I have tested a standalone Java, which worked and a local method inside JSP, which 
also worked!!! That led me to the source of the problem. The only difference between 
my attempt to do a recursive inclusion in a method (which worked) and recursive 
inclusion with <jsp:include> was in the placement of JDBC variables.

In case of the method, they were local to the method. In case of the <jsp:include> 
they were global for the page, inside <%!  %> section. Once I moved the declarations 
to the scriptlet <%  %> portion, it all started working.

So, this deosn't work in a recursion:

<%!
Connection conn;
Statement stat;
ResultSet rs;
%>
...
<%
conn = DriverManager.getConnection( "jdbc://postgresql/www", "user", "pass" );
stat = conn.createStatement();
rs = stat.executeQuery( querySubOU + id );
%>
<jsp:include page="<%= \"page.jsp?partial=true&id=\" + subID %>"/>

But this does work:

<%!
...
%>
<%
Connection conn;
Statement stat;
ResultSet rs;
conn = DriverManager.getConnection( "jdbc://postgresql/www", "user", "pass" );
stat = conn.createStatement();
rs = stat.executeQuery( querySubOU + id );
%>
<jsp:include page="<%= \"page.jsp?partial=true&id=\" + subID %>"/>

Could anyone explain why?

Variables within <%!  %> section are global to the instance of the servlet class or 
(if declared as "static") global to the whole class. Since I didnot declare them as 
"static", they should have been used per-instance. Funny thing is, the error was not 
occuring upon exit from the recursion (as I expected), but upon *second* entering of 
recursion.

QUESTION
-------------
If a servlet (JSP) tries to include itself, will Tomcat create two instances of the 
servlet's class? From my point of view, it should, since the caller has not finished.

Can anyone shed some light on the matter?

Nix.


Reply via email to