Filter处理中文乱码 web.xml出错

J2EE 码拜 9年前 (2015-11-15) 1201次浏览
1、Webcontent的body中创建一个链接跳转到ServletDemo1;
2、ServletDemo1负责验证结果response.getWriter().write(“中国”);
3、CharacterEncodingFilter类中用Filter过滤器实现解决中午乱码问题
4、<filter>
    <filter-name>CharacterEncodingFilter</filter-name>
    <filter-class>cn.itcast.web.Filter.CharacterEncodingFilter</filter-class>
    <init-param>
      <param-name>charset</param-name>
      <param-value>UTF-8</param-value>
    </init-param>
  </filter>
  <filter-mapping>
    <servlet-name>CharacterEncodingFilter</servlet-name>
运行时这行出错,出错的原因求帮忙分析下
    <url-pattern>/*</url-pattern>
  </filter-mapping>
  <servlet>
    <description></description>
    <display-name>ServletDemo1</display-name>
    <servlet-name>ServletDemo1</servlet-name>
    <servlet-class>cn.itcast.web.servlet.ServletDemo1</servlet-class>
  </servlet>
  <servlet-mapping>
    <servlet-name>ServletDemo1</servlet-name>
    <url-pattern>/ServletDemo1</url-pattern>
  </servlet-mapping>
5、提示出错:
Description Resource Path Location Type
cvc-complex-type.2.4.a: Invalid content was found starting with element “”servlet-name””.
 One of “”{“http://java.sun.com/xml/ns/javaee”:filter-name}”” is expected.
解决方案:50分
你把那个charset 改成encoding 试试

<filter>
        <filter-name>encodingFilter</filter-name>
        <filter-class>
            org.springframework.web.filter.CharacterEncodingFilter
        </filter-class>
        <init-param>
            <param-name>encoding</param-name>
            <param-value>UTF-8</param-value>
        </init-param>
        <init-param>
            <param-name>forceEncoding</param-name>
            <param-value>true</param-value>
        </init-param>
    </filter>
    <filter-mapping>
        <filter-name>encodingFilter</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>

CodeBye 版权所有丨如未注明 , 均为原创丨本网站采用BY-NC-SA协议进行授权 , 转载请注明Filter处理中文乱码 web.xml出错
喜欢 (0)
[1034331897@qq.com]
分享 (0)