"Shun, Vadim" wrote:

> Hi,
>
> At this moment I am a little confused about the technique of useBean in JSP
> if collections are passed.
> It would be probably a typical situation when the controller/action servlets
> pass back to JSP some collection, for instance a Vector or ArrayList
> containing generated beans.
> If you use a session for servlet-JSp communication, your code might look
> something along these lines:
>
> // Action implementation class code
>     HttpSession session = req.getSession (false);
>     if (session == null) {
>       res.sendRedirect("http://localhost:8080/error.html");
>     }
>     String myAttribute = (String)session.getAttribute("myAttrname");
> //  do some calculations
>     ArrayList myResponce = ...
>     session.setAttribute ("myResponseName", myResponce );
>         // pass execution and bean to display JSP
>     String url="/jsp/myJsp.jsp";
>     ServletContext sc = servlet.getServletContext();
>     RequestDispatcher rd = sc.getRequestDispatcher (url);
>     rd.forward (req, res);
>
> // JSP code
>   ArrayList responseAttr = (ArrayList)
> session.getAttribute("myResponseName");
>   if (responseAttr != null && (responseAttr .size() >0)) {
>         // work with the bean here, draw HTML table containing returned data
> or whatever
> }
>
> While I understand the benefits of using request instead of session, I
> cannot figure out the syntax on JSP to get the request, i.e. if Action class
> stores response class in request and not in session. Is it just the same as
> for session, i.e. instead of calling session.getAttribute we retrieve a
> collection from request.getAttribute, and then iterate through the
> collection collecting bean properties?
>

Yes.  The only difference is that request attributes are thrown away after
completion of this request (that is, they become available for garbage collection
if you have no other live references to those objects).  Session attributes remain
valid until you remove them, or until the session is invalidated (or times out).

For a single bean, as opposed to a collection, you can use a <jsp:useBean id="xxxx"
scope="request"/> element to refer to a request attribute.

>
> BTW, what Craig names Action interface appears to me to resemble pretty
> close a Command pattern fro GoF book. Yes, and it has a method Execute.
>

Pretty much the same idea, adapted slightly to reflect the fact that it is a "web
application command", not just a "command".

>
> Vadim.
>

Craig


>
> -----Original Message-----
> From: Craig R. McClanahan [mailto:[EMAIL PROTECTED]]
> Sent: Saturday, March 25, 2000 10:03 PM
> Subject: Re: Model 2 questions
> ...
> Yes, the <jsp:useBean> in the JSP page grabs it back out.  You have to match
> on
> the scope where your servlet placed the bean, as well as the name you stored
> it
> under.  In the case above, it would be something like:
>
>     <jsp:useBean id="MyBeanName" scope="request" class="....."/>
>
> or
>
>     <jsp:useBean id="MyBeanName" scope="request" type="....."/>
>
> (The latter is nice because you can use an interface if you've got
> polymorphic
> beans -- for the usual case I just use class= instead.)
> ...
>
> ===========================================================================
> 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

===========================================================================
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