wood burning stoves 2.0*
The moose likes Servlets and the fly likes Mapping Beans to Servlets Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "Mapping Beans to Servlets" Watch "Mapping Beans to Servlets" New topic
Author

Mapping Beans to Servlets

Arvind Kumar Patel
Greenhorn

Joined: Mar 11, 2006
Posts: 24
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

Joined: Dec 11, 2004
Posts: 13410

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.


Java API J2EE API Servlet Spec JSP Spec How to ask a question... Simple Servlet Examples jsonf
Arvind Kumar Patel
Greenhorn

Joined: Mar 11, 2006
Posts: 24
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

Joined: Dec 11, 2004
Posts: 13410

Thank you for posting back.
I'm glad you found your answer, even it it wasn't here.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Mapping Beans to Servlets