Two Laptop Bag
The moose likes Web Component Certification (SCWCD/OCPJWCD) and the fly likes jsp:useBean type requirements Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Java Interview Guide this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Certification » Web Component Certification (SCWCD/OCPJWCD)
Bookmark "jsp:useBean type requirements" Watch "jsp:useBean type requirements" New topic

jsp:useBean type requirements

Ken Duncan
Ranch Hand

Joined: Apr 30, 2007
Posts: 51
In HFSJ, the statement is made that in the jsp:useBean tag, if you only specify the type and not the class, the bean must exist as an attribute and already be in the defined scope. e.g., page scope. I have a question about this. If the bean object is set as an attribute in the request, that's fine, but if I specify a scope of page or specify no scope at all, which equals page, since the jsp:useBean tag typically comes at the beginning of the JSP, the bean is not, I would think, in page scope yet because the request object has not been processed by the JSP (except perhaps under the covers). So will it ever work to specify type and not class for a bean that exists as a request attribute if not scope is specified? Thanks.

Marc Peabody
pie sneak

Joined: Feb 05, 2003
Posts: 4727

Terrific observation!

I don't think it's common in the real world to use type without class. In fact, I'm straining my brain to think if I've ever used jsp:useBean.

There might be some scenarios where the file containing jsp:useBean might make sense to expect a page scoped bean if you knew the JSP was never used independently, meaning you know it is always used via include. I seriously doubt if I'd write something like that, but still.

A good workman is known by his tools.
Mihai Radulescu
Ranch Hand

Joined: Sep 18, 2003
Posts: 918

Hi Duncan

Do you mean : if you use jsp:useBean without to specify a scope - then the bean in in the page scope, that means before the jsp:useBean you must add the bean in to the scope.
If this is what you mean you are right you must register the bean before usage.
There is one scenario when you don't need to do this - on static include
(@include file =".."), in this case the included file is just copied (inserted) on a certain position in your jsp, and this position can be after the point where you register the bean (in the page scope).


Rakesh Vende

Joined: Jul 31, 2006
Posts: 1
HI Mr. Duncan

It wont work ever. If your question is something like that

<% jsp:useBean id="somename" type="sometype" %> definalty container will look for existing bean instance in the PAGE SCOPE only not in request scope.

Hence in this case keepig bean instance in request will never server a purpose.

Rakesh Vende
Christophe Verré

Joined: Nov 24, 2005
Posts: 14688

I don't think there's any problem here, and I think you're saying two different things, but I may be missing something. If the bean is not in the session, you should not use useBean with a session scope and only a type. Same for the page scope. If the bean is not in the page, you should not use useBean with a page scope and only a type. Same problem, with a different scope.

You have some chances to put the bean in the session before forwarding to the page. Same for the page scope. You're still allowed to make the bean before the useBean tag. But using what ? A scriplet ? That would be meaningless.

So will it ever work to specify type and not class for a bean that exists as a request attribute if not scope is specified?

As a request attribute ? That's a different question, isn't it ? If you don't set the scope, useBean will look into the page scope. If the bean is in the request, it will fail. (same comment as Rakesh)

[My Blog]
All roads lead to JavaRanch
I agree. Here's the link:
subject: jsp:useBean type requirements
It's not a secret anymore!