问题描述
我在代码中继承了类HttpServletRequestWrapper,代码如下:public class MyRequestWrapper extends HttpServletRequestWrapper {HttpSession iSession = null;public MyRequestWrapper(HttpServletRequest request) {super(request);} public HttpSession getSession() {if (null != iSession) {return iSession;}return super.getSession();}public HttpSession getSession(boolean create) {if (null != iSession) {return iSession;}return super.getSession(create);}public void setSession(HttpSession iSession) {this.iSession = iSession;}}而我在struts Action接受到页面提交过来的信息时,初始化了该类(MyRequestWrapper),Action代码如下:public ActionForward login(ActionMapping mapping, ActionForm form,HttpServletRequest request, HttpServletResponse response) { ......... MyRequestWrapper wrapperRequest = (MyRequestWrapper) request; .........}可是出错了:java.lang.ClassCastException: org.apache.catalina.connector.RequestFacadeat com.redMud.web.action.LoginAction.login(LoginAction.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:585)at org.apache.struts.actions.DispatchAction.dispatchMethod(DispatchAction.java:274)at org.apache.struts.actions.DispatchAction.execute(DispatchAction.java:194)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:1194)at org.apache.struts.action.ActionServlet.doPost(ActionServlet.java:432)at javax.servlet.http.HttpServlet.service(HttpServlet.java:709)at javax.servlet.http.HttpServlet.service(HttpServlet.java:802)at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:252)at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:173)at com.shareinfo.common.util.EncodingFilter.doFilter(EncodingFilter.java:37)at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:202)at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:173)at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:213)at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:178)at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:126)at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:105)at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:107)at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:148)at org.apache.coyote.http11.Http11AprProcessor.process(Http11AprProcessor.java:833)at org.apache.coyote.http11.Http11AprProtocol$Http11ConnectionHandler.process(Http11AprProtocol.java:639)at org.apache.tomcat.util.net.AprEndpoint$Worker.run(AprEndpoint.java:1285)at java.lang.Thread.run(Thread.java:595)这个问题有哪位朋友遇到过的吗?帮忙解答一下,谢。。。PS:能说说HttpServletRequestWrapper 具体的用法吗?
解决方案
应该这样写:public ActionForward login(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) { ......... MyRequestWrapper wrapperRequest = new MyRequestWrapper(request); ......... }