• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

MVC2.0 how to pass bean object to jsp

 
harish pathak
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All,

Anybody please tell me how to pass bean class object from controller to jsp ?

I am developing an application in MVC2.0.
I am creating an object of bean class in controller servlet and want to pass this object to jsp so anybody please tell how to pass this object to jsp.

-----------------------------
ControllerServlet--

Bean obj = new Bean();
-----------------------------
Bean --

public String aMethod() {

return data;
}

--------------------
I have to use this method in my jsp and I am creating an object of this bean in controller servlet.

Please help

Thanks

Please reply soon
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you follow the javabean conventions for naming accessors and mutators (setters and getters) and give them names that start with get, set, or is, you can access your bean properties with either the <jsp:getProperty ../> tags or with the newer EL expression tags ${beanName.firstName}.
If you don't follow those conventions, you will need to resort to scriptlet expression tags <%=beanName.getFirstName()%>.

In any case, the way to share beans between servlets and JSPs is to bind them to one of the scope objects (request, session, context) with the get/setAttribute methods.

See
java.lang.String,%20java.lang.Object&#41;" target="_blank" rel="nofollow">http://java.sun.com/j2ee/1.4/docs/api/javax/servlet/ServletRequest.html#setAttribute(java.lang.String,%20java.lang.Object)
java.lang.String&#41;" target="_blank" rel="nofollow">http://java.sun.com/j2ee/1.4/docs/api/javax/servlet/http/HttpSession.html#getAttribute(java.lang.String)
java.lang.String&#41;" target="_blank" rel="nofollow">http://java.sun.com/j2ee/1.4/docs/api/javax/servlet/ServletContext.html#getAttribute(java.lang.String)

If you aren't using JSP2.0 you should also read up on the <jsp:useBean .. /> tag which provides a very easy way to access session scoped beans from your JSPs.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic