This week's book giveaway is in the Agile and Other Processes forum.
We're giving away four copies of The Little Book of Impediments (e-book only) and have Tom Perry on-line!
See this thread for details.
Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

jsp:useBean - HF issue

 
Raef Kandeel
Ranch Hand
Posts: 87
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi there ranchers,

I have been studying from Head First Servlets & JSP's and I found Be-the-Container example on chapter 8:



Now, there is a servlet code that gets called first and after it gets called it forwards to the above jsp:-




In the answers it says it works fine, but, however, when I tried it, it did not work fine, apparently because it stores the value in the request scope when the jsp:useBean uses the page scope.

Am I right? Is HF wrong? Am I missing something?
 
Aaron Raja
Ranch Hand
Posts: 206
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Before jumping to answer your question, can you please post Employee Class also you might have miss something, and please let us know which page of HF example you doing and what error you getting!
Thanks
AR
 
Milton Ochoa
Ranch Hand
Posts: 336
Firefox Browser Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
this is my speculation:

I think... when you use the tag <jsp:useBean/>, you have to specify the attribe scope, I think that by default is page, but I not sure.
 
Raef Kandeel
Ranch Hand
Posts: 87
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok, let's see, yes you are right, it does assign a page default scope. I am pretty sure about that. As for the code:-





I use a slightly different naming: Person1 instead of Person and edu.scwcd.books.headfirst.ch8 for the package name instead of foo.

I am using a slightly old version of HF, but in my book the page is: 356.
 
sridhar row
Ranch Hand
Posts: 162
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Raef Kandeel wrote:Hi there ranchers,

I have been studying from Head First Servlets & JSP's and I found Be-the-Container example on chapter 8:



Now, there is a servlet code that gets called first and after it gets called it forwards to the above jsp:-




In the answers it says it works fine, but, however, when I tried it, it did not work fine, apparently because it stores the value in the request scope when the jsp:useBean uses the page scope.

Am I right? Is HF wrong? Am I missing something?


You are right..you should get an javax.servlet.ServletException: bean Employee not found within scope. This is because when the bean attribute is not found in the scope Contianer will try to do a new on the Employee and since it is a type exception occurs. Try changing the "type" to "class".
 
Raef Kandeel
Ranch Hand
Posts: 87
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks sridhar row for your reply. I guess you are right, if I change it to class, it would instantiate a new instance for the class and the name property would be Fred rather than Evan. Another solution would be to change the scope to request in which case the name property would be Evan rather than Fred.
 
Suresh Babu Venkat
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks everyone, i also got same doubt and abt to create a new topic and did a search before doing it and found this .. thanks
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic