JSP项目中的乱码全套解决方式
JSP的中文乱码问题一直是我的问题,曾经有段时间解决过,但是输入中文的生僻字的时候显示依然不正常。前几天又把生僻字的问题解决了,我想中文乱码的问题应该暂时告一段落了。中文乱码问题的核心是因为编码的问题,Tomcat中的编码是ISO-8859-1,而我们中文的编码一般是GBK跟GB2312,Linux的编码是UTF-8,XML的编码是UTF-8,网络中传输的编码是UTF-8,那我们的项目编码应该是什么?毫无疑问UTF-8才是正确的选择。UTF-8的编码方式要从开始建项目的时候就确立,因此建立的项目编码格式首先就是UTF-8的。然后页面的编码方式自然也要是UTF-8的,但是页面中要加入以下的2行代码:
<%@ page contentType="text/html; charset=UTF-8" %>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
在上面打中文等是不受影响的。最后就是要加个过滤器了,过滤所有的网页,所有的网页经过过滤器的时候自动转成了UTF-8的编码。下面是过滤器的代码:
<div style="padding: 4px 5.4pt; width: 95%;">http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gifpackage XX.XX;
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gifimport java.io.*;
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gifimport java.util.*;
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gifimport javax.servlet.*;
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gifimport javax.servlet.http.*;
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gifpublic class filter
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gifhttp://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif extends HttpServlet implements Filter ...{
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif // 编码的字符串
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif protected String encoding = null;
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif // 过滤器的配置
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif protected FilterConfig filterConfig = null;
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif // 是否忽略客户端的编码
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif protected boolean ignore = true;
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif // 销毁过滤器
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gifhttp://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif public void destroy() ...{
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif this.encoding = null;
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif this.filterConfig = null;
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif }
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif // 过滤方法
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif public void doFilter(ServletRequest request, ServletResponse response,
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gifhttp://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif FilterChain chain) throws IOException, ServletException ...{
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif // 如果使用过滤器,忽略客户端的编码,那么使用通过过滤器设定编码
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gifhttp://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif if (ignore || (request.getCharacterEncoding() == null)) ...{
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif String encoding = selectEncoding(request);
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gifhttp://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif if (encoding != null)...{
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif request.setCharacterEncoding(encoding);
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gifhttp://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif }else...{
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif request.setCharacterEncoding("UTF-8");
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif }
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif }
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif // 传送给下一个过滤器
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif chain.doFilter(request, response);
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif }
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif // 初始化过滤器
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gifhttp://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif public void init(FilterConfig filterConfig) throws ServletException ...{
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif this.filterConfig = filterConfig;
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif this.encoding = filterConfig.getInitParameter("encoding");
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif String value = filterConfig.getInitParameter("ignore");
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif if (value == null)
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif this.ignore = true;
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif else if (value.equalsIgnoreCase("true"))
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif this.ignore = true;
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif else if (value.equalsIgnoreCase("yes"))
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif this.ignore = true;
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif else
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif this.ignore = false;
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif }
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif // 返回过滤器设定的编码
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gifhttp://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif protected String selectEncoding(ServletRequest request) ...{
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif return (this.encoding);
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif }
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif}
页:
[1]