站内搜索: 请输入搜索关键词

当前页面: 开发资料首页JSP 专题J2EE WEB-Tomcat5.5.9中文问题解决方案

J2EE WEB-Tomcat5.5.9中文问题解决方案

摘要: J2EE WEB-Tomcat5.5.9中文问题解决方案
<tr> <td>   1、html

  无论是独立的html,还是其他程序生成的,如Servlet等,注意在最终的html的和之间必须加入meta标签,用来指定html中输入字符的编码,如:

 
<table cellSpacing=0 borderColorDark=#ffffff cellPadding=2 width=400 align=center borderColorLight=black border=1> <tr> <td class=code style="FONT-SIZE: 9pt" bgColor=#e6e6e6>
 

  <head>

  

  测试GET && POST-Send

  </head>

  
</td></tr></table>


  2、jsp和servlet

  首先必须解决程序输出(如response.writeln(String s))和接受从客户端传来的数据(如request.getParameter(String sname))编码问题,我们可以利用文件过滤功能,具体需要所用的jsp/servlet容器或者服务器提供的功能设置,如在Tomcat5.5.9中可以在webapps/yourAppDirectory/WEB-INF/web.xml中设置如下:

 
<table cellSpacing=0 borderColorDark=#ffffff cellPadding=2 width=400 align=center borderColorLight=black border=1> <tr> <td class=code style="FONT-SIZE: 9pt" bgColor=#e6e6e6>
 

  

  SetCharsetEncodingFilter

  SetCharsetEncodingFilter

  Set CharsetEncoding Filter

  com.gg.comm.web.SetCharsetEncodingFilter

  

  encoding

  gb2312

  

  

  

  SetCharsetEncodingFilter

  /*

  

  
</td></tr></table>


  其中SetCharsetEncodingFilter Class就是用来设置request和reponse字符编码的filter类,其中设置语句如下:

  
<table cellSpacing=0 borderColorDark=#ffffff cellPadding=2 width=400 align=center borderColorLight=black border=1> <tr> <td class=code style="FONT-SIZE: 9pt" bgColor=#e6e6e6>
  request.setCharacterEncoding(targetEncoding);

  response.setContentType("text/html");

  response.setCharacterEncoding(targetEncoding);

  
</td></tr></table>


  另外为了解决通过get(url中带有参数)方式传递参数的乱码问题,我们还需要设置一下url传递参数所需要的编码,具体在Tomcat5.5.9中可以在${Tomcat_home}\conf\server.xml中的之间设置,如下:

  
<table cellSpacing=0 borderColorDark=#ffffff cellPadding=2 width=400 align=center borderColorLight=black border=1> <tr> <td class=code style="FONT-SIZE: 9pt" bgColor=#e6e6e6>
  

  URIEncoding="GBK":Force GET method String(Chinese)

    can be transferd properly by http:uri

  

  note:Tomcat only support GBK specification,so not set charset gb2312

  

  

  

  
</td></tr></table>


  最后为了解决jsp的乱码问题,我们还需要作如下处理,即在左右的jsp头均加上如下指令:

  
<table cellSpacing=0 borderColorDark=#ffffff cellPadding=2 width=400 align=center borderColorLight=black border=1> <tr> <td class=code style="FONT-SIZE: 9pt" bgColor=#e6e6e6>
  

    <%@ page contentType="text/html;charset=gb2312" language="java" %>

  或者

  <%@ page pageEncoding="gb2312"%>

  
</td></tr></table>


  3、JDBC和数据库

  关于写入数据库和读取数据库数据的乱码问题,可以通过如下方式轻松解决:

  对于JAVA程序的处理方法按我们指定的方法处理。

  把数据库默认支持的编码格式改为GBK或GB2312的。

  到此,一般来说对于WEB方式的应用来说,中文问题就可以解决了。当然以上方法是根据统一编码的原则解决的以及WEB方式的文件转换关系(file->class->load->execute or transfered or response or request)来做的。</td> </tr> </table>
↑返回目录
前一篇: 浅淡代码生成
后一篇: 初学者如何开发出高质量的J2EE系统