I've had problems with similar examples in the same book.  It is
indeed a scope problem.  If you examine the source code of the generated
servlet, you'll probably find that the <%= employee.getId() %> expressions
are accessing a local variable 'employee', while the <jsp:getProperty
name="employee" property="id"/> actions are trying to access an attribute
'employee' of either the request or the pageContext object.

        If you want to continue using bean tags, try replacing

employee = (EmployeeBean)i.next();

with something like:

pageContext.setAttribute("employee", (EmployeeBean)i.next(),
pageContext.REQUEST_SCOPE);

        What are you using for a server?  I've been using Tomcat 3.1.

-----Original Message-----
From: John Cartwright [mailto:[EMAIL PROTECTED]]
Sent: Tuesday, June 06, 2000 5:29 PM
Subject: passing bean from servlet to jsp


Hello All,

I am trying to understand an example from Chapter 8 of the WDJSP by
Fields&Kolb that relates to passing a bean from a servlet to a jsp page.
<Code listing below>.  The accessor methods work OK, but the getProperty
tags do not.  They behave like they are referring to an empty bean.  I'm
guessing that there is some sort of scope problem here, but I don't see
it.

The jsp graps a vector beans from the request and then prints the
properties from each one.

Any help would be much appreciated!


-- john



<body>
<b>Current Employees</b>
<ul>
<%
  Vector v = (Vector)request.getAttribute("list");

  Iterator i= v.iterator();
  while (i.hasNext()) {
     employee = (EmployeeBean)i.next();
%>
<li>
<a href="/webdev/servlet/FetchEmployeeServlet?cmd=get&id=
<%= employee.getId() %>">
<%= employee.getLastName() %>,
<%= employee.getFirstName() %>
</a>
<jsp:getProperty name="employee" property="id"/>
<jsp:getProperty name="employee" property="lastName"/>,
<jsp:getProperty name="employee" property="firstName"/>
<% } %>
</ul>
</body>
</html>






============================================================================
===
John Cartwright
Professional Research Assistant / Associate Scientist
CIRES, SEG/NGDC/NOAA
(303) 497-6284
[EMAIL PROTECTED]
============================================================================
===

===========================================================================
To unsubscribe: mailto [EMAIL PROTECTED] with body: "signoff JSP-INTEREST".
Some relevant FAQs on JSP/Servlets can be found at:

 http://java.sun.com/products/jsp/faq.html
 http://www.esperanto.org.nz/jsp/jspfaq.html
 http://www.jguru.com/jguru/faq/faqpage.jsp?name=JSP
 http://www.jguru.com/jguru/faq/faqpage.jsp?name=Servlets

Reply via email to