You are right in setting the scope to session. Set a name for the instance
of the bean using the id attribute of the <jsp:useBean /> tag, i.e.

<jsp:useBean id="myTestBean" scope="session" class="TestBean" />

When you put the above tag in a page, it will look for a bean in the current
session called myTestBean. If it can't find one, it will instantiate one of
class TestBean. Once a bean has been instantiated, subsequent uses of the
above tag (on different pages but in the same session) will access the same
bean.

You can also do this without using <jsp:useBean /> tags:

TestBean myTestBean = (TestBean) session.getValue("testBean");
if(myTestBean == null)  // if myTestBean doesn't exist in session yet,
instantiate one and add it to session.
{
    myStringBean = new TestBean();
    session.putValue("testBean", myTestBean);
}




> -----Original Message-----
> From: A mailing list about Java Server Pages specification and reference
> [mailto:[EMAIL PROTECTED]]On Behalf Of Daniel Tillin
> Sent: 30 November 1999 14:21
> To: [EMAIL PROTECTED]
> Subject: Re: how to access the same instance of a bean in different jsp
> pages
>
>
> Struggling with this myself. If you set the scope as session within your
> <jsp:useBean > tag, it should be available in any other page (i.e
> if you set
> a <jsp:useBean > tag in the next page, it should find the bean),
> but what if
> there could be
> many instances of the bean "alive"? The session.getValue(id) should be one
> way, but how can you know the id in the other page? I'm still
> experimenting,
> so can't say for sure. I'd be very interested if anyone out there has
> encountered and resolved this kind of process/reference flow issue??
>
> Dan
>
> -----Original Message-----
> From: A mailing list about Java Server Pages specification and reference
> [mailto:[EMAIL PROTECTED]]On Behalf Of ranjith
> Sent: Tuesday, November 30, 1999 1:01 PM
> To: [EMAIL PROTECTED]
> Subject: how to access the same instance of a bean in different jsp
> pages
>
>
> hi everyone,
>           can anyone guide me on  how to access the same bean instance
> in different jsp pages ie set a bean property in one page and access it
> in a different page.
>          ranjith
>
> ==================================================================
> =========
> To unsubscribe: mailto [EMAIL PROTECTED] with body: "signoff
> JSP-INTEREST".
> FAQs on JSP can be found at:
>  http://java.sun.com/products/jsp/faq.html
>  http://www.esperanto.org.nz/jsp/jspfaq.html
>
> ==================================================================
> =========
> To unsubscribe: mailto [EMAIL PROTECTED] with body: "signoff
> JSP-INTEREST".
> FAQs on JSP can be found at:
>  http://java.sun.com/products/jsp/faq.html
>  http://www.esperanto.org.nz/jsp/jspfaq.html
>

===========================================================================
To unsubscribe: mailto [EMAIL PROTECTED] with body: "signoff JSP-INTEREST".
FAQs on JSP can be found at:
 http://java.sun.com/products/jsp/faq.html
 http://www.esperanto.org.nz/jsp/jspfaq.html

Reply via email to