首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > JAVA > J2SE开发 >

一个关于清空session属性的有关问题

2012-03-06 
一个关于清空session属性的问题我想要清空session里的部分属性,但要留住我的登陆信息!代码如下:Java codeE

一个关于清空session属性的问题
我想要清空session里的部分属性,但要留住我的登陆信息!代码如下:

Java code
        Enumeration attNames = session.getAttributeNames();        while ( attNames.hasMoreElements() ) {            String attName = (String) attNames.nextElement();            if ( !attName.equalsIgnoreCase( "loginInfo" )                    && !attName.equalsIgnoreCase( "VerifyCode" )                    && !attName.startsWith( "org.apache" ) ) {                session.removeAttribute( attName );            }        }

执行的时候老是报以下错误:
Java code
javax.servlet.ServletException: java.util.ConcurrentModificationException    at org.apache.struts.action.RequestProcessor.processException(RequestProcessor.java:520)    at org.apache.struts.action.RequestProcessor.processActionPerform(RequestProcessor.java:427)    at org.apache.struts.action.RequestProcessor.process(RequestProcessor.java:228)    at org.apache.struts.action.ActionServlet.process(ActionServlet.java:1913)    at com.common.baseclass.ActionServletUTF8Encoding.process(ActionServletUTF8Encoding.java:45)    at org.apache.struts.action.ActionServlet.doGet(ActionServlet.java:449)    at javax.servlet.http.HttpServlet.service(HttpServlet.java:114)    at javax.servlet.http.HttpServlet.service(HttpServlet.java:91)    at com.caucho.server.dispatch.ServletFilterChain.doFilter(ServletFilterChain.java:103)    at com.caucho.server.cache.CacheFilterChain.doFilter(CacheFilterChain.java:192)    at com.caucho.server.webapp.WebAppFilterChain.doFilter(WebAppFilterChain.java:187)    at com.caucho.server.dispatch.ServletInvocation.service(ServletInvocation.java:266)    at com.caucho.server.http.HttpRequest.handleRequest(HttpRequest.java:280)    at com.caucho.server.port.TcpConnection.handleConnection(TcpConnection.java:563)    at com.caucho.server.port.TcpConnection$AcceptTask.doAccept(TcpConnection.java:1122)    at com.caucho.server.port.TcpConnection$AcceptTask.run(TcpConnection.java:1069)    at com.caucho.util.ThreadPool$Item.runTasks(ThreadPool.java:759)    at com.caucho.util.ThreadPool$Item.run(ThreadPool.java:681)    at java.lang.Thread.run(Thread.java:619)Caused by: java.util.ConcurrentModificationException    at java.util.Hashtable$Enumerator.next(Hashtable.java:1031)    at java.util.Collections$1.nextElement(Collections.java:3389)    at com.rmdkt.user.advice.SessionAdvice.cleanSessionAttribute(SessionAdvice.java:38)    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)    at java.lang.reflect.Method.invoke(Method.java:597)    at org.springframework.aop.aspectj.AbstractAspectJAdvice.invokeAdviceMethodWithGivenArgs(AbstractAspectJAdvice.java:597)    at org.springframework.aop.aspectj.AbstractAspectJAdvice.invokeAdviceMethod(AbstractAspectJAdvice.java:576)    at org.springframework.aop.aspectj.AspectJMethodBeforeAdvice.before(AspectJMethodBeforeAdvice.java:39)    at org.springframework.aop.framework.adapter.MethodBeforeAdviceInterceptor.invoke(MethodBeforeAdviceInterceptor.java:49)    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:171)    at org.springframework.aop.aspectj.MethodInvocationProceedingJoinPoint.proceed(MethodInvocationProceedingJoinPoint.java:77)    at com.rmdkt.user.advice.LoginValidateAdvice.actionLoginValidate(LoginValidateAdvice.java:68)    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)    at java.lang.reflect.Method.invoke(Method.java:597)    at org.springframework.aop.aspectj.AbstractAspectJAdvice.invokeAdviceMethodWithGivenArgs(AbstractAspectJAdvice.java:597)    at org.springframework.aop.aspectj.AbstractAspectJAdvice.invokeAdviceMethod(AbstractAspectJAdvice.java:583)    at org.springframework.aop.aspectj.AspectJAroundAdvice.invoke(AspectJAroundAdvice.java:60)    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:171)    at org.springframework.aop.interceptor.ExposeInvocationInterceptor.invoke(ExposeInvocationInterceptor.java:89)    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:171)    at org.springframework.aop.framework.Cglib2AopProxy$DynamicAdvisedInterceptor.intercept(Cglib2AopProxy.java:629)    at com.rmdkt.user.company.struts.action.UserMgrShowMyselfEstateBusinessListAction$$EnhancerByCGLIB$$6ac69521.execute(<generated>)    at org.apache.struts.action.RequestProcessor.processActionPerform(RequestProcessor.java:425)    at org.apache.struts.action.RequestProcessor.process(RequestProcessor.java:228)    at org.apache.struts.action.ActionServlet.process(ActionServlet.java:1913)    at com.common.baseclass.ActionServletUTF8Encoding.process(ActionServletUTF8Encoding.java:45)    at org.apache.struts.action.ActionServlet.doGet(ActionServlet.java:449)    at javax.servlet.http.HttpServlet.service(HttpServlet.java:114)    at javax.servlet.http.HttpServlet.service(HttpServlet.java:91)    at com.caucho.server.dispatch.ServletFilterChain.doFilter(ServletFilterChain.java:103)    at com.caucho.server.cache.CacheFilterChain.doFilter(CacheFilterChain.java:192)    at com.caucho.server.webapp.WebAppFilterChain.doFilter(WebAppFilterChain.java:187)    at com.caucho.server.dispatch.ServletInvocation.service(ServletInvocation.java:266)    at com.caucho.server.http.HttpRequest.handleRequest(HttpRequest.java:280)    at com.caucho.server.port.TcpConnection.handleConnection(TcpConnection.java:563)    at com.caucho.server.port.TcpConnection$AcceptTask.doAccept(TcpConnection.java:1122)    at com.caucho.server.port.TcpConnection$AcceptTask.run(TcpConnection.java:1069)    at com.caucho.util.ThreadPool$Item.runTasks(ThreadPool.java:759)    at com.caucho.util.ThreadPool$Item.run(ThreadPool.java:681)    at java.lang.Thread.run(Thread.java:619) 


循环到最后一个属性时,attNames.hasMoreElements()为真,可以进入到循环体内。但是执行attNames.nextElement()就报以上错误了。郁闷至极,网上查了半天也没有查到为什么!只有人说过关于Iterator报以上错误的解决办法。
有谁碰到过以上的问题吗?

[解决办法]
探讨
会不会是你remove后,改变了session的attributeMap内的元素呢 ?

试试这样:

Enumeration attNames = session.getAttributeNames();
while ( attNames.hasMoreElements() ) {
String attName = (String) attNames.nextElement();
if ( !attName.equalsIgnoreCase( "loginInfo" )
&& !attName.equalsIgnoreCase( "VerifyCode" )
&& !at…

热点排行