近日,使用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!就可以了。