解决java/jsp乱码问题

每一个JAVA WEB开发者都会碰到乱码问题。本文阐述了JAVA WEB开发中乱码的完全解决方法。 JAVA中,一个WEB应用从构成部分来看无非分3部分: JSP JAVA程序(业务逻辑) 数据库 要解决乱码问题,也从这3部分入手。 其实,我们的目标很明确,第一、保证显示中文时不为乱码;第二、保证保存到数据库里的数据不为乱码。 怎么样实现上面2个目标呢?让我们从数据的输入/输出的角度来分析。一个典型的用户请求的过程为: 1)浏览器接收用户输入 2)用户输入的数据-〉JAVA程序 3)JAVA程序对数据进行处理,保存到数据库(需要保存时) 4)从数据库取出数据,返回给浏览器 原则上,如果我们能保证在上述阶段中的数据编码都采用同一个编码方式的话,就应该不会产生乱码。怎么样把它们的编码方式统一起来呢?可以通过以下几个步骤实现: 1、用Filter把用户的输入数据统一编码后再传送给JAVA程序 1) Filter可以用Tomcat提供的SetCharacterEncodingFilter.class,也可以使用如下代码自己做一个SetCharacterEncodingFilter:

 

 

java代码

  1. public class SetCharacterEncodingFilter      
  2.     implements Filter      
  3. {      
  4.      
  5.     protected String encoding;      
  6.     protected FilterConfig filterConfig;      
  7.     protected boolean ignore;      
  8.           
  9.     /**    
  10.      * Constructor    
  11.      *     
  12.      */     
  13.     public SetCharacterEncodingFilter()      
  14.     {      
  15.         encoding = null;      
  16.         filterConfig = null;      
  17.         ignore = true;      
  18.     }      
  19.      
  20.     /**    
  21.      * @see javax.servlet.Filter#destroy()    
  22.      */     
  23.     public void destroy()      
  24.     {      
  25.         encoding = null;      
  26.         filterConfig = null;      
  27.     }      
  28.      
  29.     /**    
  30.      * @see javax.servlet.Filter#doFilter(javax.servlet.ServletRequest, javax.servlet.ServletResponse, javax.servlet.FilterChain)    
  31.      */     
  32.     public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)      
  33.         throws IOException, ServletException      
  34.     {      
  35.         if(ignore || request.getCharacterEncoding() == null)      
  36.         {      
  37.             String encoding = selectEncoding(request);      
  38.             if(encoding != null)      
  39.                 request.setCharacterEncoding(encoding);      
  40.         }      
  41.         chain.doFilter(request, response);      
  42.     }      
  43.      
  44.     /**    
  45.      * @see javax.servlet.Filter#init(javax.servlet.FilterConfig)    
  46.      */     
  47.     public void init(FilterConfig filterConfig)      
  48.         throws ServletException      
  49.     {      
  50.         this.filterConfig = filterConfig;      
  51.         encoding = filterConfig.getInitParameter("encoding");      
  52.         String value = filterConfig.getInitParameter("ignore");      
  53.         if(value == null)      
  54.             ignore = true;      
  55.         else     
  56.         if(value.equalsIgnoreCase("true"))      
  57.             ignore = true;      
  58.         else     
  59.         if(value.equalsIgnoreCase("yes"))      
  60.             ignore = true;      
  61.         else     
  62.             ignore = false;      
  63.     }      
  64.      
  65.     /**    
  66.      * @param request    
  67.      * @return    
  68.      */     
  69.     protected String selectEncoding(ServletRequest request)      
  70.     {      
  71.         return encoding;      
  72.     }      
  73. }  

 web.xml里设置:

Xml代码

  1. <filter>      
  2.     <filter-name>SetEncoding</filter-name>      
  3.     <filter-class>包名.SetCharacterEncodingFilter</filter-class>      
  4.     <init-param>      
  5.         <param-name>encoding</param-name>      
  6.         <param-value>UTF-8</param-value>      
  7.     </init-param>      
  8.  </filter>      
  9. ...      
  10.      
  11.  <filter-mapping>      
  12.     <filter-name>SetEncoding</filter-name>      
  13.     <url-pattern>/*</url-pattern>      
  14.  </filter-mapping>  

 

如果定义后编码还有问题,注意filter-mapping在web.xml中的定义顺序。

2、 数据库采用跟HTML一样的编码。
数据库编码设置为utf-8

3、JSP里明确指定编码方式,告诉编译器采用我们指定的编码对JSP加以编译。
JSP文件的开头加上:
<%@ page contentType="text/html; charset=UTF-8" %>
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" %>

4、使用HTML<meta/>标签告诉浏览器使用指定的编码
<meta http-equiv="content-type" content="text/html; charset=UTF-8"> 

5、 统一资源文件(比如消息等定义文件)的编码方式。并在打包之前用转换成ASCII码。用ant工具的情况下,可以执行以下方法加以转换:

<native2ascii encoding="utf-8" src="资源文件所在路径" dest="${classes.dir}"  includes="**/*.properties" />

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值