1

我用 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的创建!

4

2 回答 2

2

我意识到这匹马在我发布这篇文章时可能已经跑了,但是为了其他人偶然发现它,我认为问题可能与RequestDispatcher这个问题中突出显示的特性有关:无法添加包含在 JSP 中的 cookie通过jsp:包括

于 2013-09-12T22:16:05.590 回答
0

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

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

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

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

 Cookie cookie = new Cookie("SHOW_FULL_SITE",httpServletRequest.getParameter("show_full_site"));
 cookie.setMaxAge(86400); 
 cookie.setPath("/");
于 2013-01-30T13:07:43.400 回答