从jsp表单里得到的中文是乱码
我在jsp里设置了
<META http-equiv=content-type content= "text/html; charset=utf-8 ">
在java程序后台取数据时也设置了
request.setCharacterEncoding( "utf-8 ");
可是在调试时发现,java中取到的中文数据就是乱码。
请高手指点.
------解决方案--------------------容器 jsp页面 数据库 都用统一的编码
------解决方案--------------------加一个 Filter
web.xml中配置
<filter>
<filter-name> Set Character Encoding </filter-name>
<filter-class>
com.smyl.init.CharacterEncodingFilter
</filter-class>
<init-param>
<param-name> encoding </param-name>
<param-value> UTF-8 </param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name> Set Character Encoding </filter-name>
<url-pattern> /* </url-pattern>
</filter-mapping>
/////////////////////////////
package com.smyl.init;
import
java.io.IOException;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import
javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
public class CharacterEncodingFilter implements Filter {
/**
*
*/
private static final long serialVersionUID = 1L;
protected String encoding;
protected FilterConfig filterConfig;
protected boolean ignore;
public void doFilter(ServletRequest request, ServletResponse response,
FilterChain chain) throws
IOException,
ServletException {
if (ignore || (request.getCharacterEncoding() == null)) {
String e = selectEncoding(request);
if (e != null) {
request.setCharacterEncoding(e);
}
}
chain.doFilter(request, response);
}
public void init(FilterConfig filterConfig) throws ServletException {
this.filterConfig = filterConfig;
this.encoding = filterConfig.getInitParameter( "encoding ");
String value = filterConfig.getInitParameter( "ignore ");
if (value == null) {
this.ignore = true;
} else if (value.equalsIgnoreCase( "true ")) {
this.ignore = true;
} else if (value.equalsIgnoreCase( "yes ")) {
this.ignore = true;
} else {
this.ignore = false;
}
}
protected String selectEncoding(ServletRequest request) {
return encoding;
}
/**
* Constructor of the object.
*/
public CharacterEncodingFilter() {
encoding = null;
filterConfig = null;
ignore = true;
}
/**
* Destruction of the servlet. <br>
*/
public void destroy() {
this.encoding = null;
this.filterConfig = null;
}
}
------解决方案--------------------