再谈编码问题
但是,我将我的这么系统完全迁移到LINUX的时候数据显示没有出现乱码,然而提交的数据全部变成了????.我的数据库编码为ZHS16GBK,所以不存在编码的问题.看了一篇谈得比较细的文章,就是WEBLOGIC处理这个问题的.全文如下:
--------------------------------------------------
Weblogic Server中如何解决中文显示乱码问题
由于操作系统、浏览器、数据库、JVM采用的字符集都不一样,基于Weblogic Server开发的应用经常出现中文显示乱码问题,其实在 Weblogic Server上运行的WEB应用有很多与字符集有关的设置,下面做一个总结,为了正确处理中文,最好把这些设置都设上。
1. 在JSP文件头加入
<%@ page contentType=text/html; charset=GBK %>;
指定该JSP采用的字符集。
2.在Weblogic.xml文件的中加入:
引用:encoding
GBK
指定JSP文件中采用的字符集,在JSP文件中的<%@ page contentType=text/html; charset=GBK %>;会覆盖该设置
3.在Weblogic.xml文件的中加入
compilerSupportsEncoding
true
如果为TRUE,指定在编译JSP文件时,采用在JSP文件中定义的
<%@ page contentType=text/html; charset=GBK %>;或中定义的encoding参数中定义的字符集进行编码,如果为FALSE,则采用JVM中默认指定的字符集进行编码。
4. Weblogic Server需要把HTTP request(GET 和POST)中的数据从它的原始编码转化为Unicode,以便Java servlet API进行处理,为了做这种转换,Weblogic Server需要知道HPPT request中的数据的编码方式。这可以通过在Weblogic.xml的中设置.
〈INPUT-charset>;
/
GBK
5.从ORACLE数据库中检索出来的中文显示不正确时,在这种情况下,如果数据库使用的是中文字符集,并使用的是Type 2 JDBC Driver时,可加入Weblogic.codeset=GBK的属性来解决这个问题。代码如下:
java.util.Properties props = new java.util.Properties();
props.put(Weblogic.codeset, GBK);
props.put(user, scott);
props.put(password, tiger);
String connectUrl = jdbc:Weblogic:oracle;
Driver myDriver = (Driver)
Class.forName(Weblogic.jdbc.oci.Driver).newInstance();
Connection conn =
myDriver.connect(connectUrl, props);
---------------------------------------------------------------
表述的以上文字,形式可能反应如下:
-------------------------------------------------------------
<jsp-descriptor>
<jsp-param>
<param-name>compileCommand</param-name>
<param-value>javac</param-value>
</jsp-param>
<jsp-param>
<param-name>encoding</param-name>
<param-value>GBK</param-value>
</jsp-param>
<jsp-param>
<param-name>compilerSupportsEncoding</param-name>
<param-value>true</param-value>
</jsp-param>
</jsp-descriptor>
----------------------------------------------------------
然而我的问题依然没有解决,提交数据依然是乱码;最后再网上发现了一篇解决的文章,结果还真的有效.全文如下:
----------------------------------------------------------
web.xml
<!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" " http://java.sun.com/dtd/web-app_2_3.dtd ">
<web-app>
<filter>
<filter-name>sitemesh</filter-name>
<filter-class>com.opensymphony.module.sitemesh.filter.PageFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>sitemesh</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<filter>
<filter-name>encodingFilter</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</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>*.jsp</url-pattern>
</filter-mapping>
</web-app>
<sitemesh>
<property name="decorators-file" value="/WEB-INF/decorators.xml"/>
<excludes file="${decorators-file}"/>
<page-parsers>
<parser default="true" class="com.opensymphony.module.sitemesh.parser.HTMLPageParser"/>
<parser content-type="text/html" class=" com.opensymphony.module.sitemesh.parser.HTMLPageParser"/>
<parser content-type="text/html;charset=UTF-8" class="com.opensymphony.module.sitemesh.parser.HTMLPageParser"/>
</page-parsers>
<decorator-mappers>
<mapper class="com.opensymphony.module.sitemesh.mapper.ConfigDecoratorMapper">
<param name="config" value="${decorators-file}"/>
</mapper>
</decorator-mappers>
</sitemesh>
<decorators defaultdir="/decorators">
<decorator name="main" page="test.jsp">
<pattern>/*</pattern>
</decorator>
<decorator name="panel" page="panel.jsp"/>
<decorator name="printable" page="printable.jsp"/>
</decorators>
<%@ taglib uri=" http://www.opensymphony.com/sitemesh/decorator" prefix="decorator" %>
<%@ taglib uri=" http://www.opensymphony.com/sitemesh/page" prefix="page" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" " http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml ">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title><decorator:title default="Mysterious page
<decorator:head />
</head>
<body>
<table width="100%" height="100%" border="0">
<tr>
<td width="29%"></td>
<td width="71%"> 哈哈 哈哈
</td>
</tr>
<tr>
<td><decorator:body /></td>
<td> </td>
</tr>
</table>
</body>
</html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html;charset=utf-8">
<title>哈哈</title>
</head>
<body>
<h2>哈哈</h2>
</body>
</html>
发现有中文乱码问题。
在google上查找很久都没有方法解决,后怀疑是处理编码的org.springframework.web.filter.CharacterEncodingFilter中的代码中只对request做了编码处理而没有对response做编码处理
自己对其做了一点小的修改红色字部分
HttpServletRequest request, HttpServletResponse response, FilterChain filterChain)
throws ServletException, IOException {
super.doFilter(request,response,filterChain);
if (this.forceEncoding || request.getCharacterEncoding() == null) {
request.setCharacterEncoding(this.encoding);
response.setContentType("text/html; charset="+this.encoding);
}
filterChain.doFilter(request, response);
}

浙公网安备 33010602011771号