• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Liutauras Vilda
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Saloon Keepers:
  • Scott Selikoff
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
  • Frits Walraven
Bartenders:
  • Stephan van Hulst
  • Carey Brown

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

 
Ranch Hand
Posts: 389
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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>

 
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 297
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
Ranch Hand
Posts: 124
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Ranch Hand
Posts: 371
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
author
Posts: 3252
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 3252
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
 
Ranch Hand
Posts: 84
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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...................


------------------
 
Bhupinder Dhillon
Ranch Hand
Posts: 124
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 389
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 3252
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 389
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 3252
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
 
joke time: What is brown and sticky? ... ... ... A stick! Use it to beat this tiny ad!
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic