How to pass a JavaBean from servlet to JSP?
Hi there,
I am working on a project that needs to handle quite a few client requests and I want to use the Front Controller pattern that is described in the J2EE pattern page.
The steps described in the front controller pattern is that:
1. A servlet (controller) process the client request.
2. It calls the appropriate cammand object and the cammand object gets the data from the data source and returns a JavaBean that contains the required data.
3. The servlet dispatch to the appropriate View (jsp page)
4. The JSP page displays the data in the JavaBean to the user.
However, I don't know how to pass the JavaBean from the servlet to the JSP page when the servlet forwards the control to the JSP page. Should I make the JavaBean with a request scope or higher? If so, how to uses it in the JSP page? Just refer by the ID of the JavaBean.
If any one knows there is an example or tutorial on how to use this pattern, please let me know.
Thank you !
Regards
Edmund
[1045 byte] By [
yu_hung] at [2007-9-26 3:01:54]

Hi Buddy...
Let me see if I have understood you correctly. when teh servlet responds, you want add the required information to teh JSP through a javabean right? If this is the case you might wanna try the following..
1) Let the serlvet dispatch the apporpriate JSP..
2) call the java bean from within the JSP.
I think this should work..
Let me know how it goes..
Waynoo
waynoo at 2007-6-29 11:00:23 >

Before you forward your request from servlet to the jsp where you want to display the data in bean. do the following.
request.setAttribute("anyname",bean);
where anyname is the attribute name and bean is your databean.
then in jsp page you use
request.getAttribute("anyname") and store it in the desired bean instance.
e.g. If your databean classname is
com.sun.DataBean then use the following
import the above bean and then use
DataBean dataBean = (DataBean) request.getAttribute("anyname");
then u can do whatever u want with dataBean which now contains your data
I hope this answers your question
You use the RequestDispatcher to forward your objects to your jsp page.
YourBean yb = new YourBean();
request.setAttribute("yourbean",yb);
RequestDispatcher rd;
rd = getServletContext().getRequestDispatcher()."/yourjsp.jsp");
rd.forward(req,res);
where req is the HttpServletRequest and res is the HttpServletResponse,
hope this is of help,
Marcus
Could you please give more info? i tried to do this, but always get a "Class not found exception":
response$jsp.java:65: Class org.apache.jsp.TestBean not found.TestBean bean = null;
although the class TestBean (no package name => defalt package) is in the WAR file and the Servlet seems to instantiate it (otherwise an exception would occur sooner in the TestServlet code).
my JSP code is:
-
<%@ page language="java" info="Response page" %>
<jsp:useBean id="bean" class="TestBean" />
<%
TestBean bean2 = (TestBean) request.getAttribute("TheBean");
%>
<html>
<body>
Your value: <%= bean2.getValue() %>
My val: <%= bean2.getNewValue() %>
<P>
Date: <%= new Date() %>
<P>
<A HREF="index.jsp">Neuen Wert eingeben</A>
</body>
</html>
--