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

标签 java jsp jakarta-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 的创建!

最佳答案

我意识到,当我发布此内容时,这匹马可能已经逃跑了,但是,为了其他人绊倒它的利益,我认为该问题可能与此问题中突出显示的 RequestDispatcher 功能有关:unable to add a cookie included in JSP via jsp:include

关于java - 如何在自定义标签中使用doTag方法添加cookie?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14604316/

相关文章:

jakarta-ee - 将 WildFly 9 服务器选项导出\传输到其他 PC 的正确方法

jakarta-ee - 有哪些可用的 Java EE Websockets 实现?

java - 有关 WSDL 绑定(bind)的详细信息

java - 在 Linux 服务器的 tomcat 上找不到 Struts2 操作命名空间和请求的 URL

Java:整数等于 vs. ==

java - 如何从Struts Action类发送消息到jsp页面

jquery - 如何在 JAVA 中创建具有链接、图像、列表等高级功能的评论框

jakarta-ee - 不同的 bean 作用域如何在服务器上工作?

java - 在循环中启动一个线程

java - jsp编码无法正确显示字符