Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Create JavaBean in Servlet??

Status
Not open for further replies.

loewen

Programmer
Sep 16, 2002
1
CA
I would like to create a servlet that:
1. does some processing
2. stores the results in a JavaBean
3. forwards to a .jsp

The .jsp page will read the results from the bean and display the formatted results.

How do you instantiate a JavaBean from within a servlet that will be accessible to the jsp? I know how to use the <jsp:useBean> tag to create a bean to share info between .jsp pages.

Here's what I have so far:

(servlet)
..do processing..
MyBean bean = new MyBean();
bean.setTitle = &quot;Title of Document&quot;;
bean.setId = &quot;ID of document&quot;;
RequestDispatcher dispatcher = getServletContext().
getRequestDispatcher(&quot;app/display.jsp&quot;);
dispatcher.forward(request,response);

(jsp)
...
<jsp:useBean id=&quot;bean&quot;
class=&quot;myapp.MyServlet&quot;
scope=&quot;session&quot;>
title:<br>
<jsp:getProperty name=&quot;bean&quot; property=&quot;title&quot;><br>
id:<br>
<jsp:getProperty name=&quot;bean&quot; property=&quot;id&quot;><br>


The display.jsp page displays blanks for all properties. So it must be instantiating a new bean, instead of accessing the one I tried to create in my servlet.

Obviously i'm missing something pretty basic here. Can anyone help?

--dave
 
Having the same problem with a book example (Manning's Web Development with Java Server Pages, pp. 256-261.).
This example:
1.
uses a servlet to access a database (successfully gets 101 records) and loads them into a vector called 'list' containing beans of class EmployeeBean and does a setAttribute(&quot;list&quot;,list) to provide the vector to the JSP.
2. forward to JSP page
3. jsp page does a <jsp:useBean id=&quot;employee&quot; class=...>
4. jsp does a loop on the vector as follows:
<%
Vector v = (Vector)request.getAttribute(&quot;list&quot;);
Iterator i = v.iterator();
while (i.hasNext()) {
employee = (EmployeeBean)i.next();
%>
<jsp:request.getProperty name=&quot;employee&quot; property=&quot;id&quot;/>,
<jsp:request.getProperty name=&quot;lastName&quot; property=&quot;loginId&quot;/>
<%
}
%>

The output is 101 initialized records(id = 0, lastName=&quot;&quot;).
Sounds like scope is the issue, but changing scope of the useBean does nothing.

Let me know if you can figure this out.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top