aspose file tools*
The moose likes Servlets and the fly likes how to pass the result of a query executed in a include JSP to the main JSP Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "how to pass the result of a query executed in a include JSP to the main JSP " Watch "how to pass the result of a query executed in a include JSP to the main JSP " New topic
Author

how to pass the result of a query executed in a include JSP to the main JSP

ravi janap
Ranch Hand

Joined: Nov 04, 2000
Posts: 389
I have a main JSP page which includes another JSP page say CSInclude.jsp.
Is there some way of executing a select query in CSInclude.jsp and pass the resultset to the the main JSP
<jsp:include page="CSInclude.jsp" flush="true">

</jsp:include>


SCJP, SCJD, SCWCD, SCBCD, SCEA
sameera kocharekar
Greenhorn

Joined: Apr 23, 2001
Posts: 23
hi
well i think yes u can do it
in the include file code if u declare string varibles and assisgn them the values which u retrieve from the select query and then access those strings directly in the main jsp file u would be able to do it
just try
ok bye sameera


SCJP
Michael Hildner
Ranch Hand

Joined: Oct 13, 2000
Posts: 297
Interesting question. Since the ResultSets in the resulting servlets are both Objects, they might just refer to the same thing. Maybe just use a bean?
Bhupinder Dhillon
Ranch Hand

Joined: Oct 12, 2000
Posts: 124
Originally posted by janapareddy ravindra:
I have a main JSP page which includes another JSP page say CSInclude.jsp.
Is there some way of executing a select query in CSInclude.jsp and pass the resultset to the the main JSP
<jsp:include page="CSInclude.jsp" flush="true">

</jsp:include>

Yes you can include it as an attribute in the HttpServletRequest but you would have to close the ResultSet in the main jsp which IMHO, makes a very crappy design.
Cameron Park
Ranch Hand

Joined: Apr 06, 2001
Posts: 371
When you declare variables in jsp files that are to be included in another jsp file, those variables are included in the main jsp file as if they are declared there.
Peter den Haan
author
Ranch Hand

Joined: Apr 20, 2000
Posts: 3252
Originally posted by Cameron Park:
When you declare variables in jsp files that are to be included in another jsp file, those variables are included in the main jsp file as if they are declared there.

You are confused between static inclusion (<%@include %> ) and dynamic inclusion (<jsp:include /> ).
- Peter
Peter den Haan
author
Ranch Hand

Joined: Apr 20, 2000
Posts: 3252
Originally posted by Bhupinder Dhillon:
Yes you can include it as an attribute in the HttpServletRequest but you would have to close the ResultSet in the main jsp which IMHO, makes a very crappy design.

Very much in agreement there.
There are three possibilities IMHO.

  • Generate the HTML for the ResultSet in your included JSP.
  • If you cannot do that, why the h*** is it a JSP in the first place? In that case you have difficulties because are trying to do the Wrong Thing. Refactor the code into a Java class instead.
  • If you really, really must do this, then cache the output of the ResultSet into a bean, so you can close the database connection in the finally clause of your included JSP. If you have a full JDBC 2.0 implementation you can use a disconnected RowSet like a WebRowSet.

  • - Peter
ravindra janapreddy
Ranch Hand

Joined: Jan 30, 2001
Posts: 84
Thanks everyone.
My CS application has several JSPs. What I am trying to achieve is seperate the business logic from each of the JSP's and put it in a seperate JSP which I want to call CSInclude.jsp whose job would be take care of querying , exception handling and stuff like that. Thus each of the earlier JSPs would only be displaying HTML tags and nothing more than that.
I understand JavaBeans and Java classes are a solution but I why I am trying to do like this because this is what I have been asked to do i.e use a seperate JSP for this purpose.
If I can access the variables declared in the included JSP in the main JSP like a local variable does that also mean I can access the methods declared in the included JSP in the main JSP ? Do I pose this question seperately in the forum ?
If I consider the reply in positive then I can think of having one seperate method to perform the business logic for each of these JSPs , put all these methods in a common JSP file called CSInclude and include this common JSP in all the JSPs of the CS application call the respective methods in the respective JSPs.

Any thoughts to share on this...................


------------------

SCEA, SCBCD, SCWCD, SCJD, SCJP
Bhupinder Dhillon
Ranch Hand

Joined: Oct 12, 2000
Posts: 124
I think what you really need is tag library. Instead of trying to call a method in included jsp (which BTW is probably not possible), you would simply need to call a tag and it will query the database and print out the results directly in the calling jsp.
[This message has been edited by Bhupinder Dhillon (edited April 24, 2001).]
ravi janap
Ranch Hand

Joined: Nov 04, 2000
Posts: 389
If we can access the variables declared in the included JSP in the main JSP as told by Sameera and Cameron Park why can't we access the methods declared in the included JSP in the main JSP?
Peter den Haan
author
Ranch Hand

Joined: Apr 20, 2000
Posts: 3252
Originally posted by ravindra janapareddy:
I understand JavaBeans and Java classes are a solution but I why I am trying to do like this because this is what I have been asked to do i.e use a seperate JSP for this purpose.

I have a hard time believing that, unless the idea is to give you hands-on experience in how not to do it (which might be very instructive, come to think of it).
If I can access the variables declared in the included JSP in the main JSP like a local variable does that also mean I can access the methods declared in the included JSP in the main JSP ?

No you can't do any of this, not with the inclusion method you are using. That writer was mistaken. It is, however, possible to do with a static include (<%@include ... %> ), which is much like a C #include. Maybe that is what you are looking for?
- Peter
ravi janap
Ranch Hand

Joined: Nov 04, 2000
Posts: 389
okay then, I will include this common JSP using the static include <%@ include ... %> and call the methods .
However , What I would like to know -- Is this approach wrong from design point of view ?
Peter den Haan
author
Ranch Hand

Joined: Apr 20, 2000
Posts: 3252
Originally posted by janapareddy ravindra:
However , What I would like to know -- Is this approach wrong from design point of view ?

Oh boy, where to start.

  • Using static includes is just a glorified way to cut and paste code. There is no clearly defined interface or contract between the JSP and the included code. It's a bit of a throwback to the BASIC spaghetti days. Static includes are tolerable for snippets of presentation and maybe the odd security call, but not for code.
  • This is pure Java code, which makes it ill suited to implementation in a JSP. JSPs have been designed to contain mainly presentation code (text, HTML, XML,...) with tiny bits of embedded Java code (in fact, taglibs are an attempt to get all the Java code out of JSPs). Appropriate places to put pure Java code are plain Java classes, JavaBeans, and tag libraries.
  • The data access code becomes much harder to test. Unit testing frameworks for JSPs test their HTML output. In fact, you would want to unit test the data access code separately, as a Java class, on the method level.
  • The code is replicated as many times as you have JSPs, wasting perfectly good memory. When you isolate it inside a class (, bean, tag), there is only one copy present in your application.

  • HTH
    - Peter
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: how to pass the result of a query executed in a include JSP to the main JSP