最新分享 » 编程设计 » 今天解决了困扰很久的JAVA数据库乱码问题

今天解决了困扰很久的JAVA数据库乱码问题

时间:2008-8-25 作者:天空下的雨 分类: 编程设计 评论:0 浏览:4891

现在把它分享出来吧,

第一步:进入安装Tomcat文件夹 >> conf 目录>> 找到 server.xml

修改server.xml配置文件,在Server标签中加入URIEncoding="UTF-8"

<Server URIEncoding="UTF-8" port="8005" shutdown="SHUTDOWN">

第二步:建立一个.filter的类,实现过滤字符

Fil .java

package sun.filter;
import java.io.IOException;
import javax.servlet.*;


public class Fil implements Filter {

 public void destroy() 
         {
            }


 public void doFilter(ServletRequest arg0, ServletResponse arg1,
   FilterChain arg2) throws IOException, ServletException
            {
                  // 将Request 、Response都转化为utf-8编码
                   arg0.setCharacterEncoding("UTF-8");  
                  arg1.setCharacterEncoding("UTF-8");                
                  arg2.doFilter(arg0, arg1);
            }


 public void init(FilterConfig arg0) throws ServletException
          {
              }        
}


第三步: 在网站配置文件web.xml, 为了更好理解把我的给帖出来

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.4"
 xmlns="http://java.sun.com/xml/ns/j2ee"
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
 http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
 
  <filter>
  <filter-name>encoding</filter-name>
  <filter-class>sun.filter.Fil</filter-class>
  </filter>
 
  <servlet>
    <servlet-name>userpass</servlet-name>
    <servlet-class>sun.sta.userpass</servlet-class>
  </servlet>


<filter-mapping>
     <filter-name>encoding</filter-name>
      <url-pattern>/*</url-pattern>
    </filter-mapping>
   
  <servlet-mapping>
    <servlet-name>userpass</servlet-name>
    <url-pattern>/userpass</url-pattern>
  </servlet-mapping>


  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
</web-app>

评论:

一只菜尿
2008-08-25 19:28
一堆错误,哥哥你好歹查一下再发上来啊
蔷薇
2008-08-10 21:55
找了半天,终于搜索找到详细的了,谢谢~!