【发布时间】:2014-06-19 11:27:38
【问题描述】:
目前我正在将我的应用程序从 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);
}
}
catalina.policy
// Permission the examples/samples - see the context xml files
grant codeBase "file:${catalina.home}/webapps/CreationWeb/-" {
permission java.security.AllPermission;
};
如果我遗漏了什么,请告诉我吗?
【问题讨论】:
-
cookieName来自哪里以及在哪里定义? -
setMaxAge()会帮助你我认为.. 在response.addCookie();之前尝试一下 -
cookieName 来自一个全局变量。我已经使用调试器进行了检查,并且即将到来。让我用 setMaxAge() 检查一下。
-
好的,像这样:
cookie.setMaxAge(60*60); //1 hour
标签: java jsp tomcat servlets cookies