开发环境:eclipse3.3+myeclipse6.0+jdk6.0+tomcat6.0+mysql5.5
使用的技术:struts和hibernate
首先建议使用utf-8就不会有这样的问题;如果不使用utf-8,可以参考以下的方法:
1.在每个jsp文件的开头都加有
<%@ page language="java" contentType="text/html; charset=GB2312" pageEncoding="GB2312"%>
2.mysql的一个配置文件my.ini 中,将default-character-set=latin1 改成default-character-set=gbk
3.mysql表的字符集都用的是gbk;
4.hibernate.cfg.xml为
<?xml version='1.0' encoding='UTF-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 2.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-2.0.dtd">
<!-- Generated by MyEclipse Hibernate Tools. -->
<hibernate-configuration>
<session-factory>
<property name="dialect">
net.sf.hibernate.dialect.MySQLDialect
</property>
<property name="connection.url">
<![CDATA[jdbc:mysql://localhost:3306/webdemodb?useUnicode=true&characterEncoding=GBK&mysqlEncoding=GBK ]]>
</property>
<property name="connection.username">root</property>
<property name="connection.password">admin</property>
<property name="connection.useUnicode">true</property>
<property name="connection.characterEncoding">GBK</property>
<property name="connection.driver_class">
com.mysql.jdbc.Driver
</property>
<property name="myeclipse.connection.profile">
mysql_Demo
</property>
<mapping resource="com/mycompony/webdemo/po/Disposeinfo.hbm.xml" />
<mapping resource="com/mycompony/webdemo/po/Progress.hbm.xml" ></mapping>
<mapping resource="com/mycompony/webdemo/po/Role.hbm.xml" />
<mapping resource="com/mycompony/webdemo/po/User.hbm.xml" />
</session-factory>
</hibernate-configuration>
5.新建EncodingFilter.java文件:
package com.cs2c.webdemo.util;
import java.io.*;
import javax.servlet.*;
public class EncodingFilter implements Filter
{
protected String encoding = null;
protected FilterConfig config;
public void init(FilterConfig filterConfig) throws ServletException
{
this.config = filterConfig;
//从web.xml配置文件中获取编码配置
this.encoding = filterConfig.getInitParameter("Encoding");
}
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException
{
if(request.getCharacterEncoding() == null)
{
String encode = getEncoding();
if(encode != null)
{
//设置request的编码方式
request.setCharacterEncoding(encode);
}
}
chain.doFilter(request,response);
}
public String getEncoding()
{
return encoding;
}
public void destroy()
{
}
}
===========================================================================
6.web.xml为:
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="2.5" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<filter>
<filter-name>encodingFilter</filter-name>
<filter-class>com.cs2c.webdemo.util.EncodingFilter</filter-class>
<init-param>
<param-name>Encoding</param-name>
<param-value>GB2312</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>encodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<!--页面请求编码过滤器结束-->
<servlet>
<servlet-name>action</servlet-name>
<servlet-class>org.apache.struts.action.ActionServlet</servlet-class>
<init-param>
<param-name>config</param-name>
<param-value>/WEB-INF/struts-config.xml</param-value>
</init-param>
<init-param>
<param-name>debug</param-name>
<param-value>3</param-value>
</init-param>
<init-param>
<param-name>detail</param-name>
<param-value>3</param-value>
</init-param>
<load-on-startup>0</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>action</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>