java - addCookie() 在 Tomcat 7 中不起作用

标签 java jsp tomcat servlets cookies

目前我正在将我的应用程序从 Tomcat 6 迁移到 Tomcat 7。在我的流程中,有一部分是在 servlet 的方法中将 cookie 从 servlet 添加到 jsp。但它既没有添加 cookie,也没有抛出任何异常。为了交叉检查它,我试图在 jsp 中强制添加 cookie。但是得到了相同的结果。

servlet.java

public void logon( String username, String password, String ip, HttpServletResponse response) throws Exception, LogonFailure {
    System.out.println(" Input Received ::"+response);
        System.out.println("Attachment :::"+getNextAttachment());

        AuthenticatorTicketWrapper wrapper = new AuthenticatorTicketWrapper( username, password, ip, "" + getNextAttachment(), this );

        // if no exception thrown then we must of managed to log on.
        String userID =  "" + this.getNextID();
        System.out.println("User Id within Logon method ::"+userID);
        wrapper.setUserName(userID);
        //wrapper.set

        m_Cache.put( userID, wrapper );
        // add cookie to the users browser



        try {
            System.out.println("Cookiename in the logon method :"+cookieName);
            response.addCookie( new Cookie(this.cookieName + this.CREATION_ID, userID) );
            response.addCookie( new Cookie(this.cookieName + this.CREATION_KEY, wrapper.getUserCookieKey() ) );
        } catch (Exception e) {
            // TODO: handle exception
            System.out.println("Failed Add cookie, Exception " + se);
        }


    }

卡特琳娜政策

// Permission the examples/samples - see the context xml files
grant codeBase "file:${catalina.home}/webapps/CreationWeb/-" {
      permission java.security.AllPermission;
};

如果我遗漏了什么,你能告诉我吗?

最佳答案

我之前在tomcat 7中使用过如下代码设置cookie

    Cookie userCookie = new Cookie("someName", "someValue");
    userCookie.setMaxAge(99999999999); 
    userCookie.setPath("/");
    resp.addCookie(userCookie);

在上面的例子中,resp 是 HttpServletResponse。此代码已经过测试,适用于 tomcat 7 和 JDK 1.7

关于java - addCookie() 在 Tomcat 7 中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24298208/

相关文章:

java 日期转字符串 - 有时是 CET,有时是 CEST

java - ProcessBuilder 未正确执行程序

java - 防止非美国字母提交表单(java)

javascript - 如何使用基于时间的javascript显示jsp页面的弹出窗口?

tomcat - 监控 catalina.out [多行日志文件]

java - 问题 java.lang.Exception : Lock wait timeout exceeded; try restarting transaction

java - Libgdx 无法为目标 linux 32 位加载共享库 gdx freetype,我该如何解决?

java - Spring data rest领域驱动设计-发布非聚合根实体

jsp - com.ibatis.common.jdbc.exception.NestedSQLException : Could not start transaction

java - JSP 中的 Jar 文件未正确加载