• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Junilu Lacar
  • Liutauras Vilda
Sheriffs:
  • Paul Clapham
  • Jeanne Boyarsky
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
Bartenders:
  • Jesse Duncan
  • Frits Walraven
  • Mikalai Zaikin

jsp:useBean - HF issue

 
Ranch Hand
Posts: 87
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Ranch Hand
Posts: 206
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 336
Firefox Browser Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 162
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
reply
    Bookmark Topic Watch Topic
  • New Topic