Java Web的中文问题

摘自王俊标编著的《精通Java Web开发-基于Struts EJB Hibernate JBuilder》一书:

1、页面请求
   采用Filter过滤器方式:
                  web.xml片段

 1 None.gif ……
 2 None.gif <!--  filter 配置  -->
 3 None.gif < filter >
 4 None.gif     <!--  filter 别名  -->
 5 None.gif         < filter-name > encodingfilter </ filter-name >
 6 None.gif         <!--  类文件位置  -->
 7 None.gif         < filter-class > login3.EncodingFilter </ filter-class >
 8 None.gif        
 9 None.gif         <!--  定义属性encoding="gb2312" 即指定编码类型  -->
10 None.gif         < init-param >
11 None.gif             < desription > encode to gb2312 </ desription >
12 None.gif             < param-name > encoding </ param-name >
13 None.gif             < param-value > gb2312 </ param-value >
14 None.gif         </ init-param >
15 None.gif </ filter >
16 None.gif
17 None.gif <!--  filter 映射配置  -->
18 None.gif < filter-mapping >
19 None.gif     <!--  定义该filter 处理所有格式的请求  -->
20 None.gif     < filter-name > encodingfilter </ filter-name >
21 None.gif     < url-pattern > /* </ url-pattern >
22 None.gif </ filter-mapping >
23 None.gif……

         EncodingFilter.java

 1 None.gif import  java.io.IOException;
 2 None.gif
 3 None.gif import  javax.servlet. * ;
 4 None.gif import  javax.servlet.http.HttpServlet;
 5 None.gif
 6 None.gif public   class  EncodingFilter  extends  HttpServlet 
 7 ExpandedBlockStart.gifContractedBlock.gif     implements  Filter  dot.gif {
 8 InBlock.gif    
 9 InBlock.gif     private  FilterConfig filterConfig;
10 InBlock.gif    
11 InBlock.gif     private  String targetEncoding  =   null ;
12 InBlock.gif    
13 ExpandedSubBlockStart.gifContractedSubBlock.gif     public   void  init(FilterConfig filterConfig)  throws  ServletException  dot.gif {
14 InBlock.gif         this .filterConfig  =  filterConfig;
15 InBlock.gif         this .targetEncoding  =   this .filterConfig.getInitParameter( " encoding " );
16 ExpandedSubBlockEnd.gif    }

17 InBlock.gif
18 InBlock.gif     public   void  doFilter(ServletRequest request, ServletResponse response,
19 ExpandedSubBlockStart.gifContractedSubBlock.gif            FilterChain chain)  throws  IOException, ServletException  dot.gif {
20 ExpandedSubBlockStart.gifContractedSubBlock.gif         try   dot.gif {
21 InBlock.gif            System.out.println( " targetEncoding : "   +  targetEncoding  +   " \n " );
22 InBlock.gif            request.setCharacterEncoding(targetEncoding);
23 InBlock.gif            chain.doFilter(request, response);
24 ExpandedSubBlockStart.gifContractedSubBlock.gif        }
  catch  (ServletException sx)  dot.gif {
25 InBlock.gif            filterConfig.getServletContext().log(sx.getMessage());
26 ExpandedSubBlockStart.gifContractedSubBlock.gif        }
  catch  (IOException iox)  dot.gif {
27 InBlock.gif            filterConfig.getServletContext().log(iox.getMessage());
28 ExpandedSubBlockEnd.gif        }

29 ExpandedSubBlockEnd.gif    }

30 InBlock.gif    
31 ExpandedSubBlockStart.gifContractedSubBlock.gif     public   void  destroy()  dot.gif {
32 InBlock.gif         this .filterConfig  =   null ;
33 InBlock.gif         this .targetEncoding  =   null ;
34 ExpandedSubBlockEnd.gif    }
    
35 ExpandedBlockEnd.gif}

36 None.gif


2、Web容器编码
      在Tomcat的的源代码中的getParameter()方法,用户调用request.getParameter()方法时,首先调用parsetParameters()方法,在这里面将编码进行转换。如:
      String s_target = new String (s_source.getBytes("iso-8859-1"), "gb2312);

3、数据库
      MySQL中文问题的主要原因是:它对数据进行编码时,采用了和底层的操作系统所不同的编码。通常MySQL在中文Windows平台上正确的编码方式是GBK。因此只要将MySQL安装目录下的my.ini文件中的default-character-set选项设为GBK即可。

4、响应结果
      对于Servlet的代码处理,可以使用:
         ……
         respsonse.setContentType("text/html;charset=GB2312");
         ……
      对于JSP文件,可以在开头部分添加如下声明:
         ……
         <%@ page contentType="text/html;charset=GB2312" %>
         ……
      对于HTML页面文件,则在开头部分添加声明如下:
         ……
         <head>
            <META HTTP-EQUIV="Content-Type" CONTENT="text/html;charset=GB2312">
         </head>
         ……
5、XML文件
      在采用"UTF-8"方式编码的情况下,使用XML文件存储中文数据常常会出现乱码的问题,建议使用"GB2312":
      <?xml version='1.0' encoding="GB2312"

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值