首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 软件管理 > 软件架构设计 >

完美解决中文乱码的有关问题

2012-11-15 
完美解决中文乱码的问题1 借助springweb.xml中增加package com.filterimport java.io.IOExceptionimport

完美解决中文乱码的问题

1 借助spring

web.xml中增加

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;/*** * 创建人:金鑫* 创建时间:2008-9-21 下午03:27:03* 类作用:中文转码过滤器**/@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;   }}}

?

OK,完美解决掉啦。

其实解决乱码很简单的,就是通过过滤器来实现的,呵呵很简单吧?

热点排行