aspose file tools*
The moose likes JSP and the fly likes Using a bean inside another bean Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » JSP
Bookmark "Using a bean inside another bean" Watch "Using a bean inside another bean" New topic
Author

Using a bean inside another bean

Didier McGillis
Greenhorn

Joined: Mar 06, 2002
Posts: 27
I'm new so please go easy

Okay here is my question. I have a bean that is used from browser detection. Now I want to place it in the bean that controls order flow, and place it as close to the top as possible, and then redirect to the old browser page. Currently I have it on a JSP page.

[Bear edit: cleaned up title for easier reading]
[ October 19, 2004: Message edited by: Bear Bibeault ]
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24187
    
  34

Hi,

Welcome to JavaRanch!

You may be a beginner, but you're a JSP beginner, so off you go to the JSP forum.


[Jess in Action][AskingGoodQuestions]
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61433
    
  67

Didier, welcome to the Ranch.

Are you new to JSP? If so, I would urge you to reconsider your approach. It sounds like you are placing a lot of processing directly on the JSP pages. Current conventional wisdom is to avoid that at all costs and to place such processing in servlet controllers that perform all processing and then forward to the JSP pages to render the resulting page.

Look up terms such as "MVC" and "Model 2" in relation to web applications for more information about this pattern.

Might as well start off on the right foot and not develop practices that you will have to unlearn later.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Didier McGillis
Greenhorn

Joined: Mar 06, 2002
Posts: 27
Okay maybe I didnt phrase this correctly.

I have an order servlet that "controls" the ordering process. I wanted to restrict users with certain browsers from getting into the ordering process, so I created a detect bservlet, really my first servlet. I placed the jsp tag for the servlet on several pages and the logic to say if certain browser redirect to hear. however I thought if I could include the servlet in the beginning of the ordering servlet then I wouldnt have to do it on each and every page. however I am new and not sure how to do this properly.
rahul V kumar
Ranch Hand

Joined: May 20, 2003
Posts: 82
I am not sure if I understood your problem correctly. But I feel that Filters might help you resolve this issue.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61433
    
  67

Still unclear about what you mean by "I placed the jsp tag for the servlet on several pages", but regardless, rahul is on-the-money with recommending a Filter to perform this kind of pre-processing.

You define the Filter to be invoked for certain URL patterns (perhaps all), and it gains control before the specified resource. It can make whatever check you wish and continue to process the resource or forward to elsewhere.

Again, putting a lot of goo on the pages that has nothing to do with rendering the view is something to be avoided.
Didier McGillis
Greenhorn

Joined: Mar 06, 2002
Posts: 27
<jsp:useBean id="db" class="com.dime.logic.DetectBrowser" scope="session">
<jsp:setProperty name="db" property="request" value="<%= request %>" />
</jsp:useBean>

<%
if(db.isns4() || db.isns6()) {
response.sendRedirect("/browser.jsp");
}
%>
[ October 19, 2004: Message edited by: Didier McGillis ]
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61433
    
  67

OK, the reason that was confusing is because that is instantiating a bean, not a servlet. Very different animals.

In any case, that's an example of an anti-pattern you want to try to avoid -- instantiting a bean on a page to perform processing that is better handled by a controller servlet that gains control long before any JSP page gets invoked.

But in your case, since you want to invoke some pre-processing before every page (rather than performing pre-processing specific to a single page), the Filter approach is your best bet.
Didier McGillis
Greenhorn

Joined: Mar 06, 2002
Posts: 27
Will do. Thanks, where I was getting my information was using Bean and Servlet interchangabily.

Where can I find out about Filters?
rahul V kumar
Ranch Hand

Joined: May 20, 2003
Posts: 82
I would suggest you read some information on jsp page assembly. You can find some examples of at J2EE blue print. By having run all your jsp pages through a common template would help resolve these kind of issues.

This link should help you give an understanding about filters.
http://java.sun.com/blueprints/patterns/InterceptingFilter.html
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Using a bean inside another bean