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..?
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..
SCJP 1.4 (86%), SCWCD 1.4 (86%), SCBCD 1.3 (85%), SCDJWS (Just Started...) - Satisfaction Lies in Our EFFORT, Not in the ATTAINMENT
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();
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)
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 ]
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..?
Joined: Oct 21, 2005
To create one and set the value for name you have to write like this
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