|
一、数据库设置
不用额外设置,全部默认即可。
二、服务器设置
打开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 编辑 ] |
评分
-
2
查看全部评分
-
|