• 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

Doubts in Standard Actions

 
Ranch Hand
Posts: 344
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have a doubt in <jsp:useBean> standard action.

How the <jsp:useBean> will create a bean..

In HFSJ states that:


If the <jsp:useBean>? can't find an attribute object named "person", it can make one!




It checks for a bean based on the values of the id and scope in the tag, and if it doesn't get one, it makes an instance of the class specified in the class, assigns the object to the id variable, then sets it as an attribute in the scope you defined in the tag.



I don't understand the above two quotes..can anyone explain it..?

Example:

I have a bean class Person:



And in the JSP:


Whether the quotes states that, if I am not having the Person bean class, the <jsp:useBean> will create a new bean class with getter and setter..?


So <jsp:useBean> is set the attribute person...

The abive thing is retrieving the attribute

Then how comes when retrieving the attribute, it will be null..?

Pls explain..? some where I have confused a lot..
 
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You wrote so many things that I'm confused. Anyway, have you already studied taglibs? If yes ( and I hope so):
<jsp:useBean id="person" class="com.example.model.Person" scope="request"/>
will do something like:
Person person = (Person)request.getAttribute("person");
if (person==null)//doesn't exists
person = new Person();

Does it answer your question?
 
Ranch Hand
Posts: 292
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Its very simple....don't get confused.....if there is no attribute(we're talkin' about a bean set as an attribute) by the name specified by id="...." in the jsp:useBean tag (the container finds it out if the getAttribute() method returns null) then a new bean(the same class as class="...." in the jsp:useBean tag) is instantiated and set as an attribute by the name as in id="...." in the scope specified(or page scope if scope is not specified)

Thats it....HTH
[ January 03, 2007: Message edited by: Sayak Banerjee ]
 
Micheal John
Ranch Hand
Posts: 344
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for the reply..


Person person = (Person)request.getAttribute("person");
if (person==null)//doesn't exists
person = new Person();



My doubt is in the above thing, who is setting the attribute for person...
because we are not setting the attribute for person like request.setAttribute("person",p);
So always Person person = (Person)request.getAttribute("person"); will be null!!
If possible..can you show one example on it..pls?

[ January 04, 2007: Message edited by: Micheal John ]
[ January 04, 2007: Message edited by: Micheal John ]
 
Ranch Hand
Posts: 243
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
if your jsp looks like this

Person p=new Person();
p.setName("name1");
request.setAttribute("person",p);
<jsp:useBean id="person" scope="request">
<jsp:getProperty name="person" property="name">

This will print name1 as there is a request attribute with the name person and no new bean is created.Hope its clear now
 
Renu Radhika
Ranch Hand
Posts: 243
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please read the above as this,I have added whatever I missed out

<%
myPackage.Person p=new myPackage.Person();
p.setName("name1");
request.setAttribute("person",p);
%>
<jsp:useBean id="person" scope="request" type="myPackage.Person">
<jsp:getProperty name="person" property="name">
 
Micheal John
Ranch Hand
Posts: 344
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes.. it's clear now renu.. thanks for your explanation..


if your jsp looks like this
Person p=new Person();
p.setName("name1");
request.setAttribute("person",p);
<jsp:useBean id="person" scope="request">
<jsp:getProperty name="person" property="name">




so if in my jsp, I am using only the code below, without creating the instance of person and set the name for it.. then the <jsp:useBean> will created an instance of the person automatically and set the arrtibute wuth the specified scope..right..?
 
Renu Radhika
Ranch Hand
Posts: 243
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
To create one and set the value for name you have to write like this

<jsp:useBean id="person" scope="request" class="myPackage.Person">
<jsp:setProperty name="person" property="name" value="name1">
</jsp:usebean>

note that here both 'class' and body-content for jsp:useBean is important,then only it will know which class instance should be assigned to person reference and to set any property if a new bean is getting created then include it in the body
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic