• Post Reply Bookmark Topic Watch Topic
  • New Topic

JSP beans within servlets  RSS feed

 
Andrew Sweeney
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Afternoon everybody,
If I am using a bean within a JSP (with a <jsp:useBean> , how can I get access to said bean within a servlet that the JSP posts to?
Thanks!
(I'm thinking that setAttribute() get Attribute() will work, but I was hoping for something a little more elegant...)
 
Roy Ben Ami
Ranch Hand
Posts: 732
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
the only way i know is through the getAttribute() method you mentioned.
the "easy" tag way in jsp is very fun , but when going back to same old java classes (servlets) we have to use the whole method routine.
bummer
 
Andrew Sweeney
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Roy,
Now if I may add a follow up... In the JSP, after going through a couple of pages that populate the fields of the bean, I arrive at the final page. How do I get the now-full bean into the session object? What do I pass in to the setAttribute method? Code sample to follow...

(The bean is already established with session scope, so I don't have to useBean it)
 
Roy Ben Ami
Ranch Hand
Posts: 732
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
you dont need to do that.
the minute you set up the bean with the useBean tag it goes to the scope you mentioned - in this case the session!
it does the setAttribute thing itself (thats how it passes the bean all the time between the jsp pages).
so its even easier than you thought.
 
Guoqiao Sun
Ranch Hand
Posts: 317
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think you can share the bean between JSP and Servlet using request, session as well as application scope. In other word, you create the bean in JSP, and put it in either request, session or application scope, you can access the bean in the following Servlet by using
YourBean bean = (YourBean) request.getAttribute("bean-id");
YourBean bean = (YourBean) session.getAttribute("bean-id");
YourBean bean = (YourBean) application.getAttribute("bean-id");
regards,

Originally posted by Andrew Sweeney:
Afternoon everybody,
If I am using a bean within a JSP (with a <jsp:useBean> , how can I get access to said bean within a servlet that the JSP posts to?
Thanks!
(I'm thinking that setAttribute() get Attribute() will work, but I was hoping for something a little more elegant...)
 
Andrew Sweeney
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Finally! I've discovered what the problem was. I was attempting to retrieve the object from another session entirely. One of my co-workers, in his (ahem) infinite wisdom, built a new session and rewrote my code to deposit my object into it. <shaking my head>
Thanks to all for your advice!
-Andy
 
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!