• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

JSP:Include question

 
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
So, Basically I have about 15 jsp pages that each are supposed to display some basic html, and then they iterate through a bean named byNameDbbean and display the records from it on the page. I realized that each of these jsp pages uses the same code to iterate and display items from the bean, so I want to seperate this out to it's own jsp and use jsp:include to call that code. So, I tried this. I basically cut out all the bean iteration scriptlets I'm using, and I pasted them into one jsp file called "ItemDisplayer.jsp". I then do a jsp include of ItemDisplayer, from every one of my 15 jsp pages. My problem is that I'm not sure how to make the ItemDisplayer (the included jsp file) be "aware" and able to use the byNameDBbean and the other beans that I did a jsp:useBean for on the main pages. I thought I wouldn't have to do anything because the jsp included page would already know about all the variables that are declared in one of the main jsp pages?
Instead, do I need to also have code like:
<%@ page import="com.db.beans.*" %>
<jsp:useBean id="byNameDBbean" class="com.db.beans.DBSelect" scope="session" />

listed in the ItemDisplayer.jsp so it knows about these beans?

Any help would be great. Thanks!

(See below, to see what I'm trying to do)



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

Then in ItemDisplayer.jsp I would like to do something like:


I get an error on byNameDBbean right now because the jsp doesn't know about this variable.
 
Sheriff
Posts: 67747
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Claude Forkner:
I thought I wouldn't have to do anything because the jsp included page would already know about all the variables that are declared in one of the main jsp pages?



How?

Even though the scoped variable (bean) is present in request or session scope, you still need to tell the included page to hook up to it with a <jsp:useBean> action.

You can't do it at all with page-scoped variables as they are not shared with included pages.
[ January 22, 2007: Message edited by: Bear Bibeault ]
 
Claude Forkner
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ok, that is making some sense to me. So I need to add the jsp:usebean to the inserted jsp.

A couple questions about doing that. Say I have in the main page:


Are you saying that I should change this tag to be request scope (instead of page scope) like:


Are there any negatives to changing my beans to be request scoped rather than page scoped?

From what i can tell, if I do the usebean and change it to be request scoped, as long as there isn't a bean with that name already in the request scope, it will create this bean in the main jsp page. THEN, when I use the same usebean stmt in the included jsp page, it will not create a new bean on that page, because it should find the bean already in the request?

Then when I am in my jsp included page, what jsp:usebean stmt do i use to have access to this bean that is from the main page? Would that be the same piece of code as the main page's jsp:include, or would it be different?

Thanks for your help!
[ January 22, 2007: Message edited by: Claude Forkner ]
 
Bear Bibeault
Sheriff
Posts: 67747
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Claude Forkner:

Are you saying that I should change this tag to be request scope (instead of page scope)



If you want to share the scoped variable outside of the page, then you can't use page scope. That's the whole point of page scope.

Are there any negatives to changing my beans to be request scoped rather than page scoped?



Only that the scoped variable now has request scope. If that's your intention, it's a plus. If it's not, it's a negative.

Generally, the useBean action will be the same. But the useBean action has a lot of variations and semantics so that's not always true.

Personally, I wouldn't be using includes for this in the first place. I'd write custom actions and explicitly pass data to them in the attributes rather than using includes with implicit sharing via scoped variables.
 
Sheriff
Posts: 13411
Firefox Browser VI Editor Redhat
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Why are you using the <jsp:include ../> action?

It sounds like the static <%@ include .. %> directive would be more suitable.
The include directive is the equivalent to cutting the code files together before the JSP is compiled into a servlet.

The include action runs each page as standalone JSPs.

See:
http://faq.javaranch.com/view?IncludesActionDirective
 
Claude Forkner
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hmm. Well, this sounds scary (when using the directive):
"One drawback on older containers is that changes to the include pages may not take effect until the parent page is updated. Recent versions of Tomcat will check the include pages for updates and force a recompile of the parent if they're updated. "

And I like this feature of the Action:
"Pages can conditionally be included at run time. This is often useful for templating frameworks that build pages out of includes. The parent page can determine which page, if any, to include according to some run-time condition. "

I have never written a custom action myself and a little worried about doing that, since it seems quit a bit more complicated to setup than just doing an include.

What is the best solution for this?
 
Bear Bibeault
Sheriff
Posts: 67747
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If you are using JSP 2.0, custom actions implemented as tagfiles are almost as simple as includes.
 
It's weird that we cook bacon and bake cookies. Eat this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic