views:

63

answers:

1

Hi,

I really don't understand why the following code in JSP is running fine, but as described in Head first book, it will show compile time error.

<html><body>

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

</body></html>

The code of Person class is:

 package foo;

public class Person extends foo.Person1
{
private String s;

public void setEmpID(String s)
{
  this.s=s;
  }
  public String getEmpID()
  {
     return s;
  }


  }

and the code to instantiate foo.Person in servlet class and setting it as an attribute "Person" in request scope is:

Person1 p=new Person();
p.setName("Greenhorn");
request.setAttribute("person",p);

and the code of Person1 bean class is:

package foo;

 public abstract class Person1
{


private String s;

public void setName(String s)
{
  this.s=s;
  }
  public String getName()
  {
     return s;
  }


  }

Why it is working? why it is not showing any error? Is the book wrong?

Thanks in advance for any suggestion.

A: 

I checked the book. On page 354 it changes the Person to an abstract class and creates a sublcass of Employee to illustrate how you can specify the type attribute of the useBean. The type parameter tells the page what class to use later on as the reference. The class attribute tells the page what object to instatiate.

Edit: On page 356 the black box states: "If type is used without class bean must already exist." The scope in the book example is page instead of request.

kd304
But in above code JSP has type attribute with value "foo.Person", but in servlet the reference is actually declared as "foo.Person1 p=new foo.Person();". So it means it should be incorrect, but it is not when I run it in Tomcat.
Greenhorn