This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Web Component Certification (SCWCD/OCPJWCD) and the fly likes Doubts in Standard Actions Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Certification » Web Component Certification (SCWCD/OCPJWCD)
Bookmark "Doubts in Standard Actions" Watch "Doubts in Standard Actions" New topic
Author

Doubts in Standard Actions

Micheal John
Ranch Hand

Joined: Nov 01, 2006
Posts: 344
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..


Micheal John
SCJP 1.4 (86%), SCWCD 1.4 (86%), SCBCD 1.3 (85%), SCDJWS (Just Started...) - Satisfaction Lies in Our EFFORT, Not in the ATTAINMENT
Tha�s Manfrim Firmino
Ranch Hand

Joined: Apr 12, 2004
Posts: 48
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?


Tha�s M. K. M. Firmino
Sayak Banerjee
Ranch Hand

Joined: Nov 28, 2006
Posts: 292
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 ]

Turn on, tune in, drop out.
Micheal John
Ranch Hand

Joined: Nov 01, 2006
Posts: 344
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 ]
Renu Radhika
Ranch Hand

Joined: Oct 21, 2005
Posts: 243
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

Joined: Oct 21, 2005
Posts: 243
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

Joined: Nov 01, 2006
Posts: 344
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

Joined: Oct 21, 2005
Posts: 243
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
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: Doubts in Standard Actions
 
Similar Threads
unable to access requested scoped attribute in page scoped JSP
bean problem
useBean doubt
<jsp:useBean> unexpected behavior
jsp:useBean and jsp:setProperty question