On Fri, 16 Aug 2002, Les Parker wrote:
> Date: Fri, 16 Aug 2002 10:57:03 -0600
> From: Les Parker <[EMAIL PROTECTED]>
> Reply-To: Tomcat Developers List <[EMAIL PROTECTED]>
> To: [EMAIL PROTECTED]
> Subject: Re: Adding a cookie via a filterChain...
>
> Thanks Craig for your help.
>
> Here is what does'nt seem to work... Any ideas??
>
This should really be moved over to TOMCAT-USER where it belongs ... but
does your servlet/JSP page use forwarding? If so the cookie you create
will still get wiped out.
Craig
> public final class SessionControlFilter implements Filter
> {
> private String attribute = null;
> private FilterConfig filterConfig = null;
> public void init(FilterConfig filterConfig) throws ServletException
> {
> this.filterConfig = filterConfig;
> this.attribute = filterConfig.getInitParameter("attribute");
> sessionController = SessionController.getInstance();
> }
> public void doFilter(ServletRequest request, ServletResponse response,
>FilterChain chain)
> throws IOException, ServletException
> {
> if (attribute != null) request.setAttribute(attribute, this);
> if(getCookie((HttpServletRequest)request, "CHOCLATE_CHIP") == null)
> {
> makeCookie((HttpServletResponse)response, "CHOCLATE_CHIP", "CLIENT", "666");
> }
> chain.doFilter(request, response);
> }
> public static Cookie getCookie(HttpServletRequest request, String name)
> {
> Cookie[] cookies = request.getCookies();
> Cookie myCookie = null;
> for (int i=0;i<cookies.length ;i++ )
> {
> if (cookies[i].getName().equals(name))
> {
> myCookie = cookies[i];
> break;
> }
> }
> return myCookie;
> }
> public void makeCookie(HttpServletResponse response, String name, String channel,
>String visitorId)
> {
> String value = channel + "|" + visitorId;
> Cookie newCookie = new Cookie(name, value);
> newCookie.setPath("/");
> newCookie.setComment("No comment.");
> newCookie.setMaxAge(315360000);
> newCookie.setVersion(1);
> response.addCookie(newCookie);
> }
> public void destroy()
> {
> this.attribute = null;
> this.filterConfig = null;
> }
> }
>
> This electronic mail transmission may contain confidential information and is
>intended only for the person(s) named. Any use, copying or disclosure by any other
>person is strictly prohibited. If you have received this transmission in error,
>please notify the sender via e-mail.
>
>
--
To unsubscribe, e-mail: <mailto:[EMAIL PROTECTED]>
For additional commands, e-mail: <mailto:[EMAIL PROTECTED]>