一、常见的乱码解决
在servlet中
请求乱码:req.setCharacterEncoding("UTF-8");
响应乱码:resp.setContextType("text/html";charaset=utf-8);
二、自定义过滤器解决乱码问题
1)、先在web.xml中配置Filter过滤器
2)、创建一个类来实现Filter接口
重写接口中的方法
在doFilter中写入
req.setCharacterEncoding("UTF-8");
resq.setContextType("text/html";charaset=utf-8);
三、在spring中spring提供了乱码解决
在web.xml中配置过滤器
<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>
</filter>
<filter-mapping>
<filter-name>encodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
四、springmvc中json乱码解决
在springmvc.xml中配置
<mvc:annotation-driven>
<mvc:message-converters register-defaults="true">
<bean class="org.springframework.http.converter.StringHttpMessageConverter">
<constructor-arg value="UTF-8"/>
</bean>
<bean class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">
<property name="objectMapper">
<bean class="org.springframework.http.converter.json.Jackson2ObjectMapperFactoryBean">
<property name="failOnEmptyBeans" value="false"/>
</bean>
</property>
</bean>
</mvc:message-converters>
</mvc:annotation-driven>