C�dric Janssens:

> > How do you retrieve cookies with the JSP 1.0 <jsp:useBean> </jsp:useBean>,
> > ..., <jsp:getProperty ... /> syntax ?
> >

Craig McClanahan:

> JSP pages are compiled into servlets, so you can use the normal servlet API
> capabilities to access cookies:
>
> <%
>     Cookie cookies[] = request.getCookies();
> %>
>
> Then, you can walk through the array of cookies that were included with this
> request, looking for the one you want.  See the servlet API
> (http://java.sun.com/products/servlet) for more information.

You can also write a bean that will handle the cookie stuff if you
want to hide the details from page authors:

----------------------------------------
package com.canetoad;

import javax.servlet.http.*;
import java.util.Hashtable;

public class CookieBean {
    private Hashtable cookies;

    public CookieBean() {
        cookies = new Hashtable();
    }

    public void setRequest(HttpServletRequest request) {
        Cookie _cookies[] = request.getCookies();

        for(int i=0;i<_cookies.length;i++) {
            cookies.put(_cookies[i].getName(),_cookies[i]);
        }
    }

    public String getCookieValue(String name) {
        Cookie tmp = (Cookie) cookies.get(name);
        if(tmp != null) return tmp.getValue();
        return null;
    }

    public String getMyCookie() {
        return getCookieValue("myCookie");
    }
}

----------------------------------------

You could also write similar accessors to get the domain, comments, max
age, etc.

In your JSP you could then do:

<jsp:useBean id="cookies" class="com.canetoad.CookieBean"/>

<jsp:setProperty name="cookies" property="request"
 value="<%= request %>"/>

<P>
Your cookie named myCookie has the value:
<jsp:getProperty name="cookies" property="myCookie"/>
</P>


                                                  - Larne

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