如何在自定义标记中使用doTag方法添加cookie?

时间:2013-01-30 12:43:13

标签: java jsp java-ee servlets jsp-tags

我在Java中开发了一个自定义标记库,我在我的Web应用程序中使用它。

我不知道为什么,但我的doTag()根本没有设置cookie。我已经清除了缓存并重新启动了我的计算机。这是代码:

public class UserVersionOfSite extends EvenSimplerTagSupport {

    private static final Log logger = LogFactory.getLog(UserVersionOfSite.class);
    private StringWriter sw = new StringWriter();



    @Override
    public void doTag() throws IOException, JspException {

                 getJspBody().invoke(sw);   //get the tag body and put it in StringWriter object

                //get request object to get cookie value
                PageContext ctx = (PageContext)getJspContext();
                HttpServletRequest httpServletRequest = (HttpServletRequest) ctx.getRequest();
                HttpServletResponse httpServletResponse = (HttpServletResponse) ctx.getResponse();

                    if(httpServletRequest.getParameterMap().containsKey("show_full_site"))  {

                        logger.debug("show_full_site ");





                                   if(!checkIfCookieExists(httpServletRequest)){


                                         Cookie cookie = new Cookie("SHOW_FULL_SITE",httpServletRequest.getParameter("show_full_site"));
                                         cookie.setMaxAge(86400);



                                         httpServletResponse.addCookie(cookie);

                                                //write the tag output
                                               if(!httpServletRequest.getParameter("show_full_site").equalsIgnoreCase("true")){
                                                   //write the response
                                                   getJspContext().getOut().println(sw.toString());
                                               }
                                    }else{

                                       String cookieValueString = getCookieValue(httpServletRequest.getCookies(),"SHOW_FULL_SITE","false");

                                       if(!cookieValueString.equalsIgnoreCase("true")){

                                           //write the response
                                           getJspContext().getOut().println(sw.toString());

                                       }



                                    }

                    }



    }


    @Override
    public String getResult() throws IOException {

                  return "User version of site";
    }


    public String getCookieValue(Cookie[] cookies,
                                        String cookieName,
                                        String defaultValue) {
        for(int i=0; i<cookies.length; i++) {
            Cookie cookie = cookies[i];
            if (cookieName.equals(cookie.getName()))
                return(cookie.getValue());
        }
        return(defaultValue);
    }

    public boolean checkIfCookieExists(HttpServletRequest httpServletRequest){

        logger.debug("inside checkIfCookieExists()");

        boolean cookiePresent = Arrays.asList(httpServletRequest.getCookies()).contains( "SHOW_FULL_SITE" );



        return cookiePresent;
    }
}

即使我尝试添加代码而不使用if else语句但仍然没有成功。有什么关键我不知道吗?

任何想法的家伙?? !!!我也检查了浏览器的设置,但是没有什么阻止创建cookie!

2 个答案:

答案 0 :(得分:1)

我意识到这匹马可能在我发布这篇文章的时候已经狂奔但是,为了让其他人感到磕磕绊绊,我觉得这个问题可能与此问题中突出显示的RequestDispatcher功能有关: unable to add a cookie included in JSP via jsp:include

答案 1 :(得分:0)

checkIfCookieExists()方法中的以下行是错误的:

Arrays.asList(httpServletRequest.getCookies()).contains( "SHOW_FULL_SITE" );

HttpServletRequest.getCookies()返回Cookie []。您将它包装在List中并检查其中的字符串“SHOW_FULL_SITE”。

回到你的问题 - 你怎么知道HTTP标头中没有设置cookie?尝试使用像firebug这样的浏览器插件来查看来自服务器的HTTP响应标头。在将cookie添加到响应之前,还要设置cookie的路径,例如

 Cookie cookie = new Cookie("SHOW_FULL_SITE",httpServletRequest.getParameter("show_full_site"));
 cookie.setMaxAge(86400); 
 cookie.setPath("/");