我继承了一些代码,并在尝试运行时遇到错误.错误如下:
10:08:32,093 ERROR [MyServlet]:260 - Servlet.service() for servlet MyServlet threw exception java.lang.IllegalArgumentException: setAttribute: Non-serializable attribute at org.apache.catalina.session.StandardSession.setAttribute(StandardSession.java:1270) at org.apache.catalina.session.StandardSession.setAttribute(StandardSession.java:1248) at org.apache.catalina.session.StandardSessionFacade.setAttribute(StandardSessionFacade.java:130) at uk.co.my.servlet.MyServlet.doPost(MyServlet.java:121) at javax.servlet.http.HttpServlet.service(HttpServlet.java:647) at javax.servlet.http.HttpServlet.service(HttpServlet.java:729) at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:269) at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:188) at com.my.ddi.security.SecurityContextServletFilter.doFilter(SecurityContextServletFilter.java:55) at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:215) at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:188) at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:213) at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:172) at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:433) at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127) at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:117) at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:108) at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:174) at org.apache.coyote.http11.Http11AprProcessor.process(Http11AprProcessor.java:835) at org.apache.coyote.http11.Http11AprProtocol$Http11ConnectionHandler.process(Http11AprProtocol.java:640) at org.apache.tomcat.util.net.AprEndpoint$Worker.run(AprEndpoint.java:1286) at java.lang.Thread.run(Unknown Source)
我想主要的问题是它在uk.co.my.servlet.MyServlet.doPost(MyServlet.java:172)上击中了我的代码.MyServlet的第121行是session.setAttribute("LISTENER",myListener);
此myListener对象来自以下类:
public class myListener { static Log log = LogFactory.getLog(TerminationListener.class.getName()); private boolean shouldCancel = false;
我该怎么做才能摆脱这个错误?我需要使用transient关键字吗?
如果要将实例myListener
放在会话中,则需要使其可序列化.这可能涉及使一些字段瞬态,但那些字段将不会保存在会话中.您还需要实现类实现Serializable
.
我建议您查看Serializable的文档,其中详细介绍了序列化.