读书人

jsp中文乱码有关问题解决

发布时间: 2012-11-14 10:12:18 作者: rapoo

jsp中文乱码问题解决

jsp中文乱码问题解决方法一、JSP页面显示乱码下面的显示页面(display.jsp)就出现乱码:<html><head><title>JSP的中文处理</title><meta http-equiv="Content-Type" content="text/html; charset=gb2312"></head><body><%out.print("JSP的中文处理");%></body></html>对不同的WEB服务器和不同的JDK版本,处理结果就不一样。原因:服务器使用的编码方式不同和浏览器对不同的字符显示结果不同而导致的。解决办法:在JSP页面中指定编码方式(gb2312),即在页面的第一行加上:<%@ page contentType="text/html; charset=gb2312"%>,就可以消除乱码了。完整页面如下:<%@ page contentType="text/html; charset=gb2312"%><html><head><title>JSP的中文处理</title><meta http-equiv="Content-Type" content="text/html; charset=gb2312"></head><body><%out.print("JSP的中文处理");%></body></html>二、表单提交中文时出现乱码下面是一个提交页面(submit.jsp),代码如下:<html><head><title>JSP的中文处理</title><meta http-equiv="Content-Type" content="text/html; charset=gb2312"></head><body><form name="form1" method="post" action="process.jsp"><div align="center"><input type="text" name="name"><input type="submit" name="Submit" value="Submit"></div></form></body></html>下面是处理页面(process.jsp)代码:<%@ page contentType="text/html; charset=gb2312"%><html><head><title>JSP的中文处理</title><meta http-equiv="Content-Type" content="text/html; charset=gb2312"></head><body><%=request.getParameter("name")%></body></html>如果submit.jsp提交英文字符能正确显示,如果提交中文时就会出现乱码。原因:浏览器默认使用UTF-8编码方式来发送请求,而UTF- 8和GB2312编码方式表示字符时不一样,这样就出现了不能识别字符。解决办法:通过request.setCharacterEncoding ("gb2312")对请求进行统一编码,就实现了中文的正常显示。修改后的process.jsp代码如下:<%@ page contentType="text/html; charset=gb2312"%><%request.setCharacterEncoding("gb2312");%><html><head><title>JSP的中文处理</title><meta http-equiv="Content-Type" content="text/html; charset=gb2312"></head><body><%=request.getParameter("name")%></body></html>三、数据库连接出现乱码只要涉及中文的地方全部是乱码,解决办法:在数据库的数据库URL中加上useUnicode=true&characterEncoding=GBK 就OK了。四、数据库的显示乱码在mysql4.1.0中,varchar类型,text类型就会出现中文乱码,对于varchar类型把它设为binary属性就可以解决中文问题,对于text类型就要用一个编码转换类来处理,实现如下:public class Convert {/** 把ISO-8859-1码转换成GB2312*/public static String ISOtoGB(String iso){String gb;try{if(iso.equals("") || iso == null){return "";}else{iso = iso.trim();gb = new String(iso.getBytes("ISO-8859-1"),"GB2312");return gb;}}catch(Exception e){System.err.print("编码转换错误:"+e.getMessage());return "";}}}把它编译成class,就可以调用Convert类的静态方法ISOtoGB()来转换编码。总结:1. 在jsp中<%@ page contentType="text/html; charset=A" %>如果指定了,那么在改jsp中所有构造的String(不是引用),如果有指定编码,那么这些String的编码是A的。从request的得到的String如果有指定request的编码的话,他是iso-8859-1的从别的地方得到的String是使用原初始的编码的,比如从数据库得到String,如果数据库的编码是B,那么该String的编码是B而不是A的,也不是系统默认的。此时,如果要输出的String的编码不是A,那么,很可能显示乱码的,所以首先要将String正转化为编码A的String,然后输出。2. 在jsp中<%@ page contentType="text/html; charset=A" %>有指定,那么相当于指定了<%@ page contentType="text/html; charset=ISO-8859-1" %>3. Servelte中如果执行了像 response.setContentType("text/html;charset=A");明将response的字符输出流编码设置为A,所有要输出的String的编码要转化为A的,否会得到乱码的。Servelet中从request得到的String的编码和jsp中一样的,但是在servlet java文件中构造的String是使用的系统默认的编码的。在servelt中从外部得到的String 是使用原来的编码的,比如从编码为B的数据库得到的数据是编码为B的,不是A,也不是系统默认的编码。 本文来源:IT传媒网原文链接:http://www.cniter.com/tech/jsp/10142_1.html-------------------补充: 2)表单提交乱码         表单提交时(post和Get方法),使用request.getParameter方法得到乱码,这是因为tomcat处理提交的参数时默认的是iso-8859-1,表单提交get和post处理乱码问题不同,下面分别说明。    (1)POST处理    对post提交的表单通过编写一个过滤器的方法来解决,过滤器在用户提交的数据被处理之前被调用,可以在这里改变参数的编码方式,过滤器的代码如下:Java代码 package example.util;              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;                    public void destroy() {                this.encoding = null;         this.filterConfig = null;               }               public void doFilter(ServletRequest request, ServletResponse response,         <STRONG><SPAN style="COLOR: #ff0000"> FilterChain chain) throws IOException, ServletException {                    if (ignore || (request.getCharacterEncoding() == null)) {          String encoding = selectEncoding(request);          if (encoding != null) {           request.setCharacterEncoding(encoding);          }         }</SPAN>   </STRONG>             // Pass control on to the next filter         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")) {          this.ignore = true;         } else if (value.equalsIgnoreCase("yes")) {          this.ignore = true;         } else {          this.ignore = false;         }               }               protected String selectEncoding(ServletRequest request) {                return (this.encoding);               }              }  package example.util;        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;           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);       }      }          // Pass control on to the next filter      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")) {       this.ignore = true;      } else if (value.equalsIgnoreCase("yes")) {       this.ignore = true;      } else {       this.ignore = false;      }         }         protected String selectEncoding(ServletRequest request) {          return (this.encoding);         }        }     文中红色的代码即为处理乱码的代码。      web.xml文件加入过滤器 Xml代码 <filter>      <filter-name>Encoding</filter-name>      <filter-class>              example.util.SetCharacterEncodingFilter        </filter-class>      <init-param>     <param-name>encoding</param-name>     <param-value>gbk</param-value>     <!--gbk或者gb2312或者utf-8-->    </init-param>    <init-param>     <param-name>ignore</param-name>     <param-value>true</param-value>    </init-param>   </filter>  <filter>    <filter-name>Encoding</filter-name>    <filter-class>            example.util.SetCharacterEncodingFilter     </filter-class>    <init-param>   <param-name>encoding</param-name>   <param-value>gbk</param-value>   <!--gbk或者gb2312或者utf-8-->  </init-param>  <init-param>   <param-name>ignore</param-name>   <param-value>true</param-value>  </init-param> </filter>Xml代码 <filter-mapping>    <filter-name>Encoding</filter-name>    <servlet-name>/*</servlet-name>   </filter-mapping>  <filter-mapping>  <filter-name>Encoding</filter-name>  <servlet-name>/*</servlet-name> </filter-mapping> (2) Get方法的处理 tomcat对post和get的处理方法不一样,所以过滤器不能解决get的乱码问题,它需要在其他地方设置。 打开<tomcat_home>\conf目录下server.xml文件,找到对8080端口进行服务的Connector组件的设置部分,给这个组件添加一个属性:URIEncoding="GBK"。修改后的Connector设置为:  Java代码 <Connector port="8080" maxHttpHeaderSize="8192"                 maxThreads="150" minSpareThreads="25" maxSpareThreads="75"                 enableLookups="false" redirectPort="8443" acceptCount="100"                 connectionTimeout="20000" disableUploadTimeout="true" <SPAN style="COLOR: #ff0000">URIEncoding="GBK"</SPAN> />  <Connector port="8080" maxHttpHeaderSize="8192"               maxThreads="150" minSpareThreads="25" maxSpareThreads="75"               enableLookups="false" redirectPort="8443" acceptCount="100"               connectionTimeout="20000" disableUploadTimeout="true" URIEncoding="GBK" />  * 注意修改后重新启动tomcat才能起作用。

读书人网 >JavaScript

热点推荐