Two Laptop Bag*
The moose likes Web Component Certification (SCWCD/OCPJWCD) and the fly likes <jsp:useBean> Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Web Component Certification (SCWCD/OCPJWCD)
Bookmark "<jsp:useBean>" Watch "<jsp:useBean>" New topic
Author

<jsp:useBean>

Lucas Smith
Ranch Hand

Joined: Apr 20, 2009
Posts: 804
    
    1

Can you give an example where this standard action is useful when a component already exists?


SCJP6, SCWCD5, OCE:EJBD6.
BLOG: http://leakfromjavaheap.blogspot.com
Chinmaya Chowdary
Ranch Hand

Joined: Apr 21, 2008
Posts: 432
Hi, Lucas.

Using 'type' attribute of <jsp:useBean> like
Lucas Smith
Ranch Hand

Joined: Apr 20, 2009
Posts: 804
    
    1

OK, but why should I do this?
Isn't using <jsp:useBean> when the attribute exists meaningless?
Niteen Patil
Ranch Hand

Joined: Oct 27, 2009
Posts: 45
<jsp:useBean> allows us to use an attribute from a particular scope using the scope attribute and allows us to write a scriptless code.
Lucas Smith
Ranch Hand

Joined: Apr 20, 2009
Posts: 804
    
    1

But we can use the attribute using EL and useBean is not necessary.
Please give me a good example where useBean does not create a new object and it is necessary to run scriptless code.
Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14687
    
  16

jsp:setProperty But as you have noticed, with EL, useBean is not very useful, unless you want to create or set something. As we know, JSP are views, so you should not need it.


[My Blog]
All roads lead to JavaRanch
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60782
    
  65

In a scriptless environment, <jsp:usebean> isn't all that useful if the scoped variable already exists. As you have noted, it's not necessary to make the scoped variable accessible via the EL. Where I do use it to is to instantiate instances of view-support beans on behalf of the page.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Lucas Smith
Ranch Hand

Joined: Apr 20, 2009
Posts: 804
    
    1

Thanks a lot.
One more question.
<jsp:useBean> (or other custom action) should be used to make a bean visible for <jsp:setProperty> and <jsp:getProperty> actions. But there is written is JSP specs that <jsp:setProperty> and <jsp:getProperty> will apply to a bean returned by pageContext.findAttribute(...).
So why do those actions need <jsp:useBean>? pageContext.findAttribute(...) finds a bean without <jsp:useBean>.
How can <jsp:useBean> makes a bean visible for <jsp:set/getProperty>?
Chinmaya Chowdary
Ranch Hand

Joined: Apr 21, 2008
Posts: 432
Hi, Lucas.
So why do those actions need <jsp:useBean>? pageContext.findAttribute(...) finds a bean without <jsp:useBean>.
How can <jsp:useBean> makes a bean visible for <jsp:set/getProperty>?


We can set and get properties(by using <jsp:set/getProperty>) without using <jsp:useBean>, if the attribute is present in scope. Like, if we set attribute by using scriptinting,
Lucas Smith
Ranch Hand

Joined: Apr 20, 2009
Posts: 804
    
    1

I do not think so.



and response:
rg.apache.jasper.JasperException: jsp:getProperty for bean with name 'person'. Name was not previously introduced as per JSP.5.3

So?
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9291
    
  17

jsp:setProperty and jsp:getProperty that the bean be declared using the jsp:useBean action. This is from the JSP spec

The object named by the name must have been “introduced” to the JSP processor using either the jsp:useBean action or a custom action with an associated VariableInfo entry for this name. If the object was not introduced in this manner, the container implementation is recommended (but not required) to raise a translation error, since the page implementation is in violation of the specification.

Note – A consequence of the previous paragraph is that objects that are stored
in, say, the session by a front component are not automatically visible to jsp:set-
Property and jsp:getProperty actions in that page unless a jsp:useBean action, or
some other action, makes them visible.



SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
Lucas Smith
Ranch Hand

Joined: Apr 20, 2009
Posts: 804
    
    1

Thanks Ankit, I have already seen it.

I have one more question.

Please give me some information about <variable> tag (it can be assigned inside <tag> in TLD or as a directive in tag file).

I think that because of "variable" there is possibility to put variable from custom tag outside this tag (dependent on scope - NESTED and others). How can I set that variable in tag handler and tag file? But on the other hand we can assing variable inside tag handler or tag file using pageContext. I know that "variable" is connected with VariableInfo.

Please give me wider information.

Thanks!
Chinmaya Chowdary
Ranch Hand

Joined: Apr 21, 2008
Posts: 432
Hi, Lucas.

I tested with Tomcat5.5.9 and it is working without any exceptions.
Lucas Smith
Ranch Hand

Joined: Apr 20, 2009
Posts: 804
    
    1

Please consider:



Above code works fine.



Does not work. Bean was not introduced.
Tomcat 5.5.28
I do not know what should I think.

And please give me an explanation for <variable>.
Chinmaya Chowdary
Ranch Hand

Joined: Apr 21, 2008
Posts: 432
Hi, Lucas.

The JSP specification says,
If the object was not introduced in this manner, the container implementation is recommended (but not required) to raise a translation error, since the page implementation is in violation of the specification.


I think it depends on container implementation. It seems that 'Tomcat 5.5.28' will not implement this.

Incase of <variable>, see this. And for 'variable' directive in tag files, see this.
avi sinha
Ranch Hand

Joined: Mar 15, 2009
Posts: 452

Chinmaya Chowdary wrote:Hi, Lucas.

The JSP specification says,
If the object was not introduced in this manner, the container implementation is recommended (but not required) to raise a translation error, since the page implementation is in violation of the specification.


I think it depends on container implementation. It seems that 'Tomcat 5.5.28' will not implement this.

Incase of <variable>, see this. And for 'variable' directive in tag files, see this.


correct chinmaya !!

in my system i am able to access a bean using setProperty & getProperty without declaring the name using useBean or custom tag
i think its vendor dependent. mine is tomcat5.5.
avi sinha


SCJP 5.0 SCWCD 5.0
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: <jsp:useBean>