1、环境
tomcat6.0.8 + jdk6 + struts1
2、问题的产生
页面有两个异常的请求
http://127.0.0.1/project/a.action?do=testA&m=10 http://127.0.0.1/project/a.action?do=testB&m=10
两个请求几乎同步到达server。结果出现异常:
java.lang.NullPointerException at org.apache.catalina.connector.Request.parseParameters(Request.java:2446) at org.apache.catalina.connector.Request.getParameter(Request.java:1040) at org.apache.catalina.connector.RequestFacade.getParameter(RequestFacade.java:355) at com.tepper.common.util.JsonUtil.jsonResponse(JsonUtil.java:101) at com.tepper.flow.action.TechnologyFlowAction.loadLinkers(TechnologyFlowAction.java:126) at com.tepper.flow.action.TechnologyFlowAction.judgeRequest(TechnologyFlowAction.java:86) at com.tepper.flow.action.TechnologyFlowAction.execute(TechnologyFlowAction.java:59) at org.apache.struts.action.RequestProcessor.processActionPerform(RequestProcessor.java:419) at org.apache.struts.action.RequestProcessor.process(RequestProcessor.java:224) at org.apache.struts.action.ActionServlet.process(ActionServlet.java:1196) at org.apache.struts.action.ActionServlet.doPost(ActionServlet.java:432) at javax.servlet.http.HttpServlet.service(HttpServlet.java:637) at javax.servlet.http.HttpServlet.service(HttpServlet.java:717) at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290) at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206) at org.springframework.orm.hibernate3.support.OpenSessionInViewFilter.doFilterInternal(OpenSessionInViewFilter.java:198) at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:76) at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235) at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206) at org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:88) at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:76) at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235) at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206) at com.tepper.common.SessionFilter.doFilter(SessionFilter.java:76) at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235) at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206) at com.tepper.common.ResFilter.doFilter(ResFilter.java:49) at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235) at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206) at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233) at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191) at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:433) at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:128) at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102) at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109) at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:286) at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:845) at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:583) at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:447) at java.lang.Thread.run(Thread.java:619)
或者后台没异常,前台第一个请求的response返回的是第二个请求的结果。而第二次请求则没有response。
经调试,发现两次请求的request对象是同一对象。然后想起Struts1是非线程安全的,而我在代码中,为了让一个Action接收多个页面的请求,把request对象写成了全局对象。
3、解决
struts1是非线程安全的。两个同一时间的异步请求,会得到同一个request对象,两个请求会造成冲突。
在execution方法中使用局部变量是最简单的保证线程安全的方法了。也就是说在使用struts1的action时,不要在类中定义属性。
把全局的request对象,放回execution方法中,变成局部变量,问题得到解决。
时间: 2024-10-21 09:55:43