Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Can a servlet access the bean created by the JSP  RSS feed

 
Karthik Guru
Ranch Hand
Posts: 1209
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi all,
I have a jsp which has jsp:useBean tag and creates a bean. Then i forward the request to a servlet. Can the servlet now access the bean using the id attribute value i specified while creating the bean in the jsp??
thanks
karthik
 
Zakaria Haque
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
you have to specify the scope as something other than the default page. If you need the the bean only to serve that particular request, give it a request scope, it will bind the bean with the request object with the "id" as key.
 
George Pavamani
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I agree with what ever Zakaria Haque has said.
But my question is otherway round. Let us suppose that a servelt has set an attribute in the request object using setAttribute(), can this attribute be made available in the jsp page(let us suppose that control is transfered to JSP page using RequestDispatch.forward()) using <jsp:usebean> tag, i mean without instantiating a new bean??
Thanks
George
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66204
151
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Absolutely. That's the definition of how useBean works. In a nutshell: if a bean with the specified name exists at the specified scope level, it is used. Otherwise, a new one is created using the default constructor (hence, one of the reasons it must be a bean) and placed into the scope.
bear
 
George Pavamani
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Bear for the reply.
But my question was, what if the attribute set to the request object is not a bean (i.e which doesn't implement Serializable, doen't have a no-arguement constructor, ot corresponding setter and getter methods), can we still make the object visible using the jsp:usebean tag???
Thanks a ton for the reply
Thanks
George
 
William Brogden
Author and all-around good cowpoke
Rancher
Posts: 13078
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think you would run into trouble if the object does not meet the bean criteria but you can certainly attach anything you want to the request as an attribute and retrieve it again on the JSP side using normal request.getAttribute() and casting to the right class.
Bill
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66204
151
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
William is correct, of course. With useBean, if your class does not at least sport a no-arg constructor your JSP will not compile. (It won't care about Serializable.)
If your class does not follow at least the rudimentary bean pattern you can do it "by hand" via getAttribute on the appropriate scope, or using the page context convenience function that will let you specify which scope to look into.
In fact, I'd suggest taking a look at how a useBean tag gets translated in the java servlet generated for your JSP page. Very instructional.
hth,
bear
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!