aspose file tools*
The moose likes Servlets and the fly likes servlet & jsp related doubt Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "servlet & jsp related doubt" Watch "servlet & jsp related doubt" New topic
Author

servlet & jsp related doubt

vianyrajnish rajnish
Ranch Hand

Joined: Apr 22, 2007
Posts: 70
hi all ,

This is taken from Kathy Sierra..Head first jsp and servlets

jsp code is like this below:

<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" />


questions are :
what happens if the sevlet code is like this

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

answer :
fails at request time . The person attribute is stored at request scope
so <jsp:useBean > tag wont work because it can't find bean attribute .

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

answer : this works fine and prints out "Evan"

-------------------------------------------------

my doubt is in both 1) and 2) question the person attribute is having request scope only ...then how the 2) will run succesfully.
( in jsp above ther is no scope , so default is page scope )


can anyboby please answer this ..

thanks,

Viany Rajnish
[ July 11, 2007: Message edited by: Bear Bibeault ]
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

Are you missing something?
1) and 2) look the same to me.

Did you mean to post a second version of the JSP code?


Java API J2EE API Servlet Spec JSP Spec How to ask a question... Simple Servlet Examples jsonf
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

Please do not post the same question in multiple forums.
This is called cross-posting.
It causes confusion and duplication of effort which will annoy the people who would otherwise help you making it less likely that you will get an answer to your question.

I closed the topic in the JSP forum.

If you're not getting an answer to your question, maybe you could improve the question. We have some tips for doing so here
vianyrajnish rajnish
Ranch Hand

Joined: Apr 22, 2007
Posts: 70
hi,

questions 1) and 2) are not same

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



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

--------------------------

here Employee class is inheriting Person class.

See the assignments objects of references

Thanks,
Vinay Rajnish
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61658
    
  67

The only difference is how p can be used in the servlet after its declarations. In one, p can only be treated as a Person. In two, it can be treated as an Employee.

What happens later, like in a JSP, depends on what you do later.
[ July 13, 2007: Message edited by: Bear Bibeault ]

[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Joe Harry
Ranch Hand

Joined: Sep 26, 2006
Posts: 9524
    
    2

Did you try to check the errata for this? Check it at http://www.oreilly.com/catalog/headservletsjsp/errata/headservletsjsp.confirmed


SCJP 1.4, SCWCD 1.4 - Hints for you, Certified Scrum Master
Did a rm -R / to find out that I lost my entire Linux installation!
vianyrajnish rajnish
Ranch Hand

Joined: Apr 22, 2007
Posts: 70
Hi Jothi Shankar Kumar,


Thanks for the reply ... I found the error following the errata ..

but who writes these errata for the book...

( this is not to be posted here but i am curious to know)

thanks all

Vinay rajnish
Joe Harry
Ranch Hand

Joined: Sep 26, 2006
Posts: 9524
    
    2

I guess if you visit the SCWCD forum, there is a link which leads you to the errata website. Also note that, any software book will for sure have a site dedicated to errata's. You can be doubly sure of that.
vianyrajnish rajnish
Ranch Hand

Joined: Apr 22, 2007
Posts: 70
hi Jothi Shankar Kumar,

Thank you....


thanks ,

Vinay rajnish
 
Don't get me started about those stupid light bulbs.
 
subject: servlet & jsp related doubt