File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Web Component Certification (SCWCD/OCPJWCD) and the fly likes jsp:useBean conceptual problem 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 conceptual problem" Watch "jsp:useBean conceptual problem" New topic
Author

jsp:useBean conceptual problem

Harshana Dias
Ranch Hand

Joined: Jun 11, 2007
Posts: 327
In HFSJ 1.5 page 420 I have a problem.

It says,

<jsp:useBean id="person" type="foo.Employee" scope="request">
<jsp:setProperty name="person" property="name" value="Fred" />
</jsp:useBean>

Name is: <jsp:getProperty name="person" property="name">


Following is what in the servlet,

foo.Person p=new foo.Employee();
p.setName("Evan");
request.setAttribute("person",p);


And following are the bean classes,

public class abstract Person(){

private String name;

public void setName(String name){
this.name=name;
}

public String getName(){
return name;
}
}

public class Employee extends Person{

// setEmpID and getEmpID methods
}

So what i understand is since the jsp:useBean only define type the body will not get execute. But it wont be a problem because in servlet we have already set a attribute.

And the servelt generated by compiled by the jsp will look like this probebly,

Employee person = null;
person = (Employee) _jspx_page_context.getAttribute("person", PageContext.REQUEST_SCOPE);
if (person == null){
throw new java.lang.InstantiationException("bean person not found within scope");
}

So my point isnt the "_jspx_page_context.getAttribute("person", PageContext.REQUEST_SCOPE);" will return a object (with refernce type Person and object type Employee) becuse we set it in our servlet (both have request scope i mean in servlet and in jsp)and it will be cast in to Employee and person variable here will be initialized. So Evan should have print.

But the book says, Fail at request time.(me : what is exactly this request time..is it like after having RequestDispatcher we forward(request,response) to the JSP time??) The "person" attribute is stored at request time so the jsp:useBean tag wont work since it specify only the type (me : here im having a problem with this statement it should be jsp:setProperty tag wont work not jsp:useBean??) and it also saying The container knows that if you have only a type specified there must be an existing bean attribute of that name and scope(me: ofcourse we have set a attribute in the servlet with person name and request scope so there shouldn't have a problem??)

please explain me what is wrong or if I have understand the concept wrong..im stuck with this concept.

Thank you.
Narendhiran Nagarajan
Ranch Hand

Joined: Jun 17, 2009
Posts: 30
You are wrong!!!

Class- both the reference AND the object type
Type- only reference type

When you use-
<jsp:useBean id="person" type="foo.Employee" scope="request">
It searches for the existing bean, whose reference type is "foo.Employee"(not object type).

Consider Person <- Employee <- ITEmployee <-ITIntern.

When you use the bove tag, there should be a bean
with the reference type= Employee (must) and the object type can be either Employee or ITEmployee or ITIntern.
Also this bean should be stored as an attribute with the id="person", at the scope="request" .

The reason for not able to create a new instance,when we have only type is , we are just providing the information about the reference type and not about the object type.

I hope, you confused between id (person) used in the tag and the object type (Person)


Naren
-------------------
SCJP - 90%
SCWCD - 98%
Harshana Dias
Ranch Hand

Joined: Jun 11, 2007
Posts: 327
Thankx for the reply.

When you use the bove tag, there should be a bean
with the reference type= Employee (must) and the object type can be either Employee or ITEmployee or ITIntern.


But in the servlet generated in the translation time of JSP have this code which we cast the Person type to Employee.

Employee person = null;
person = (Employee) _jspx_page_context.getAttribute("person", PageContext.REQUEST_SCOPE);

wouldn't it help ??
Narendhiran Nagarajan
Ranch Hand

Joined: Jun 17, 2009
Posts: 30
person = (Employee) _jspx_page_context.getAttribute("person", PageContext.REQUEST_SCOPE);

The getAttribute will always return object, unlike getParameter returns string.
String s=request.getAttribute("name") ; // This will not work
String s=(String)request.getAttribute("name") ; //This will work

Its similar to this.So, we are telling what kind of object is coming from getAttr.
TypeCasting doent come into this context, I think so.

If I m not wrong,
Employee person = null;
person = (Employee) _jspx_page_context.getAttribute("person", PageContext.REQUEST_SCOPE);
will always be same.
Harshana Dias
Ranch Hand

Joined: Jun 11, 2007
Posts: 327
Narendhiran Nagarajan wrote:
person = (Employee) _jspx_page_context.getAttribute("person", PageContext.REQUEST_SCOPE);



is there any impact with this cast?
Harshana Dias
Ranch Hand

Joined: Jun 11, 2007
Posts: 327
Well guys it should print Evan. i guess what i am speaking was right.
this is a error with HFSJ and its been confirmed by authors.

check this,
http://oreilly.com/catalog/errata.csp?isbn=9780596516680


geeta vemula
Ranch Hand

Joined: Jul 18, 2008
Posts: 208
from the above discussion what i understand is even though we specify only type for usebean , it is able to recognize the beans set in the specified scope whose referance type is not same but some superclass of that(like person-employee) . but what will happen to the polymorphism issues here. like what if employee have one more property called empId or persons name is something else set in person bean? will the getAttribute works the same way?

thanks,
geeta
Vaib Gupta
Greenhorn

Joined: May 11, 2009
Posts: 15
I think this would be more of an SCJP issue. ...Everthing would be decided on run time. If 'emp' has an extra property so would the underlying object of referred by 'person'...And as for any name defined in person, it would be again governed by the underlying object unless the property is static...
ramkumar rengarajan
Greenhorn

Joined: Jul 31, 2009
Posts: 4
But when I implement the below code in Websphere and test, it prints

Name is: Fred

According to JSP spec, the body will not get execute and it should have printed Name is: Evan.

I have checked the generated servlet code (see below).

foo.Employee person1 = null;

synchronized (request) {

person1 = (foo.Employee) pageContext.getAttribute("person1", PageContext.REQUEST_SCOPE);

if (person1 == null) {

throw new java.lang.InstantiationException("bean person1 not found within scope");

}

out.write(_jsp_string1);

org.apache.jasper.runtime.JspRuntimeLibrary.introspecthelper(pageContext.findAttribute("person1"), "name", "Fred", null, null, false);

out.write(_jsp_string1);

}

It is showing the code for setting the name as “Fred” which means body gets executed and able to see the output as Name is: Fred

In other words, the code for setting the name as “Fred” should not execute.

Please explain what went wrong or if I have misunderstood the concept.
ramkumar rengarajan
Greenhorn

Joined: Jul 31, 2009
Posts: 4
I have tried my above code on Tomcat container.

It is behaving as per JSP spec unlike Websphere.


We do not have the below statement for the my given code on Tomcat container.

org.apache.jasper.runtime.JspRuntimeLibrary.introspecthelper(pageContext.findAttribute("person1"), "name", "Fred", null, null, false);

So, the container used by Websphere behaves differently in this case.

Not sure why? can anybody investigate this?

 
It is sorta covered in the JavaRanch Style Guide.
 
subject: jsp:useBean conceptual problem