乱码问题(filter类写法)

 

1.如果form用get传值,tomcat 中修改server.xml

因为tomcat默认编码是iso-8859-1,这里把他改成中文编码.

<Connector port="8080" maxThreads="150" minSpareThreads="25" maxSpareThreads="75"

enableLookups="false" redirectPort="8443" acceptCount="100"

connectionTimeout="20000" disableUploadTimeout="true" URIEncoding='GBK' />

 

2.修改JAVA编码环境的字符集

JAVA程序中添加如下一段代码来修改JAVA变成环境所采用的字符集:

response.setContentType(“text/html;charset=GBK”);

它的作用是让浏览器把Unicode字符转换为GBK字符,这样页面的内容和浏览器的显示模式都设成了GBK,就不会乱码了。

 

3MySQL数据库所采用的字符集

MySQL数据库环境默认字符集是latin1,但是可以在安装时修改他的默认编码,如果使用工具创建数据库或者表,一般会有字符集选择项,设置成和前后环节统一的字符标准。

 

4JSP页面编辑时

JSP文件的头部加上<%@ page=contentType=”text/html;charset=GBK” %>

连接数据库时:

con=DriverManager.getConnection(“jdbc:mysql://localhost:3306/sample_db?user=&password=&userUnicode=true&characterEncoding=GBK”);

 

5.getParameter()乱码也可以这样解决:

  String id=new String(request.getParameter("id").getBytes("ISO8859-1"),"UTF-8");

   String name = new String(request.getParameter("name").getBytes("ISO8859-1"),"UTF-8");

 

6.如果form用post传值,添加Fileter类(过滤类):

package com.yj.filter;

import java.io.IOException;
import java.io.PrintWriter;

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 SetCharacterEncodingFilter implements Filter {
 protected FilterConfig filterConfig = null;
 protected String encoding = null;

 public void destroy() {
  

 }

 public void doFilter(ServletRequest request, ServletResponse response,
   FilterChain chain) throws IOException, ServletException {

  response.setContentType("text/html;charset=gb2312");
  PrintWriter out = response.getWriter();
  if ((request.getCharacterEncoding() == null)) {
   String encoding = selectEncoding(request);
   if (encoding != null)
    request.setCharacterEncoding(encoding);
  }
  chain.doFilter(request, response);

 }

 public void init(FilterConfig filterConfig) throws ServletException {
  this.filterConfig = filterConfig;
  this.encoding = filterConfig.getInitParameter("encoding");
 }

 protected String selectEncoding(ServletRequest request) {

  return (this.encoding);

 }
}

 

然后在web.xml文件进行相应的配置:

  <filter>
 <filter-name>Set Character Encoding</filter-name>
 <filter-class>com.yj.filter.SetCharacteEncodingFilter</filter-class>
 <init-param>
 <param-name>encoding</param-name>
 <param-value>GBK</param-value>
 </init-param>
  </filter>
 
  <filter-mapping>
 <filter-name>Set Character Encoding</filter-name>
 <url-pattern>/*</url-pattern>
  </filter-mapping>

代码说明:<filter-class>元素项就是设定SetCharacterEncodingFilter.java类所存放的具体位置。

 

7.如果是ajax通过get方式提交,或者是IE乱码,FireFox正常,那可以这么做

AJAX.get("comment",url + "&username=" + encodeURIComponent(username4) + "&content=" + encodeURIComponent(content4) + yzimgstr ,0);

意思就是把要传的参数用javascript自带的encodeURIComponent方法将参数进行编码

在插入数据库时再一次解码编码

content=urldecode(content);//解码
content=iconv("UTF-8","GB2312",content); //编码转换,注意:解码必须在编码转换前面,不然得不到正确值

你可能感兴趣的:(tomcat,mysql,Ajax,servlet,IE)