自己总结的(建议使用方法1)
方法一:
****************
写个过滤器类:
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 Filter1 implements Filter {
private FilterConfig filterConfig=null;
private String encoding=null;
public void init(FilterConfig filterConfig) throws ServletException {
// TODO 自动生成方法存根
this.filterConfig=filterConfig;
this.encoding=this.filterConfig.getInitParameter("encoding");
}
public void doFilter(ServletRequest request, ServletResponse response,
FilterChain chain) throws IOException, ServletException {
// TODO 自动生成方法存根
if(encoding!=null)
{
request.setCharacterEncoding(encoding);
}
chain.doFilter(request, response);
}
public void destroy() {
// TODO 自动生成方法存根
this.encoding=null;
this.filterConfig=null;
}
}
再在项目的web.xml中配置如下:
<filter>
<filter-name>filter1</filter-name>
<filter-class>com.tools.Filter1</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>filter1</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
注意:所有的JSP页面的类型均为<%@ page contentType="text/html; charset=UTF-8"%>
//////////////////////////////////////////////////////////////////////////////
方法二:
/////////////////////////////////////////////////////////////////////////
在struts中的可以重写ActionServlet类的process方法
package tools;//包
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.*;
import org.apache.struts.action.ActionServlet;
public class MyActionServlet extends ActionServlet{
private static final long serialVersionUID = 6864646927139403188L;
public MyActionServlet()
{
}
protected void process(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
request.setCharacterEncoding("UTF-8");
super.process(request, response);
}
}
再在项目的web.xml中
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="2.4" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<servlet>
<servlet-name>action</servlet-name>
<servlet-class>tools.MyActionServlet</servlet-class>/////////////根据自己的的MyActionSerclet所在的位置
<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>
</web-app>
注意:所有的JSP页面的类型均为<%@ page contentType="text/html; charset=UTF-8"%>
1中方法=====================================================
要是数据库是MYSQL还要在applicationContext.xml配置中加
<property name="url" value="jdbc:mysql://localhost:3306/j2ee?useUnicode=true&characterEncoding=UTF-8"></property>
2中方法.****************************************************
MYSQL 中的hibernate.cfg.xml在<session-factory>和</session-factory>之间加入这么一段:
<property name="connection.useUnicode">true</property>
<property name="connection.characterEncoding">UTF-8</property>
这样一来,是可以解决乱码问题。
posted on 2007-09-22 13:45
Crying 阅读(162)
评论(0) 编辑 收藏 所属分类:
数据库