thinkthink's bolg

enjoy

BlogJava 首页 新随笔 联系 聚合 管理
  0 Posts :: 3 Stories :: 0 Comments :: 0 Trackbacks

一. 乱码问题是web开发过程中基本都要处理的。
二.解决办法:

 1。 统一全部的编码方式,强烈建议采用“UTF-8”;
     
   例如,jsp 里面的配置为
1<%@ page contentType="text/html;charset=UTF-8"  pageEncoding="UTF-8" %>
  2。 对需要在web.xml中配置过滤器(filter)的,也要设置为“UTF-8”;
   例如:
    
package commons.filter;

import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import java.util.*;

public class EncodingFilter extends HttpServlet implements Filter {
    
private FilterConfig filterConfig;
    
private String targetEncoding = "UTF-8";
  //Handle the passed-in FilterConfig
    public void init(FilterConfig filterConfig) throws ServletException {
        
this.filterConfig = filterConfig;
    }


    
//Process the request/response pair
    public void doFilter(ServletRequest request, ServletResponse response,
                         FilterChain filterChain) 
{
        
try {
            request.setCharacterEncoding(targetEncoding);
            filterChain.doFilter(request, response);
        }
 catch (ServletException sx) {
            filterConfig.getServletContext().log(sx.getMessage());
        }
 catch (IOException iox) {
            filterConfig.getServletContext().log(iox.getMessage());
        }

    }


    
//Clean up resources
    public void destroy() {
        filterConfig 
= null;
        targetEncoding 
= null;
    }

}

3. 如果不用“UTF-8”,你会发现在使用AJAX提交数据时,URL中的中文在服务器端是乱码,因为AJAX是以“UTF-8”编码提交数据的。
4.关于国际化属性文件(xxxxx.properties), 因为需要unicode编码,建议使用PropertiesEditor国际化插件进行编辑,他会自动存为unicode编码。
    关于这个PropertiesEditor国际化插件,我下载的时候发现只有for eclipse3.0的, 如果用的eclipse版本是3.2,,3.3根本不行,于是只有下载standone 方式的PropertiesEditor,在cmd下运行: java -jar PropertiesEditor.jar 

5.如果发现数据库数据也有乱码,记得正确设置数据库的编码方式
posted on 2007-08-25 23:55 thinkthink 阅读(307) 评论(0)  编辑  收藏 所属分类: Struts2

只有注册用户登录后才能发表评论。


网站导航: