当您在会话中存储对象并在新部署后尝试访问它时,捕获 ClassCastException 吗?
我面临的情况是,如果我在会话中存储表单,在进行新的战争部署并尝试访问该表单后,我会收到 java.lang.ClassCastException。
为了使这对用户透明,我编写了以下代码:
try {
command = (ReservationOfBooksCommand) request.getPortletSession().getAttribute(RESERVATION_OF_BOOKS_COMMAND_SESSION_NAME);
} catch (ClassCastException e) {
request.getPortletSession().removeAttribute(RESERVATION_OF_BOOKS_COMMAND_SESSION_NAME);
}
但不确定是否有更优雅的替代方案,因为我不喜欢捕获 RuntimeExceptions 并且不想每次部署新战争时都重新启动服务器。
谢谢。
I'm facing a situation where if I stored a form in session, after making a new deployment of a war and trying to access the form, I get a java.lang.ClassCastException.
In order to make this transparent to the user, I wrote the following code:
try {
command = (ReservationOfBooksCommand) request.getPortletSession().getAttribute(RESERVATION_OF_BOOKS_COMMAND_SESSION_NAME);
} catch (ClassCastException e) {
request.getPortletSession().removeAttribute(RESERVATION_OF_BOOKS_COMMAND_SESSION_NAME);
}
But not sure if there is a more elegant alternative as I don't like catching RuntimeExceptions and don't want to restart the server every time I deploy a new war.
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以使用instanceof运算符
You can use the instanceof operator
由于您已使用
tomcat
标记了此问题,因此我建议您:META-INF/context.xml
。示例:
正如 Michael Barker 所说,默认情况下,大多数应用程序服务器会在重新部署后清除请求和会话。
要允许 Tomcat 存储会话,请设置
saveOnRestart="true"
。这允许 tomcat 使用PersistentManager
将会话存储在FileStore
中(也就是说,使用文件存储系统而不是数据库存储系统)。希望这有帮助。
Since you've tagged this question with
tomcat
, I suggest that you:META-INF/context.xml
in your own web application.Sample:
As Michael Barker said, most application servers cleans requests and sessions after a redeployment, by default.
To allow Tomcat to store the session, set
saveOnRestart="true"
. This allows tomcat to store the session usingPersistentManager
in aFileStore
(meaning, use a file storage system instead of a database storage system).Hope this helps.
某些应用程序服务器允许在重新部署之前清除会话。例如 Resin 将继续使用旧代码旧会话,将新会话移至新代码。显然,您需要在某个时候使用户会话过期。
Some app servers allow the sessions to be cleared down before redeploying. Resin for example will continue to use the old code for the old session, moving new sessions onto new code. Obviously you will need to expire the users session at some point.