You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@tomcat.apache.org by ma...@mjwilcox.com on 2000/06/14 04:14:18 UTC
session variable in taglib
Hi,
Just wondering if anyone has any idea on how to store and retrieve
a session scoped variable in a taglib class (as in teh Java code
that makes up the taglib, not the JSP).
thanks,
Mark
Re: Passing In Objects (was:session variable in taglib)
Posted by "Craig R. McClanahan" <Cr...@eng.sun.com>.
Jon Baer wrote:
> "Craig R. McClanahan" wrote:
>
> > Now, you can execute something like this:
> >
> > MyObject object = (MyObject) pageContext.getAttribute("mykey",
> > PageContext.SESSION_SCOPE);
>
> Im interested in how many ways there are to pass an object into a JSP taglib other
> than the session or ie, how to get a bean object, for example:
>
> <jsp:useBean id="myBean" class="com.myBean" ...
> <jsp:setProperty name="myBean" property="prop" value="<%=
> request.getParameter("name") %>"...
>
> Now I want to use myBean in a taglib, should it be throw into the session and
> grabbed from the taglib? Or is there another option to use? Is it available via
> PageContext? Is the method you provided the normal way? Is everything treated
> like attribute or can it be made available inside the tag?
>
If you use <jsp:useBean> to create your bean, you either explicitly or implicitly
assign it to a particular scope (the default is page scope). For each of those cases,
you can access it from a custom tag using an appropriate second argument to the
PageContext.getAttribute() method. See the JavaDoc documentation for the JSP 1.1
classes for more details.
Besides searching a particular scope with PageContext.getAttribute(), you can also
call PageContext.findAttribute() to find the bean in *any* scope -- it starts from the
most restrictive and gradually expands outward (page, request, session, application)
until it finds, or does not find, an attribute with the specified key.
>
> <tag:myTag class="myBean"/>
>
> Thanks in advance.
>
Check the Javadocs for the PageContext class -- you will find that you have access,
from within a custom tag's class, to pretty much anything you could possibly want.
>
> - Jon
>
Craig McClanahan
Re: Passing In Objects (was:session variable in taglib)
Posted by Jon Baer <jo...@digitalanywhere.com>.
"Craig R. McClanahan" wrote:
> Now, you can execute something like this:
>
> MyObject object = (MyObject) pageContext.getAttribute("mykey",
> PageContext.SESSION_SCOPE);
Im interested in how many ways there are to pass an object into a JSP taglib other
than the session or ie, how to get a bean object, for example:
<jsp:useBean id="myBean" class="com.myBean" ...
<jsp:setProperty name="myBean" property="prop" value="<%=
request.getParameter("name") %>"...
Now I want to use myBean in a taglib, should it be throw into the session and
grabbed from the taglib? Or is there another option to use? Is it available via
PageContext? Is the method you provided the normal way? Is everything treated
like attribute or can it be made available inside the tag?
<tag:myTag class="myBean"/>
Thanks in advance.
- Jon
Re: session variable in taglib
Posted by "Craig R. McClanahan" <Cr...@eng.sun.com>.
mark@mjwilcox.com wrote:
> Hi,
>
> Just wondering if anyone has any idea on how to store and retrieve
> a session scoped variable in a taglib class (as in teh Java code
> that makes up the taglib, not the JSP).
>
If your custom tag extends TagSupport or BodyTagSupport (the usual case), then
you have an instance variable named "pageContext" that has been set to the
PageContext for this tag.
Now, you can execute something like this:
MyObject object = (MyObject) pageContext.getAttribute("mykey",
PageContext.SESSION_SCOPE);
There are also constants for the second argument to access the various other
scopes as well.
>
> thanks,
>
> Mark
>
Craig McClanahan
Re: session variable in taglib
Posted by Volker Turau <tu...@informatik.fh-wiesbaden.de>.
Hi,
see the following example on how to store and retrieve scoped
variables in a taglib classes:
http://shannon.informatik.fh-wiesbaden.de/buch/codedoc/linkCode.jsp
The tag handler NextTag inserts and grabs an object with application
scope. To see the application check
http://shannon.informatik.fh-wiesbaden.de/buch/tags/datei1.jsp
hope that helps!
to see all my examples online (including source code) check
http://shannon.informatik.fh-wiesbaden.de/buch/index.html
volker turau
FH Wiesbaden Fachbereich Informatik
Tel.: +49-611-9495-205 FAX +49-611-9495-210
http://www.informatik.fh-wiesbaden.de/~turau