try 发表于 2007-5-14 15:29

JSP2+Tomcat6+MSSQL2000中文问题的解决方法

一、数据库设置

不用额外设置,全部默认即可。


二、服务器设置

打开tomcat目录下的conf/server.xml文件,找到这样的一句:
<Connector port="8080" protocol="HTTP/1.1"maxThreads="150" connectionTimeout="20000" redirectPort="8443"/>

修改成这样:
    <Connector port="8080" protocol="HTTP/1.1"
               maxThreads="150" connectionTimeout="20000"
               redirectPort="8443"useBodyEncodingForURI="true" URIEncoding="GBK"/>

同时在配置JNDI连接池时,也指定连接编码。如:

<Context path="/myApp" docBase="c:/jspProject"
    debug="0" reloadable="true" crossContext="true">
    <Resource name="jdbc/myjndi" auth="Container" type="javax.sql.DataSource"
      maxActive="100" maxIdle="30" maxWait="10000"
      username="sa" password="" driverClassName="com.microsoft.jdbc.sqlserver.SQLServerDriver"
      url="jdbc:microsoft:sqlserver://127.0.0.1:1433;DatabaseName=dbTest;useUnicode=true&characterEncoding=GBK"/>
</Context>

三、页面设置

首先应该写一个过滤器。

package mypackage;
import java.io.IOException;
import javax.servlet.*;

public class CharacterEncoding implements Filter {
protected String encoding = null;
protected FilterConfig filterConfig = null;
protected boolean ignore = true;

public void destroy() {
this.encoding = null;
this.filterConfig = null;
}

public void doFilter(ServletRequest request, ServletResponse response,
   FilterChain chain) throws IOException, ServletException {
if (ignore || (request.getCharacterEncoding() == null)) {
   String encoding = selectEncoding(request);
   if (encoding != null)
    request.setCharacterEncoding(encoding);
}
chain.doFilter(request, response);
}

public void init(FilterConfig filterConfig) throws ServletException {
this.filterConfig = filterConfig;
this.encoding = filterConfig.getInitParameter("encoding");
String value = filterConfig.getInitParameter("ignore");
if (value == null)
   this.ignore = true;
else if (value.equalsIgnoreCase("true")
    || value.equalsIgnoreCase("yes"))
   this.ignore = true;
else
   this.ignore = false;
}

protected String selectEncoding(ServletRequest request) {
return (this.encoding);
}
}

同时在应用程序的web.xml中配置:


<filter>
<filter-name>CharacterEncoding</filter-name>
<filter-class>mypackage.CharacterEncoding</filter-class>
<init-param>
   <param-name>encoding</param-name>
   <param-value>GBK</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>CharacterEncoding</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>

这样基本上已经可以了,但做完下面的一些事,会更好。

写一个JSP文件,后缀名可以为"jspf",即 header.jspf ,里面有一句:
<%@ page contentType="text/html; charset=GBK" %>

然后又在web.xml中配置:


<jsp-config>
<jsp-property-group>
   <url-pattern>*.jsp</url-pattern>
   <page-encoding>GBK</page-encoding>
   <include-prelude>
    /WEB-INF/inc/header.jspf
   </include-prelude>
</jsp-property-group>
</jsp-config>

OK,搞定。

测试通过过,不论是post提交的中文字符还是get提交的都可以正确接受而不用另外转换。

[ 本帖最后由 try 于 2007-5-14 15:32 编辑 ]

hjack 发表于 2007-5-14 22:25

https://www.gdutbbs.com/viewthread.php?tid=51635&highlight=%D6%D0%CE%C4

很久之前写过一篇,原来那么眼熟,欢迎楼主多参与讨论。

try 发表于 2007-5-14 23:07

奇怪,楼上的帖和我的不同,但都可以解决中文问题!

iptton 发表于 2007-5-14 23:11

PPT的MJhjack 竟认不出..

PS:谁来总结下PHP+Mysql的乱码问题..

try 发表于 2007-5-23 16:27

最后醒悟,UTF-8才是正道。

因为用GBK编码时,如果图片文件名有中文字符,在IE下无法查看,在firefox下却可以。因为IE的默认设置中有这样一项:

http://blog.gdutbbs.com/attachments/2007/05/23/43591_200705231633111.jpg

上述设置全部改成UTF-8,除了

<jsp-config>
<jsp-property-group>
   <url-pattern>*.jsp</url-pattern>
   <page-encoding>GBK</page-encoding>
   <include-prelude>
    /WEB-INF/inc/header.jspf
   </include-prelude>
</jsp-property-group>
</jsp-config>


红色部分,因为那是编译时的编码,如果文件保存格式为UTF-8的话,应该要设置为UTF-8(在Windows下的编辑器默认是GB2312,如eclispe)。

上传文件使用组件org.apache.commons.fileupload时,读取表单文本时用
FileItem 的getString(String encode)方法。即getString("UTF-8")

[ 本帖最后由 try 于 2007-5-23 17:22 编辑 ]
页: [1]
查看完整版本: JSP2+Tomcat6+MSSQL2000中文问题的解决方法