We help IT Professionals succeed at work.

JSP how to retrieve a return value from an include?

S_B
S_B asked
on
hi!!!

i'm including an jsp-site
with <jsp:include page="mypage.jsp" flush="true"/>

how can i get some values from this include???
that means: i'm processing something in the include and want to return it to the site where i include it...

how can i manage this, or how can i solve this problem in  different way?

i'm running jsp on tomcat 3.21

thx ahead s.

Comment
Watch Question

Commented:
You can use session variables. Use this in the include JSP:

request.getSession().setAttribute("yourVariableName", yourValue);

And use this in the JSP after <jsp:include page="mypage.jsp" flush="true"/>:

yourValue = request.getSession().getAttribute("yourVariableName");

Good luck!

Commented:
I think you can use:
<%@ include file=File %>  or <jsp:directive.include file=File %>

for more info check out:
http://www.jspinsider.com/reference/jsp/jspincludes.html

CJ

Commented:
Dear cheeky

S_B will want to use a dynamic include (<jsp:include>), because he wants to do some processing in the page and return some results back to the calling page. So, the question is not whether to use dynamic or static includes, but how to return some values back to the calling page.

Commented:
>or how can i solve this problem in  different way?

I thought implied that he might want to use a different approach.

CJ

Commented:
A different approach would be to write a bean and use it in the calling page.

Instantiating the bean:

<jsp:usebean id="myName" class="YourBeanClass" />

Getting a property value from the bean and inserting it
into the text:

<jsp:getProperty name="myName" property="myValue" />

Commented:
How would the included file see the bean using jsp:include? or the calling jsp see any beans used by the jsp:include file?

CJ

Commented:
No, no, the bean is used instead of the included page.

Commented:
OK.  Yup, that would work.

Creating convenience methods on beans (to do what your JSP include was doing) is definitely an option too.

CJ
Commented:
Summary for S-B:

Solution 1. Using session variables: getSession().set/getAttribute()

Solution 2. Using beans or other classes instead of the include JSP
S_B

Author

Commented:
thx a lot for the answers...
even if they don't hit my needs 100% i'm sure i can use them otherwise...

(i'm, not able to use beans because this will be processed in a different framework and a few other things which deny beans...)


lg sascha