【问题标题】:How to add a cookie using doTag method in custom tag?如何在自定义标签中使用 doTag 方法添加 cookie?
【发布时间】:2013-01-14 06:53:39
【问题描述】:

我用 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!

【问题讨论】:

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


    【解决方案1】:

    您在 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("/");
    

    【讨论】:

    • 我正在使用firebug,并且也检查过它,但我根本看不到cookie。而关于 checkIfCookieExists 方法,它是正确的,因为 Arrays 类中的 asList() 将数组作为参数并返回一个 List 类对象,您可以使用它来检查 contains。
    • 我的意思是 List 将包含 Cookie 类型的对象。但是您正在列表中检查字符串“SHOW_FULL_SITE”。 Cookie 对象和字符串对象不会比较相等,因此,列表将始终返回 false。您是否也通过设置路径来尝试使用 cookie?
    • 您还可以查看struts bean:cookie 标签以了解如何处理 JSP 标签内的 cookie。 bean:tag 用于读取 cookie。
    • 是的,我尝试使用 setPath() 但没有成功!应该很简单,因为我可以通过请求对象访问参数,就像我应该能够使用响应对象设置cookie一样。
    【解决方案2】:

    我意识到这匹马在我发布这篇文章时可能已经狂奔了,但是为了其他人偶然发现它的好处,我认为问题可能与此问题中突出显示的RequestDispatcher 的功能有关:@987654321 @

    【讨论】:

      猜你喜欢
      • 2022-09-27
      • 1970-01-01
      • 1970-01-01
      • 2016-10-07
      • 2021-02-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多