addCookie()在Tomcat 7中不起作用

时间:2014-06-19 03:16:51

标签: java jsp tomcat servlets cookies

目前我正在将我的应用程序从Tomcat 6迁移到Tomcat 7.在我的流程中,有一部分是在servlet方法中将servlet中的cookie添加到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);
        }


    }

catalina.policy里

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

如果我遗失任何东西,你可以告诉我吗?

1 个答案:

答案 0 :(得分:0)

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

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

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