本站首页    管理页面    写新日志    退出


«February 2026»
1234567
891011121314
15161718192021
22232425262728


公告
暂无公告...

我的分类(专题)

日志更新

最新评论

留言板

链接

Blog信息
blog名称:
日志总数:29
评论数量:19
留言数量:0
访问次数:118517
建立时间:2006年5月29日




[java与中文]乱码问题(五)
软件技术

krf301 发表于 2007/4/16 22:02:33

jsp,struts处理中文乱码问题 一、关于jsp处理中文乱码的问题   在web.xml中设置正确的编码类型,使网页在发送表单时不会出现中文乱码。 /*  * 解决中文乱码问题,该类必须在web.xml设置,指定编码类型  */ package com.login.app;   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;   public class SetCharacterEncodingFilter implements Filter {     protected String encoding = null;       protected FilterConfig filterConfig = null;       protected boolean ignore = true;       /**      * Take this filter out of service.      */     public void destroy() {           this.encoding = null;         this.filterConfig = null;       }       public void doFilter(ServletRequest request, ServletResponse response,                          FilterChain chain)         throws IOException, ServletException {           // Conditionally select and set the character encoding to be used         if (ignore || (request.getCharacterEncoding() == null)) {             String encoding = selectEncoding(request);             if (encoding != null)                 request.setCharacterEncoding(encoding);         }           // Pass control on to the next filter         chain.doFilter(request, response);       }         /**      * Place this filter into service.      *      * @param filterConfig The filter configuration object      */     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"))             this.ignore = true;         else if (value.equalsIgnoreCase("yes"))             this.ignore = true;         else             this.ignore = false;       }       protected String selectEncoding(ServletRequest request) {           return (this.encoding);       } } 参照tomcat自带的filter类,自己写一个filter的子类,用来设置编码。并在web.xml上指定就可以了: <!--filer in Chinese-->   <filter>      <filter-name>Set Character Encoding</filter-name>      <filter-class>com.login.app.SetCharacterEncodingFilter</filter-class>              <init-param>               <param-name>encoding</param-name>               <param-value>GBK</param-value><!—在此处设置相应的编码à              </init-param>  </filter>    <filter-mapping>             <filter-name>Set Character Encoding</filter-name>             <url-pattern>*.jsp</url-pattern>  </filter-mapping> 把以上代码放在web.xml中<display-name>Encoding Application</display-name>的后面. 二、在struts中处理中文乱码问题: 要解决在提交表单时,中文不会出显乱码,只须继承RequestProcessor类,重写processPreprocess这个方法,则可以解决。 package com.login.app; import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse; import org.apache.struts.action.RequestProcessor; public class MyRequestProcessor extends RequestProcessor{ public MyRequestProcessor(){} protected boolean processPreprocess(HttpServletRequest request,   HttpServletResponse response){  try{   request.setCharacterEncoding("UTF-8");//在此设置字符集  }  catch(Exception ex){   System.out.println("字符集设置失败");  }  return true; }} 并在struts-config.xml设置相应的<controller>,如下所示: <controller processorClass="com.login.app.MyRequestProcessor"/>  三、解决插入和读取数据库数据时出现的中文乱码 插入数据到数据库时出现乱码: 这种乱码会使你插入数据库的中文变成乱码,或者读出显示时也是乱码,解决方法如下: 在数据库连接字符串中加入编码字符集 String url=”jdbc:mysql://localhost/test?user=root&password=root&useUnicode=true&characterEncoding=UTF-8" 把characterEncoding的值设置成你期望的编码。 无论你是用哪种方式连接数据库,只要在url后面加上useUnicode=true&characterEncoding=UTF-8"就可以解决中文乱码问题,当然你提交的数据编码也必须是UTF-8的,总而言之,在构造一个网站,整个网站必须采用统一的编码。支持中文的编码有GBK,gb2312,UTF-8等。 例如在struts中利用数据源连接数据库,必须在struts-config.xml做如下配置: <data-sources> <!-- 设置数据源标识 --> <data-source key="mysqlDB1" type="org.apache.tomcat.dbcp.dbcp.BasicDataSource"> <!-- 设置数据库驱动对应类名 --> <set-property property="driverClassName" value="org.gjt.mm.mysql.Driver"/> <!-- 设置待连接数据源URL --> <set-property property="url" value="jdbc:mysql://localhost/firststruts?useUnicode=true&charact erEncoding=UTF-8"/> <!-- 设置同时打开连接的最大数目 --> <set-property property="maxActive" value="5"/> <!-- 设置登录数据库服务器用户名及密码 --> <set-property property="username" value="root"/> <set-property property="password" value=""/> <!-- 设置事务处理中的自动提交 --> <set-property property="autoCommit" value


阅读全文(1444) | 回复(0) | 编辑 | 精华
 



发表评论:
昵称:
密码:
主页:
标题:
验证码:  (不区分大小写,请仔细填写,输错需重写评论内容!)



站点首页 | 联系我们 | 博客注册 | 博客登陆

Sponsored By W3CHINA
W3CHINA Blog 0.8 Processed in 0.484 second(s), page refreshed 144813423 times.
《全国人大常委会关于维护互联网安全的决定》  《计算机信息网络国际联网安全保护管理办法》
苏ICP备05006046号