Win a copy of Java Mock Exams (software) this week in the Programmer Certification (OCPJP) forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

jsp:useBean Doubt

 
Praveen Sharma
Ranch Hand
Posts: 129
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have some JSP code:

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


I have the following servlet code which does some work and forwards the request to the above JSP:

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


Now, this scenario is supposed to fail (as explained in Head First Servlets and Jsp book, Chapter is Scriptless Jsp). Can someone please explain to me why should it fail ? If we use only the type attribute the bean should already exist and I think the following line is doing that :
request.setAttribute("person",p);
 
Praveen Sharma
Ranch Hand
Posts: 129
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Just to add to my question "Person" is an abstract class and "Employee" is a non-abstract class.
 
Srividhya Kiran
Ranch Hand
Posts: 166
 
Jan Sterk
Ranch Hand
Posts: 142
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by deepak valechha:
.. If we use only the type attribute the bean should already exist and I think the following line is doing that :
request.setAttribute("person",p);


The bean exists in the request scope. But the useBean action looks in the page scope, since the scope attribute is missing and page scope is the default.
 
Praveen Sharma
Ranch Hand
Posts: 129
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Jan !!
 
What are you doing? You are supposed to be reading this tiny ad!
the new thread boost feature brings a LOT of attention to your favorite threads
https://coderanch.com/t/674455/Thread-Boost-feature
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!