John,

A word of warning.  You very rarely want to declare ANY variable in a JSP
using the <%!  %> tags.  By doing so, you are in essence declaring a class
member variable to the servlet.  This can give you concurrency problems on a
multi-user product.  You usually want to declare a variable in a jsp page as
local in scope, i.e. as a local member of public void
_jspService(HttpServletRequest request, HttpServletResponse response) method
of servlet class.  You declare a local variable in a jsp as follows:

<% String lid; %>

And on a second note, a java class bean differs from a "traditional" java
class in that it can take NO parameters in the constructor, and it has no
visual interface components.  That is what differentiates a java class bean
from a standard java class.  All classes, be they Java or C++, have "getter"
and "setter" functions, so that in itself does not differentiate a bean from
a standard Java class.  Also note that a Java bean, which has a visual
interface and runs in a "container", is different than the java "class
beans" we are discussing.  In our shop, we differentiate between the two by
using the definition "class bean" vs. "bean", since many people use the word
"bean" interchangeable between the two.

Hope this helps clear up some of your questions.

Celeste




-----Original Message-----
From: John Kilbourne [mailto:[EMAIL PROTECTED]]
Sent: Monday, December 10, 2001 4:07 PM
To: [EMAIL PROTECTED]
Subject: Re: newbie wanting to pass data from servlet to jsp


Thanks Joe. Both your responses are very helpful to me. If I store my
resultset in an Employee[] (or Vector), to avoid having to keep my
connection open, you're saying I can store it in a session attribute.
I presume I would have to declare the Employee[] using a declaration
(<%!...%> before accessing it in the jsp using
<%Employee[] e=(Employee[])session.getAtrribute("allemployees");%>

When would I use a bean to store this data, either record-by-record
(public String getFirstName(), public void setFirstName(String first))

or as a collection
public Employee[] getRecords(), public void setRecords(Employee[])?

The second is less straightforward, but may be a possibility to store
the collection for later retrieval. I only today learned that beans
are classes with getters and setters; I didn't know I had been using
them for months. Anyway, the tag syntax of a bean seems to be another
way to get the data, if the bean can be "set" in a servlet and
retrieved in a jsp. Is there a clearer way to think about beans in
the context of passing info from a servlet to a jsp?

John



------------------ Reply Separator --------------------
Originally From: Joe Cheng <[EMAIL PROTECTED]>
Subject:  Re: newbie wanting to pass data from servlet to jsp
Date: 12/10/2001 03:47pm


You can store the objects you want to pass in the request scope.

public void request.setAttribute(String attrname, Object value)
public Object request.getAttribute(String attrname)

Your servlet code would look like:

 request.setAttribute("myData", rs);
 request.getRequestDispatcher("path_to_jsp").forward(request,
response);

Your JSP code would look like this:

 ResultSet rs = (ResultSet)request.getAttribute("myData");

If you want to store the object for a larger scope than just the
current
request, you can store it in the session (same syntax).

"<%@ page import=" is like import statements in your Java classes.
For
example, if your servlet has this:

import java.util.*;
import java.sql.ResultSet;

Your JSP equivalent would be this:

<%@ page import="java.util.*,java.sql.ResultSet" %>

-jmc

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

 http://archives.java.sun.com/jsp-interest.html
 http://java.sun.com/products/jsp/faq.html
 http://www.esperanto.org.nz/jsp/jspfaq.jsp
 http://www.jguru.com/faq/index.jsp
 http://www.jspinsider.com

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

 http://archives.java.sun.com/jsp-interest.html
 http://java.sun.com/products/jsp/faq.html
 http://www.esperanto.org.nz/jsp/jspfaq.jsp
 http://www.jguru.com/faq/index.jsp
 http://www.jspinsider.com

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

 http://archives.java.sun.com/jsp-interest.html
 http://java.sun.com/products/jsp/faq.html
 http://www.esperanto.org.nz/jsp/jspfaq.jsp
 http://www.jguru.com/faq/index.jsp
 http://www.jspinsider.com

Reply via email to