Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Mapping Beans to Servlets

 
Arvind Kumar Patel
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a bean and want to access it from within a Servlet. How can it be done?

For the JSP, I would probably do "<jsp:useBean id="handler" class="servlet.FormHandler" scope="session"/> ". But how would I access the "FormHandler" bean from within a Servlet?

FormHandler myBean = .... //Get it from HttpServletRequest somehow
myBean.setSomeProperty(value);
getServletConfig().getServletContext().setAttribute("handler", myBean);

Is this correct? Will the JSP be able to see the results after the Servlet updates the bean? I am looking for a <jsp:setProperty../> which can be used within a Servlet as opposed to a JSP.

EDIT: Clarification
[ April 18, 2006: Message edited by: Arvind Kumar Patel ]
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
JSPs have access to beans bound to the various scoped objects.
In older versions you had to use the <jsp:useBean .. /> tag in order to define a reference. In newer versions, you can reference them with EL regardless of the scope to which they're bound.

Any JSP tutorial worth it's salt covers access to scoped objects.


Also:
I need a way to "despatch" a bean when sending a JSP back to the client.

You do know that you don't 'send' a JSP to the client, right?
When you forward or redirect to a JSP, it's executed on the server and the resulting text is streamed to the client.
Depending on whether you forward with a requestDispatcher or use response.sendRedirect, you will have different limits as far as what scoped objects you can bind your beans to.
 
Arvind Kumar Patel
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Ben, I've updated my initial post to clarify what I'm trying to do.

EDIT: Found the answer here: http://www.experts-exchange.com/Programming/Programming_Languages/Java/Q_20143027.html
[ April 18, 2006: Message edited by: Arvind Kumar Patel ]
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you for posting back.
I'm glad you found your answer, even it it wasn't here.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic