关于post和get的中文乱码处理
get:
(1)转码:String username=request.getParameter("username"); String un=new String(username.getBytes("iso-8859-1"),"UTF-8"); (2)在tomcat配置文件server.xml 之connector标签配置文件中加URIEncoding="UTF-8",可以改善get提交的中文乱码(方法不太灵活)<Connector port="8080" protocol="HTTP/1.1"
connectionTimeout="20000" redirectPort="8443" useBodyEncodingForURI="true" />或者在同样的地方useBodyEncodingForURI="true"(根据响应页面的编码格式而改变,即对应的是request.setCharacterEncoding("UTF-8");
post
request.setCharacterEncoding("UTF-8");****************************************************************
配置字符过滤器:web.xml配置spring字符集过滤器或者自定义过滤器为UTF-8
<!-- spring字符编码过滤器start-->
<filter> <!--① Spring 编码过滤器 --> <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> <!-- ② 过滤器的匹配 URL --> <filter-mapping> <filter-name>encodingFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <!-- spring字符编码过滤器end-->***********************************************************************************
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">