Hello Everyone:

I have recently posted some messages regarding a class cast exception I was
getting when using the multiple="true" attribute with the bean:cookie tag.

To resolve this problem in my environment (Visual Age for Java and Tomcat
3.2), I changed org.apache.struts.taglib.bean.CookieTei to:

    /**
     * Return information about the scripting variables to be created.
     */
    public VariableInfo[] getVariableInfo(TagData data) {

        //Object type = null;
        String type = null;
        if (data.getAttribute("multiple") == null)
        {
                type = "javax.servlet.http.Cookie";
                //type = new Cookie("name", "value");
                if( data.getAttribute("name") == null ){
                        type = "javax.servlet.http.Cookie[]";
                }
        }
        else
            //type = new Cookie[0];
            type = "javax.servlet.http.Cookie[]";
        return new VariableInfo[] {
          new VariableInfo(data.getAttributeString("id"),
                           //type.getClass().getName(),
                           type,
                           true,
                           VariableInfo.AT_BEGIN)
        };

    }

The basic change is Object type = null to String type = null. If you follow
the code from there on, I'm sure you can see the rest of the change. The
original code I left commented out with a single line comment.

You might also notice that inside the if there is:

if( data.getAttribute("name") == null ){
        type = "javax.servlet.http.Cookie[]";
}

What I did with this tag is that I changed my tld to not make the name
attribute mandatory so that if it is ommited, the tag makes available to the
page context all of the cookies in the user's browser.

My other change was to the doStartTag method of
org.apache.struts.taglib.bean.CookieTag:

    // --------------------------------------------------------- Public
Methods


    /**
     * Retrieve the required property and expose it as a scripting variable.
     *
     * @exception JspException if a JSP exception has occurred
     */
    public int doStartTag() throws JspException {

        // Retrieve the required cookie value(s)
        ArrayList values = new ArrayList();
        Cookie[] cookies =
            ((HttpServletRequest) pageContext.getRequest()).getCookies();
        if (cookies == null)
            cookies = new Cookie[0];

                //Return all cookies if no name is specified
                if(name == null ){
                        for( int i = 0; i < cookies.length; i++ ){
                                values.add(cookies[i]);
                        }
                        pageContext.setAttribute(id, (Cookie[]) 
values.toArray(cookies));
                }
                else{

                for (int i = 0; i < cookies.length; i++) {
                if (name.equals(cookies[i].getName()))
                        values.add(cookies[i]);
                }
                if ((values.size() < 1) && (value != null))
                        values.add(new Cookie(name, value));
                if (values.size() < 1) {
                        JspException e = new JspException
                        (messages.getMessage("cookie.get", name));
                         RequestUtils.saveException(pageContext, e);
                throw e;
                }

                // Expose an appropriate variable containing these results
                if (multiple == null) {
                Cookie cookie = (Cookie) values.get(0);
                pageContext.setAttribute(id, cookie);
                } else {
                cookies = new Cookie[values.size()];
                pageContext.setAttribute(id, (Cookie[])
values.toArray(cookies));
                }
                }
        return (SKIP_BODY);

    }

But I am still curious if anyone has had any problems with the
multiple="true" attribute of the cookie tag. The only way I could get it to
work is to change the TEI class as I explained above.

Best wishes,

**********************************************
Juan Alvarado
Internet Developer -- Manduca Management
(786)552-0504
[EMAIL PROTECTED]
AOL Instant Messenger: [EMAIL PROTECTED]


--
To unsubscribe, e-mail:   <mailto:[EMAIL PROTECTED]>
For additional commands, e-mail: <mailto:[EMAIL PROTECTED]>

Reply via email to