目前我正在将我的应用程序从 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/