• Post Reply Bookmark Topic Watch Topic
  • New Topic

useBean and includes  RSS feed

 
Angela Poynton
Ranch Hand
Posts: 3143
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I've done a search on this and can't find a post where is has been answered before. So here I go.
I have a JSP in which I use the <jsp:useBean> tag and I set the scope to "request".
this same JSP also includes another JSP using the <jsp:include> tag. The included JSP tries to access the bean retrieved in the outer jsp .. .but it seems it can't see it!!
In case that wasn't english! Here's some of my code.
outer.jsp

inner.jsp

and my error ... in Tomcat!

Anyone have any clues??
 
Brian Glodde
Ranch Hand
Posts: 171
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Your code says "include this code fragment at the translation phase". What this means is the code fragment in the include does not have visibility to the elements on the original page.
The jsp:include doesn't include the actual contents of the page, it only includes the response produced.
Since you need it in the response phase, one solution might be to pass a reference to the bean on the included page with the jsp: param tag

That's just an example of how you pass parameters to your includes. You can learn more about includes and param
here. Also the colon after jsp and param shouldn't have a space, but when I type correctly, it makes

[ April 26, 2002: Message edited by: Brian Glodde ]
[ April 26, 2002: Message edited by: Brian Glodde ]
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66208
151
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The bean is accessible from the included page, just not using the handy id that you assigned it with the useBean tag.
In a nutshell, what that tag does is to create a variable in the servlet generated for the JSP page. Naturally, this is only available within that servlet and hence, that page. However, this variable points to an instance of the bean stored as an attribute in the scope you declare it (in this case, the request). The tag will either "attach" the variable to an existing bean that's already there, or create a new one using the no-arg constructor if there isn't one.
In any case, you can retrieve the bean using the request.getAttribute() method.
To get a handle on learning exactly how the useBean tag works, it'd be instructive to look at the code that is generated for it in the servlet created by the container for the JSP page.
hth,
bear
 
Angela Poynton
Ranch Hand
Posts: 3143
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks guys!! I took your advice and now it works perfectly.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!