近日,使用struts 1.1,发现讨厌的中文乱码问题,在form的传送过程和入库时候出现。就我在网络上找的方法罗列如下:
(Tomcat 5.0.28+struts 1.1+hibernate 2.1+sqlserver2k) 1.直接转编码
public static String isoToGB(String src)
{
String strRet=null; try{ strRet = new String(src.getBytes("ISO_8859_1"),"GB2312"); }catch(Exception e) { }
return strRet;
}
通过一个函数转编码,我没有成功,不知为何!
2.过滤filter设置法 package yourbean; import javax.servlet.*; import javax.servlet.http.*; import java.io.*; import java.util.*; public class servfilter extends HttpServlet implements Filter {
private FilterConfig filterConfig; //Handle the passed-in FilterConfig
public void init(FilterConfig filterConfig) { this.filterConfig = filterConfig; } //Process the request/response pair
public void doFilter(ServletRequest request, ServletResponse response, FilterChain filterChain) {
try { request.setCharacterEncoding("GB2312");
((HttpServletResponse)response).setHeader("Cache-control","no-cache");
response.setHeader("Pragma","No-cache"); response.setHeader("Cache-Control","no-cache"); response.setHeader("Expires","0");
((HttpServletResponse)response).setHeader("Pragram","no-cache");
filterChain.doFilter(request, response); }
catch(ServletException sx) { filterConfig.getServletContext().log(sx.getMessage()); }
catch(IOException iox) { filterConfig.getServletContext().log(iox.getMessage()); } } //Clean up resources
public void destroy() { }
}下面是一个web.xml文件你用jbuilder写上面的bean的时候会生成一个<?xml version="1.0" encoding="ISO-8859-1"?>
<!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" "http://java.sun.com/dtd/web-app_2_3.dtd"> <web-app> <display-name>Welcome to Tomcat</display-name> <description> Welcome to Tomcat </description> <filter> <filter-name>servfilter</filter-name> <filter-class>yourbean.servfilter</filter-class> </filter> <filter-mapping> <filter-name>servfilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping></web-app>把上面的servfilter编译放在你的web-inf/classes/yourbean/下web.xml放在web-inf/下和classes在一个目录下在每个jsp页面上加上<%@page contentType="text/html;charset=GBK"%> 也不是很方便,而且在tomcat也没有成功,继续郁闷! 3.我现在使用方法,推荐!! 写一个myActionServlet来并覆盖ActionServlet中的process()方法。 protected void process(HttpServletRequest request, HttpServletResponse response) throws java.io.IOException, javax.servlet.ServletException { /**@todo Override this org.apache.struts.action.ActionServlet method*/ request.setCharacterEncoding("GB2312");//就加着一行一切都解决了 super.process(request, response); } 当然别忘了改一下web.xml里面的配置 <servlet> <servlet-name>action</servlet-name> <servlet-class>strutsdemo.myActionServlet</servlet-class> <init-param> <param-name>debug</param-name> <param-value>2</param-value> </init-param> <init-param> <param-name>config</param-name> <param-value>/WEB-INF/struts-config.xml</param-value> </init-param> <load-on-startup>2</load-on-startup> </servlet> 改一下servlet-class标签中的内容就可以! 真的可以,一劳用yi! 具体编码的理论就不说了,google上已经够多了。 另外,如果不用struts的话,hibernate也可能碰到中文乱码问题,只要在hibernate.cfg.xml配置中如下: <property name="hibernate.connection.url"> jdbc:microsoft:sqlserver://Localhost:1433;SelectMethod=cursor;characterEncoding=GBK;DatabaseName=myDatabase. </property> characterEncoding=GBK!就可以了。 ******************************************************************************************************************** Java/J2EE中文问题终极解决之道
Java中文问题一直困扰着很多初学者,如果了解了Java系统的中文问题原理,我们就可以对中文问题能够采取根本的解决之道。
最古老的解决方案是使用String的字节码转换,这种方案问题是不方便,我们需要破坏对象封装性,进行字节码转换。 还有一种方式是对J2EE容器进行编码设置,如果J2EE应用系统脱离该容器,则会发生乱码,而且指定容器配置不符合J2EE应用和容器分离的原则。 在Java内部运算中,涉及到的所有字符串都会被转化为UTF-8编码来进行运算。那么,在被Java转化之前,字符串是什么样的字符集? Java总是根据操作系统的默认编码字符集来决定字符串的初始编码,而且Java系统的输入和输出的都是采取操作系统的默认编码。 因此,如果能统一Java系统的输入、输出和操作系统3者的编码字符集合,将能够使Java系统正确处理和显示汉字。这是处理Java系统汉字的一个原则,但是在实际项目中,能够正确抓住和控制住Java系统的输入和输出部分是比较难的。J2EE中,由于涉及到外部浏览器和数据库等,所以中文问题乱码显得非常突出。 J2EE应用程序是运行在J2EE容器中。在这个系统中,输入途径有很多种:一种是通过页面表单打包成请求(request)发往服务器的;第二种是通过数据库读入;还有第3种输入比较复杂,JSP在第一次运行时总是被编译成Servlet,JSP中常常包含中文字符,那么编译使用javac时,Java将根据默认的操作系统编码作为初始编码。除非特别指定,如在Jbuilder/eclipse中可以指定默认的字符集。 输出途径也有几种:第一种是JSP页面的输出。由于JSP页面已经被编译成Servlet,那么在输出时,也将根据操作系统的默认编码来选择输出编码,除非指定输出编码方式;还有输出途径是数据库,将字符串输出到数据库。 由此看来,一个J2EE系统的输入输出是非常复杂,而且是动态变化的,而Java是跨平台运行的,在实际编译和运行中,都可能涉及到不同的操作系统,如果任由Java自由根据操作系统来决定输入输出的编码字符集,这将不可控制地出现乱码。 正是由于Java的跨平台特性,使得字符集问题必须由具体系统来统一解决,所以在一个Java应用系统中,解决中文乱码的根本办法是明确指定整个应用系统统一字符集。 指定统一字符集时,到底是指定ISO8859_1 、GBK还是UTF-8呢? (1)如统一指定为ISO8859_1,因为目前大多数软件都是西方人编制的,他们默认的字符集就是ISO8859_1,包括操作系统Linux和数据库MySQL等。这样,如果指定Jive统一编码为ISO8859_1,那么就有下面3个环节必须把握: 开发和编译代码时指定字符集为ISO8859_1。 运行操作系统的默认编码必须是ISO8859_1,如Linux。 在JSP头部声明:<%@ page contentType="text/html;charset=ISO8859_1" %>。 (2)如果统一指定为GBK中文字符集,上述3个环节同样需要做到,不同的是只能运行在默认编码为GBK的操作系统,如中文Windows。 统一编码为ISO8859_1和GBK虽然带来编制代码的方便,但是各自只能在相应的操作系统上运行。但是也破坏了Java跨平台运行的优越性,只在一定范围内行得通。例如,为了使得GBK编码在linux上运行,设置Linux编码为GBK。 那么有没有一种除了应用系统以外不需要进行任何附加设置的中文编码根本解决方案呢? 将Java/J2EE系统的统一编码定义为UTF-8。UTF-8编码是一种兼容所有语言的编码方式,惟一比较麻烦的就是要找到应用系统的所有出入口,然后使用UTF-8去“结扎”它。 一个J2EE应用系统需要做下列几步工作:
以上讨论了Java/J2EE的中文问题。如果整个应用系统是从开始进行开发,那么统一指定编码为UTF-8就非常容易做到。如果是在英文源代码基础上二次开发,那么首先要将原来的源代码转换为统一编码UTF-8,那么这种转换工作会带来一定的麻烦。 有了这个解决方案,无论使用什么框架Struts 或JSF或未来出现的Java技术,统一成UTF-8的方案都不会出现乱码,笔者以前在Jsp/Servlet时就基于这个原则,后来使用Struts等框架,从未被乱码困扰过,希望本方案公布出来供更多初学者分享,减少Java/J2EE的第一个拦路虎,也避免采取一些临时解决方案。 hibernate+mysql写入数据库的中文是乱码,怎么解决?? hibernate.hbm.xml加上属性. 1.使ApplicationResources.properties支持中文 |
struts、spring、hibernate中文乱码问题解决
最新推荐文章于 2019-07-10 21:31:00 发布
struts、spring、hibernate中文乱码问题解决
2008年01月08日 星期二 18:44