天天看点

servlet中如何解决中文乱码的问题!...

一般中文乱码都是因为你字符集的设定不当而致的。一般servlet的中文乱码问题针对各种不同的情况有四种解决的方案:

1,首先你是通过post方式提交表单的,这样的话,那你只需要设定request,response的字符集为utf-8或gbk即可(request.setCharacterEncoding("utf-8");response.setContentType("text/html;charset=utf-8");).

2,针对提交方式为get和url重写。如果是这两种方式,那么就需要封装一个方法,

public String tocn(String str)

 {

  String rs=null;

  byte[] temp;

  try {

   temp=str.getBytes();

   rs=new String(temp,"utf-8");

  } catch (Exception e) {

   e.printStackTrace();

  }

  return rs;

 }

只需调用即可.

3.在tomcat中的server.xml中修改

 <Connector port="9000" protocol="HTTP/1.1"

               connectionTimeout="20000"

               redirectPort="8443" URIEncoding='utf-8'/>

其中[URIEncoding='utf-8']为新增的。

4.写一个过滤器(记得在web.xml中配置filter节点).