解决中文乱码的问题

1 借助spring web.xml中增加

<filter>
<filter-name>Set Character Encoding</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>utf8</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>Set Character Encoding</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>

如果不用spring怎么办呢?

<filter>
<filter-name>encodingFilter</filter-name>
<filter-class>com.rda.commons.filter.ChineseFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
<init-param>
<param-name>forceEncoding</param-name>
<param-value>true</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>encodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>

配置文件,然后呢,转码的过滤器就要自己写咯

package com.filter;

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;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
*
* 创建人:ayanline
* 创建时间:2008-9-21
* 类作用:中文转码过滤器
*
*/
@SuppressWarnings("serial")
public class ChineseFilter extends HttpServlet implements Filter
{
private FilterConfig filterConfig;

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

public void doFilter(ServletRequest request, ServletResponse response, FilterChain filterChain)
{
try
{
String encoding = filterConfig.getInitParameter("encoding");
//从WEB.xml配置文件中取出参数,这样我们可以通过配置修改编码格式.
request.setCharacterEncoding(encoding);//设置请求的编码格式

filterChain.doFilter(request, response);
}
catch (ServletException sx)
{
filterConfig.getServletContext().log(sx.getMessage());
}
catch (IOException iox)
{
filterConfig.getServletContext().log(iox.getMessage());
}
}

public void destroy()
{
}

protected void doGet(HttpServletRequest arg0, HttpServletResponse arg1) throws ServletException, IOException
{
super.doGet(arg0, arg1);
}

protected void doPost(HttpServletRequest arg0, HttpServletResponse arg1) throws ServletException, IOException
{
super.doPost(arg0, arg1);
}

/**
* @function 验证数据是否为空,如果为空则转换
* @param param
* @return String
*/
public String checkNull(String param)
{
if (param == null || param.equals(""))
{
return "";
}
else
{
return param;
}
}
}




import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import org.apache.struts2.dispatcher.FilterDispatcher;

public class NewFilterDispatcher extends FilterDispatcher {
private static String encoding = "utf-8";

public void init(FilterConfig filterConfig) throws ServletException {
super.init(filterConfig);
String encodingParam = filterConfig.getInitParameter("encoding");
if (encodingParam != null && encodingParam.trim().length() != 0) {
encoding = encodingParam;
}
}

public void doFilter(ServletRequest request, ServletResponse response,
FilterChain chain) throws IOException, ServletException {
request.setCharacterEncoding(encoding);
super.doFilter(request, response, chain);
}
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值