• 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:useBean>

 
Ranch Hand
Posts: 808
1
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can you give an example where this standard action is useful when a component already exists?
 
Ranch Hand
Posts: 437
Oracle Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi, Lucas.

Using 'type' attribute of <jsp:useBean> like
 
Lucas Smith
Ranch Hand
Posts: 808
1
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
OK, but why should I do this?
Isn't using <jsp:useBean> when the attribute exists meaningless?
 
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
<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
Posts: 808
1
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Sheriff
Posts: 14691
16
Eclipse IDE VI Editor Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Sheriff
Posts: 67746
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
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.
 
Lucas Smith
Ranch Hand
Posts: 808
1
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 437
Oracle Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 808
1
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
Sheriff
Posts: 9707
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.

 
Lucas Smith
Ranch Hand
Posts: 808
1
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 437
Oracle Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi, Lucas.

I tested with Tomcat5.5.9 and it is working without any exceptions.
 
Lucas Smith
Ranch Hand
Posts: 808
1
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 437
Oracle Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 453
Google Web Toolkit Hibernate Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
 
reply
    Bookmark Topic Watch Topic
  • New Topic