netshuai

导航

解决java web项目中文汉字乱码

1、页面无法显示中文

    将jsp文件开始处设为<%@ page language="java" pageEncoding="UTF-8"%>

2、页面传输乱码

    添加过滤器

      新建一个类,包名设为com.netshuai,类名为CharacterEncodingFilter

      CharacterEncodingFilter.java代码如下:

 

package com.netshuai;

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 {

    
public void destroy() {
        

    }

    
public void doFilter(ServletRequest request, ServletResponse response,
            FilterChain chain) 
throws IOException, ServletException {
        request.setCharacterEncoding(
"utf-8");
        chain.doFilter(request,response);

    }

    
public void init(FilterConfig arg0) throws ServletException {
    

    }

}

 

修改web.xml,在<servlet>前添加如下代码

<filter>
<filter-name>characterEncoding</filter-name>
<filter-class>com.netshuai.CharacterEncodingFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>characterEncoding</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>

3、国际化乱码

请安装国际化插件resourcebundle

posted on 2009-07-13 23:28  netshuai  阅读(1713)  评论(0编辑  收藏  举报