我继承了一些代码,并在尝试运行时遇到错误。错误如下:
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关键字吗?
答案 0 :(得分:9)
如果要将myListener
的实例放在会话中,则需要使其可序列化。那个可能涉及使某些字段成为瞬态,但那些字段将不会保存在会话中。您还需要将类实现Serializable
。
我建议您查看documentation for Serializable,其中有关序列化的详细信息。