Henric Larsson wrote:

> Hi,
>
> how can I control the name of a jsp bean in a session or application.
>
> If I create a bean like this:
>
> <jsp:useBean id="voteEngine" scope="application"
> class="nation.app.vote.VoteEngine" >
>  <jsp:setProperty name="voteEngine" property="database" value="rosta" />
> </jsp:useBean>
>
> how do I know what name the bean gets if I want to use the bean in a servlet
> like this:
>
> ServletContext.getAttribue("voteEngine");
>

That's exactly what happens -- the ID attribute of your <jsp:useBean> tag is the
attribute name you should use in your servlet, in the appropriate scope (i.e.
application scope attributes are in ServletContext, session scope attributes are in
HttpSession, and request scope attributes are in HttpServletRequest).  You can also
create such a bean in a servlet, store it in the appropriate spot, and access it in
a JSP page.

>
> I want a servlet to create the bean and then pull it from the application
> object in my jsp file, but I want the attribute name to be:
> nation.app.vote.voteEngine
>

Unfortunately, there is a gotcha here.  The ID tag is used for the name of the
attribute, but it is also used as the name of the local variable declared within
the generated servlet for your JSP page, and Java variable names cannot have
periods in them :-(.  I think it would be a good idea in the next JSP spec revision
to add another attribute to <jsp:useBean> that lets you use a different attribute
name if you want to (but defaults to the current behavior if not specified).

>
> .henric
>

Craig McClanahan

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