如何在开发和编译代码时指定字符集为UTF-8?如何设定数据库连接方式是UTF-8?
解决方案 »
- css,js的问题,求大神们帮忙,急急急!
- jsp中按钮调用java类的方法
- jdk1.4 和1.5的问题 急急
- 100分!问2个struts的问题!
- JSP自定义标签问题,急!!!!!!
- jstl如何调用存在session中的一个javabean?
- 请教:JSP如何生成xml文件
- 往文件中写汉字的为乱码,如何解决?
- 兄弟们。我在JAVA区竟然得不到答案。只能期望在这里找到帮助了(共200分的问题)
- jqueryui问题,有关它的tab方法问题
- 求一个时间查询语句!!!!!!!急
- 我用<bean:write name="aa" property="name" />列出三个记录在一行,怎么把他们之间的空格缩小一些呢?
import javax.servlet.Filter;
import javax.servlet.FilterConfig;
import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;/**
* ignore If set to "true", any character encoding
* specified by the client is ignored, and the value returned by the
* <code>selectEncoding()</code> method is set. If set to "false,
* <code>selectEncoding()</code> is called <strong>only</strong> if the
* client has not already specified an encoding. By default, this
* parameter is set to "true".
*/
public class SetCharacterEncodingFilter
implements Filter {
protected String encoding;
protected FilterConfig fConfig;
protected boolean ignore;
{
this.encoding = "";
this.fConfig = null;
this.ignore = true;
} public SetCharacterEncodingFilter() {
} /**
* Place this filter into service
*
* @param fConfig The filter configuration object
* @throws ServletException
*/
public void init(FilterConfig fConfig) throws ServletException {
this.fConfig = fConfig;
this.encoding = fConfig.getInitParameter("encoding");
String ignoreValue = fConfig.getInitParameter("ignore");
if (ignoreValue == null) {
this.ignore = true;
}
else if (ignoreValue.equalsIgnoreCase("true")) {
this.ignore = true;
}
else if (ignoreValue.equalsIgnoreCase("yes")) {
this.ignore = true;
}
else {
this.ignore = false;
}
} /**
* Select and set (if specified) the character encoding to be used to
* interpret request parameters for this request.
*
* @param request The servlet request we are processing
* @param response The servlet response we are creating
* @param chain The filter chain we are processing
* @throws IOException if an input/output error occurs
* @throws ServletException if a servlet error occurs
*/
public void doFilter(ServletRequest request, ServletResponse response,
FilterChain chain) throws ServletException, IOException {
if (this.ignore || (request.getCharacterEncoding() == null)) {
String tempEncoding = this.selectEncoding(request);
if (tempEncoding != null) {
request.setCharacterEncoding(tempEncoding);
}
}
System.out.println(request.getCharacterEncoding());
chain.doFilter(request, response);
fConfig.getServletContext().log( (new ServletException()).getMessage());
fConfig.getServletContext().log( (new IOException()).getMessage());
return;
} public void destroy() {
encoding = "";
fConfig = null;
} protected String selectEncoding(ServletRequest request) {
return this.encoding;
}}
<filter-name>EncodingFilter</filter-name>
<description>Character Encoding Filter</description>
<filter-class>SetCharacterEncodingFilter</filter-class>
<init-param>
<param-name>ignore</param-name>
<param-value>true</param-value>
</init-param>
<init-param>
<param-name>encoding</param-name>
<param-value>utf-8</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>EncodingFilter</filter-name>
<servlet-name>action</servlet-name>
</filter-mapping>
它能处理的只是经过servlet的request,但是还有很多是不经过servlet的!
比如直接从数据库中读取数据?
mysql> show variables like '%char%';1,修改编码
找到mysql下的my.ini文件中
[mysqld]
default-character-set=utf8
[client]
default-character-set=utf8
2,再查看,发现database的编码可能还是默认的latin1
3,修改数据库编码
ALTER DATABASE dbname CHARACTER SET utf8;
4,再查看,可能只剩一个system的编码不一致,但这没关系
5,这样把mysql数据库编码设置好了,注意这时一定要重新建表,因为表的默认编码还是以前的,可以导出原来表的sql脚本可看到,我原来因为这点痛苦了很久
6,servlet方面的编码就是用网上的fiter方法解决