• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

HFSJ Page 356, Page 416

 
Fola Fadairo
Ranch Hand
Posts: 35
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

Question:

Be the Container. Figure out what the container would do for each of the three diffent servlet code examples.


Look at this standard action:


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


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


public abstract class Person{

private String name;

public String getName();
public void setName();

}


public class Employee extends Person {
//implentations of inherited methods.... getName() and setName()

private int empID;

public int getEmpID(){
return empID;
}


void setEmpID(int e) {
empID = e;
}

}


Servlet code example 3:
What happens if the servlet code looks like:

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

The answer says:
"This works fine, and prints out "Evan". Remember, the code INSIDE the body of <jsp:useBean> will NEVER run, since we specified a type without class."


In my opinion, the answer should be: (like the 1st servlet code, not shown here)
"FAILS at request time! The person attribute is stored at request scope, so the jsp:useBean> tag won't work since it specifies only a type. The Container knows that if you have only a type specified, there must be be an existing bean attribute of that name and scope."
[ June 05, 2007: Message edited by: Fola Fadairo ]
 
Kamal Agrawal
Greenhorn
Posts: 14
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You are right.
Default scope for jsp:useBean is Page not request.
So now the belwo code will run fine.

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


Name is: <jsp:getProperty name="person" property="name" />
<br>
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic