• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

useBean with only type attribute(please help)

 
vinal sen
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I found this code at headfirst servlets and jsp(page 420 Be the container)

here is the code

followed by comments in the book: fails at request time.the container knows that if you have only a type specified there must be a existing bean attribute of that name and scope.

---

my point is:

1.there is already a bean attribute "person" set at scope "request" in the servlet,right?which means the name property "fred" is not set and the original name "evan" is the answer.

2.if only "type" attribute is specified,there should be an existing bean by that name.what do you mean by existing bean? does the above example not mean there is an existing attribute in the request scope.

thank you very much in advance

Vinal


 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 65335
97
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
1. Why would the property not be set?

2. It means it expects to hook up to an existing bean and will fail if there isn't one.
 
vinal sen
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bear Bibeault,

1."set Property" will only work if the new instance is created,right?

new instance will never be created

bcos

1.there is already an existing attribute "person
2.even if there were not any,with only "type" attribute,no new instance will be created,right.


thanks,Vinal
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 65335
97
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
vinal sen wrote:
1."set Property" will only work if the new instance is created,right?

new instance will never be created

What happened when you tried it?

Also, please use real words when posting to the forums. Abbreviations such as "bcos" in place of "because" only serve to make your posts more difficult to read and less likely to generate useful responses.

Please read this for more information.

thanks,
bear
JavaRanch sheriff

 
vinal sen
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bear Bibeault,

I tried my code.It fails at runtime.

Are we not setting the attribute "person" in the request scope? then why is it trying to create a new instance?

Could you please explain.

thanks,Vinal
 
Janez Novak
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I have tried this and it works:

In servlet code set attribute to request and forward with RequestDispatcher to a jsp file.



In jsp file useBean tag can be used without class attribute since bean is present in request scope.


Janez
 
Duran Harris
Ranch Hand
Posts: 608
Eclipse IDE Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So...
does this not mean that the question is wrong,since it didn't fail at runtime??
 
Janez Novak
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Question on page 420 is misleading and incomplete, it does not tell how to get form servlet to jsp.

If you use forward from RequestDispatcher then it works like I have described in previous post.

but if you go for example from servlet to jsp with response.sendRedirect() you will get something like this:
java.lang.InstantiationException: bean person not found within scope

In this case request object does not contain person bean and type attribute alone is not sufficient to instantiate class.

So type attribute can be used alone if bean already exists in some scope, otherwise you will get InstantiationException.
 
vinal sen
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you Janez and Durran!

Last time,when there was a runtime error in the Jsp:useBean I had given "name" attribute instead of "id".
This time I have so many code problems,I will solve everything.

Janez,in case of redirect(),why is the bean person not found in the request scope?Any ideas?


Thanks
 
Janez Novak
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Method sendRedirect sends response with URL to client. This means that redirect happens on client side, you can see changed path in address bar of your web browser.

In case of forward, method call accepts request object with attributes (this is where person is stored) and response object. Now forwarded jsp has the same object that were processed in servlet code. Also forward is done on server side and client even does not notice forwarding.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic