File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes JSP and the fly likes JSP:Include question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » JSP
Bookmark "JSP:Include question" Watch "JSP:Include question" New topic
Author

JSP:Include question

Claude Forkner
Greenhorn

Joined: Feb 19, 2006
Posts: 24
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.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61202
    
  66

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 ]

[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Claude Forkner
Greenhorn

Joined: Feb 19, 2006
Posts: 24
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
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61202
    
  66

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.
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

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


Java API J2EE API Servlet Spec JSP Spec How to ask a question... Simple Servlet Examples jsonf
Claude Forkner
Greenhorn

Joined: Feb 19, 2006
Posts: 24
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
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61202
    
  66

If you are using JSP 2.0, custom actions implemented as tagfiles are almost as simple as includes.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: JSP:Include question