chianxxp
chianxxp
2009-08-25 19:26
浏览 316
已采纳

过滤器引起的classcastException

最近作一个系统,其中有这么一个业务。就是针对所有数据插入,都要进行编码过滤,保证不会出现乱码。
大概的一个流程:
1 在web.xml中配置url-pattern.值为:*.do
例如:
1 前台请求一个/examsystem/stuAction.do?method=login(这个涉及到struts-config.xml,暂不帖出)
2 过滤器先进行过滤。
3 再到相应的action进行处理。
相关的文件:web.xml
[code="java"]
<?xml version="1.0" encoding="UTF-8"?>
xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
examsystem

<filter>
    <description>charact
    </description>
    <display-name>CharacterFilter</display-name>
    <filter-name>CharacterFilter</filter-name>
    <filter-class>common.controlUtil.j2ee.CharacterFilter</filter-class>
    <init-param>
        <param-name>encoding</param-name>
        <param-value>UTF-8</param-value>
    </init-param>
</filter>
<filter>
    <description>
    </description>
    <display-name>
    LoginValidatorFilter</display-name>
    <filter-name>LoginValidatorFilter</filter-name>
    <filter-class>common.controlUtil.j2ee.LoginValidatorFilter</filter-class>
</filter>
<filter-mapping>
    <filter-name>CharacterFilter</filter-name>
    <url-pattern>*.do</url-pattern>
</filter-mapping>
<filter-mapping>
    <filter-name>LoginValidatorFilter</filter-name>
    <url-pattern>/teacher/*</url-pattern>
</filter-mapping>
<servlet>
    <servlet-name>action</servlet-name>
    <servlet-class>org.apache.struts.action.ActionServlet</servlet-class>
    <init-param>
        <param-name>config</param-name>
        <param-value>/WEB-INF/struts-config.xml</param-value>
    </init-param>
    <load-on-startup>2</load-on-startup>
</servlet>
<servlet>
    <description>
    </description>
    <display-name>
    PageServlet</display-name>
    <servlet-name>PageServlet</servlet-name>
    <servlet-class>
    common.viewUtil.pageUtil.PageServlet</servlet-class>
</servlet>
<servlet>
    <description>
    </description>
    <display-name>
    ValidateCode</display-name>
    <servlet-name>ValidateCode</servlet-name>
    <servlet-class>
    common.controlUtil.j2ee.ValidateCode</servlet-class>
    <init-param>
        <description>
        </description>
        <param-name>width</param-name>
        <param-value>60</param-value>
    </init-param>
    <init-param>
        <description>
        </description>
        <param-name>height</param-name>
        <param-value>40</param-value>
    </init-param>
    <init-param>
        <description>
        </description>
        <param-name>codeCount</param-name>
        <param-value>4</param-value>
    </init-param>
</servlet>
<servlet-mapping>
    <servlet-name>action</servlet-name>
    <url-pattern>*.do</url-pattern>
</servlet-mapping>
<servlet-mapping>
    <servlet-name>PageServlet</servlet-name>
    <url-pattern>*.page</url-pattern>
</servlet-mapping>
<servlet-mapping>
    <servlet-name>ValidateCode</servlet-name>
    <url-pattern>*.validate</url-pattern>
</servlet-mapping>
<welcome-file-list>
    <welcome-file>index.html</welcome-file>
    <welcome-file>index.htm</welcome-file>
    <welcome-file>index.jsp</welcome-file>
    <welcome-file>default.html</welcome-file>
    <welcome-file>default.htm</welcome-file>
    <welcome-file>default.jsp</welcome-file>
</welcome-file-list>
<resource-ref>
    <description>jr</description>
    <res-ref-name>jdbc/mySQLServer</res-ref-name>
    <res-type>javax.sql.DataSource</res-type>
    <res-auth>Container</res-auth>
</resource-ref>

[/code]
CharacterFilter.java
[code="java"]public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
// TODO Auto-generated method stub
// place your code here
HttpServletRequest req = (HttpServletRequest)request;
HttpServletResponse rep = (HttpServletResponse)response;

    req.setCharacterEncoding(encoding);     
    rep.setCharacterEncoding(encoding);
    // pass the request along the filter chain      
    chain.doFilter(req, rep);
}

/**
 * @see Filter#init(FilterConfig)
 */
public void init(FilterConfig fConfig) throws ServletException {
    // TODO Auto-generated method stub
    String encoding = fConfig.getInitParameter("encoding");
    if(encoding == null)
        this.encoding = "UTF-8";
    else
        this.encoding = encoding;
}

[/code]
StudentAction.java
[code="java"]public ActionForward login(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws IOException, ServletException {
StudentForm studentForm = (StudentForm) form;
StudentBean bean = new StudentBean();
try {
BeanUtils.copyProperties(bean, studentForm);
} catch (IllegalAccessException e) {
LogProcessor.processException(e, Constant.studentSearch_error,
LogProcessor.ERROR);
} catch (InvocationTargetException e) {
LogProcessor.processException(e, Constant.studentSearch_error,
LogProcessor.ERROR);
}
String result = business.login(bean);
if(result.equals(Constant.searchSuccess)){
HttpSession session = request.getSession();
session.setAttribute(Constant.student,bean);
}
return mapping.findForward(result);
}[/code]

学生输入用户名和密码,提交。显示正确的结果页面。如果在提交一次,就是所谓的重复的提交。
就会报classcastException的错误,错误的类转换为:examsystem.bean.StudentBean.
错误提示:
[code="java"]java.lang.ClassCastException: examsystem.bean.StudentBean
org.apache.struts.chain.commands.CreateActionForm.execute(CreateActionForm.java:88)
org.apache.struts.chain.commands.ActionCommandBase.execute(ActionCommandBase.java:51)
org.apache.commons.chain.impl.ChainBase.execute(ChainBase.java:190)
org.apache.commons.chain.generic.LookupCommand.execute(LookupCommand.java:304)
org.apache.commons.chain.impl.ChainBase.execute(ChainBase.java:190)
org.apache.struts.chain.ComposableRequestProcessor.process(ComposableRequestProcessor.java:283)
org.apache.struts.action.ActionServlet.process(ActionServlet.java:1913)
org.apache.struts.action.ActionServlet.doPost(ActionServlet.java:462)
javax.servlet.http.HttpServlet.service(HttpServlet.java:709)
javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
common.controlUtil.j2ee.CharacterFilter.doFilter(CharacterFilter.java:55)

[/code]
奇了怪了,真得没有什么头绪,也想不出什么原因。因为在CharacterFilter.java:55就是:chain.doFilter(req,rep)而已。
这不是我写的。
服务器是:tomcat 5

各位认为:大概会有哪些方面的原因呢?
[b]问题补充:[/b]
最近作一个系统,其中有这么一个业务。就是针对所有数据插入,都要进行编码过滤,保证不会出现乱码。
大概的一个流程:
1 在web.xml中配置url-pattern.值为:*.do
例如:
1 前台请求一个/examsystem/stuAction.do?method=login(这个涉及到struts-config.xml,暂不帖出)
2 过滤器先进行过滤。
3 再到相应的action进行处理。
相关的文件:web.xml
[code="java"]
<?xml version="1.0" encoding="UTF-8"?>
xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
examsystem

<filter>
    <description>charact
    </description>
    <display-name>CharacterFilter</display-name>
    <filter-name>CharacterFilter</filter-name>
    <filter-class>common.controlUtil.j2ee.CharacterFilter</filter-class>
    <init-param>
        <param-name>encoding</param-name>
        <param-value>UTF-8</param-value>
    </init-param>
</filter>
<filter>
    <description>
    </description>
    <display-name>
    LoginValidatorFilter</display-name>
    <filter-name>LoginValidatorFilter</filter-name>
    <filter-class>common.controlUtil.j2ee.LoginValidatorFilter</filter-class>
</filter>
<filter-mapping>
    <filter-name>CharacterFilter</filter-name>
    <url-pattern>*.do</url-pattern>
</filter-mapping>
<filter-mapping>
    <filter-name>LoginValidatorFilter</filter-name>
    <url-pattern>/teacher/*</url-pattern>
</filter-mapping>
<servlet>
    <servlet-name>action</servlet-name>
    <servlet-class>org.apache.struts.action.ActionServlet</servlet-class>
    <init-param>
        <param-name>config</param-name>
        <param-value>/WEB-INF/struts-config.xml</param-value>
    </init-param>
    <load-on-startup>2</load-on-startup>
</servlet>
<servlet>
    <description>
    </description>
    <display-name>
    PageServlet</display-name>
    <servlet-name>PageServlet</servlet-name>
    <servlet-class>
    common.viewUtil.pageUtil.PageServlet</servlet-class>
</servlet>
<servlet>
    <description>
    </description>
    <display-name>
    ValidateCode</display-name>
    <servlet-name>ValidateCode</servlet-name>
    <servlet-class>
    common.controlUtil.j2ee.ValidateCode</servlet-class>
    <init-param>
        <description>
        </description>
        <param-name>width</param-name>
        <param-value>60</param-value>
    </init-param>
    <init-param>
        <description>
        </description>
        <param-name>height</param-name>
        <param-value>40</param-value>
    </init-param>
    <init-param>
        <description>
        </description>
        <param-name>codeCount</param-name>
        <param-value>4</param-value>
    </init-param>
</servlet>
<servlet-mapping>
    <servlet-name>action</servlet-name>
    <url-pattern>*.do</url-pattern>
</servlet-mapping>
<servlet-mapping>
    <servlet-name>PageServlet</servlet-name>
    <url-pattern>*.page</url-pattern>
</servlet-mapping>
<servlet-mapping>
    <servlet-name>ValidateCode</servlet-name>
    <url-pattern>*.validate</url-pattern>
</servlet-mapping>
<welcome-file-list>
    <welcome-file>index.html</welcome-file>
    <welcome-file>index.htm</welcome-file>
    <welcome-file>index.jsp</welcome-file>
    <welcome-file>default.html</welcome-file>
    <welcome-file>default.htm</welcome-file>
    <welcome-file>default.jsp</welcome-file>
</welcome-file-list>
<resource-ref>
    <description>jr</description>
    <res-ref-name>jdbc/mySQLServer</res-ref-name>
    <res-type>javax.sql.DataSource</res-type>
    <res-auth>Container</res-auth>
</resource-ref>

[/code]
CharacterFilter.java
[code="java"]public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
// TODO Auto-generated method stub
// place your code here
HttpServletRequest req = (HttpServletRequest)request;
HttpServletResponse rep = (HttpServletResponse)response;

    req.setCharacterEncoding(encoding);     
    rep.setCharacterEncoding(encoding);
    // pass the request along the filter chain      
    chain.doFilter(req, rep);
}

/**
 * @see Filter#init(FilterConfig)
 */
public void init(FilterConfig fConfig) throws ServletException {
    // TODO Auto-generated method stub
    String encoding = fConfig.getInitParameter("encoding");
    if(encoding == null)
        this.encoding = "UTF-8";
    else
        this.encoding = encoding;
}

[/code]
StudentAction.java
[code="java"]public ActionForward login(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws IOException, ServletException {
StudentForm studentForm = (StudentForm) form;
StudentBean bean = new StudentBean();
try {
BeanUtils.copyProperties(bean, studentForm);
} catch (IllegalAccessException e) {
LogProcessor.processException(e, Constant.studentSearch_error,
LogProcessor.ERROR);
} catch (InvocationTargetException e) {
LogProcessor.processException(e, Constant.studentSearch_error,
LogProcessor.ERROR);
}
String result = business.login(bean);
if(result.equals(Constant.searchSuccess)){
HttpSession session = request.getSession();
session.setAttribute(Constant.student,bean);
}
return mapping.findForward(result);
}[/code]

学生输入用户名和密码,提交。显示正确的结果页面。如果在提交一次,就是所谓的重复的提交。
就会报classcastException的错误,错误的类转换为:examsystem.bean.StudentBean.
错误提示:
[code="java"]java.lang.ClassCastException: examsystem.bean.StudentBean
org.apache.struts.chain.commands.CreateActionForm.execute(CreateActionForm.java:88)
org.apache.struts.chain.commands.ActionCommandBase.execute(ActionCommandBase.java:51)
org.apache.commons.chain.impl.ChainBase.execute(ChainBase.java:190)
org.apache.commons.chain.generic.LookupCommand.execute(LookupCommand.java:304)
org.apache.commons.chain.impl.ChainBase.execute(ChainBase.java:190)
org.apache.struts.chain.ComposableRequestProcessor.process(ComposableRequestProcessor.java:283)
org.apache.struts.action.ActionServlet.process(ActionServlet.java:1913)
org.apache.struts.action.ActionServlet.doPost(ActionServlet.java:462)
javax.servlet.http.HttpServlet.service(HttpServlet.java:709)
javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
common.controlUtil.j2ee.CharacterFilter.doFilter(CharacterFilter.java:55)

[/code]
奇了怪了,真得没有什么头绪,也想不出什么原因。因为在CharacterFilter.java:55就是:chain.doFilter(req,rep)而已。
这不是我写的。
服务器是:tomcat 5

各位认为:大概会有哪些方面的原因呢?

补充:
在studentAction中,不将学生的信息保存在session中,保存在request中是没有问题的。就是保存在session会报错.
[b]问题补充:[/b]
我也不确定是session.setAttribute(Constant.student,bean)这句,但确实用request.setAttribute(Constant.student,bean)却没有问题。

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

4条回答 默认 最新

  • wanghaolovezlq
    wanghaolovezlq 2009-08-25 21:03
    已采纳

    你自己多测试几次,先定位出问题所在,到底哪行代码搞出问题了

    点赞 评论
  • wanghaolovezlq
    wanghaolovezlq 2009-08-25 19:40

    你把这部分代码去掉
    public ActionForward login(ActionMapping mapping, ActionForm form,
    HttpServletRequest request, HttpServletResponse response)
    throws IOException, ServletException {
    //就只打印一条语句看,能否正确执行
    return mapping.findForward(result);
    }

    点赞 评论
  • wanghaolovezlq
    wanghaolovezlq 2009-08-25 19:48

    你的意思是这行代码导致的问题?
    session.setAttribute(Constant.student,bean);

    点赞 评论
  • james8273
    james8273 2009-08-25 21:24

    你那过滤器写的什么玩意啊,那么多费代码,加我QQ,我告诉你什么问题,不是过滤器报错,是你的studentForm里面的值拷贝到studentBean里面有错误,你看下是不是BeanUtils.copyProperties(bean, studentForm); 方法错了应该是BeanUtils.copyProperties(studentForm, bean);这样写吧???我忘记了,1年前用过

    点赞 评论

相关推荐