I think Mr Wilson's problem was that he thought the compiled JSP held
references to the implicit objects and the beans declared. Perhaps it's
worth noting that this is not true, and that is why he needed to pass these
things into his methods.
Dan
-----Original Message-----
From: A mailing list about Java Server Pages specification and reference
[mailto:[EMAIL PROTECTED]]On Behalf Of Craig R. McClanahan
Sent: Wednesday, December 01, 1999 7:06 PM
To: [EMAIL PROTECTED]
Subject: Re: Using a bean in a public declaration
C W Wilson wrote:
> Ok. That explains a few things. I was also wondering, why can't I do the
> following?
>
> <%! public void test() throws Exception {
> Cookie[] cookies = request.getCookies();
> }
> %>
>
> Java won't allow me to request anything in there? Is there any way to
solve
> this or get by it?
>
Same solution -- pass the request object as an argument:
<%!
public void test(HttpServletRequest request) throws Exception {
Cookie[] cookies = request.getCookies();
}
%>
This is no different than programming directly in Java, C++, or any other
block-structured language -- you cannot reference a local variable in one
method from the body of a different method. Once you remember that
<jsp:useBean> tags get converted to local variables (not instance variables)
in
the generated code, it all becomes much clearer.
One thing you might want to do is turn on the "keep the generated code"
option
for whatever JSP engine you are using, and examine the source code that gets
created for a particular page. That will help you understand why what you
tried created a compile error.
Now, do you want my lecture on why you should not be embedding Java logic in
your JSP pages in the first place? :-) You will find lots of comments to
this
affect on the archives of the JSP-INTEREST and SERVLET-INTEREST mailing
lists.
>
> Thanks,
> C W
>
Craig
>
> >From: "Craig R. McClanahan" <[EMAIL PROTECTED]>
> >To: C W Wilson <[EMAIL PROTECTED]>
> >CC: [EMAIL PROTECTED]
> >Subject: Re: Using a bean in a public declaration
> >Date: Wed, 01 Dec 1999 10:30:14 -0800
> >
> >C W Wilson wrote:
> >
> > > Hi,
> > >
> > > I need to access a method in a bean, like such:
> > >
> > > ---
> > >
> > > <jsp:useBean id="beanTest" class="beans.bean"/>
> > > <%!
> > > public boolean test() throws Exception {
> > > return beanTest.returnValue();
> > > }
> > > %>
> > > <%
> > >
> > > out.println(test());
> > > %>
> > >
> > > ---
> > >
> > > What do I have to do to access beanTest in test()?
> > >
> >
> >Beans are like any other Java object -- you need an object reference to
> >refer to
> >them in a method. One way to do this would be to pass it as an argument,
> >like
> >this:
> >
> ><jsp:useBean id="beanTest" class="beans.bean" />
> ><%!
> > public boolean test(beans.bean arg) throws Exception {
> > return arg.returnValue();
> > }
> >%>
> ><%
> > out.println(test(beanTest));
> >%>
> >
> > >
> > > Thanks,
> > > C W
> > >
> >
> >Craig McClanahan
> >
> >
> >
>
> ______________________________________________________
> Get Your Private, Free Email at http://www.hotmail.com
===========================================================================
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