str=new String(str.getBytes("ISO8859_1")); 
上面两种方法不能混用,意思就是要么就是GBK,要么就是ISO8859_1,从里到外都一样就好了。数据库JDBC的中文问题,一般只要按照数据库指定的编码进行转换,比如按照ISO8859_1读,ISO8859_1写

解决方案 »

  1.   

    如果我数据库里面存的是GBK我该怎么做啊?
      

  2.   

    写一个filter!
    编码从web.xml配置里读取import javax.servlet.ServletException;
    import javax.servlet.ServletContext;
    import javax.servlet.Filter;
    import javax.servlet.FilterChain;
    import javax.servlet.FilterConfig;
    import javax.servlet.http.HttpSession;
    import javax.servlet.ServletRequest;
    import javax.servlet.ServletResponse;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    import java.io.IOException;public class EncodingFilter
        implements Filter {  private FilterConfig config = null;
      // default to ASCII
      private String targetEncoding = "ASCII";  public void init(FilterConfig config) throws ServletException {
        this.config = config;
        this.targetEncoding = config.getInitParameter("encoding");
      }  public void destroy() {
        config = null;
        targetEncoding = null;
      }  public void doFilter(ServletRequest srequest, ServletResponse sresponse,
                           FilterChain chain) throws IOException, ServletException {    HttpServletRequest request = (HttpServletRequest) srequest;
        request.setCharacterEncoding(targetEncoding);
        // move on to the next
        chain.doFilter(srequest, sresponse);
      }}
      

  3.   

    可以用一楼的转码方式。也可以用牛仔的过滤器方式,由jsp容器来完成。我建议用过滤器。过滤器需要配置web-inf\web.xml文件。加以下内容:
    加到<web-app>标签里面
      <filter>
        <filter-name>EncodingFilter</filter-name>
        <display-name>EncodingFilter</display-name>
        <description>Encoding Filter for Chance OA</description>
        <filter-class>com.dmk.chance.common.system.EncodingFilter</filter-class>
        <init-param>
          <param-name>encoding</param-name>
          <param-value>GBK</param-value>
        </init-param>
        <init-param>
          <param-name>loginURL</param-name>
          <param-value>/oa/system/login/login.jsp</param-value>
        </init-param>
      </filter>
      <filter-mapping>
        <filter-name>EncodingFilter</filter-name>
        <url-pattern>/*</url-pattern>
      </filter-mapping>EnclodingFilter.java文件
    package com.dmk.chance.common.system;/**
     * <p>Title: JSP中文编码过滤器</p>
     * <p>Description: 1.JSP中文显示乱码的解决。统一编码过滤
     *                 2.登录过滤</p>
     * <p>Copyright: Copyright (c) 2004</p>
     * <p>Company: </p>
     * @author billyw
     * @version 1.0
     *
     * History : Mon Apr 12 09:08:30 2004 wxy
     *
     */import java.io.IOException;
    import javax.servlet.Filter;
    import javax.servlet.FilterChain;
    import javax.servlet.FilterConfig;
    import javax.servlet.ServletRequest;
    import javax.servlet.ServletResponse;
    import javax.servlet.ServletException;
    import javax.servlet.http.HttpSession;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;public class EncodingFilter
        implements Filter {  private FilterConfig config = null;
      // default to GBK
      private String targetEncoding = "GBK";
      private String loginURL="";  public void init(FilterConfig config) throws ServletException {
        this.config = config;
        this.targetEncoding = config.getInitParameter("encoding");
        this.loginURL=config.getInitParameter("loginURL");
      }  public void destroy() {
        config = null;
        targetEncoding = null;
      }  public void doFilter(ServletRequest srequest, ServletResponse sresponse,
                           FilterChain chain) throws IOException, ServletException {    HttpServletRequest request = (HttpServletRequest) srequest;
        HttpServletResponse response=(HttpServletResponse) sresponse;
        request.setCharacterEncoding(targetEncoding);
        String rURL=request.getRequestURI();
        if(rURL.toLowerCase().indexOf("login")==-1 && request.getSession().getAttribute("UserBean")==null){
          response.sendRedirect(request.getContextPath()+loginURL);
          return;
        }
        chain.doFilter(srequest, sresponse);
      }
    }
      

  4.   

    我很多jsp跟楼主写的差不多,在tomcat(5.0.25)跑得很正常啊,楼主还是先检查下程序吧,乱码问题只要经过转码就没问题了
      

  5.   

    我这么写的,干脆什么都不出来,连乱码都没有了
    public String[][] getTableData(String strSQL)
    {
    String[][] result=null;
    ResultSet rs=null;
    ResultSetMetaData rsmd=null;
    int columnCount,rowCount=0;
    try{
    rs = getResultSet(strSQL,true);
    //得到结果集有多少行和多少列
    rs.last();
    rowCount =rs.getRow();
    if(rowCount==0) { return result; }
    rsmd = rs.getMetaData();
    columnCount = rsmd.getColumnCount();
    result = new String[rowCount][columnCount];
    //填充返回值数组
    String strTmp;
    boolean flag = rs.first();
    while(flag)
    {
    for(int i=0 ; i < result.length ; i++)
    {
    for(int j=1; j <= result[0].length ; j++)
    {
    strTmp = rs.getString(j);
    strTmp = new String(strTmp.getBytes("ISO8859_1"),"gb2312");
    if(strTmp==null){result[i][j-1] = "";}
    else{ result[i][j-1] = strTmp.trim();}
    }
    flag = rs.next();
    }
    }
    }
    catch(Exception e){}
    finally{
    try{
    if(rs!=null) { rs.close();}
    closeConnection();
    }
    catch(Exception e){}
    }
    return result;
    }
      

  6.   

    晕啊,你这样当然不行啊,数据库里的值取出来后,传递到jsp,它又会自动转型成8859_1的编码。你先取出数值,然后传到jsp里,在要显示的地方进行转型,试试看,不行再说
      

  7.   

    我这样做试了一下,后来我也试过你说的方法
    <%if (TZdata != null){
    for(int k=0 ; k < TZdata.length ; k++){%>
                            <a href="javascript:openwindow('gsdt/tz_view.jsp?id=<%=TZdata[k][0]%>')" title="发布日期:<%=TZdata[k][2].substring(0,10)%>">&nbsp;&nbsp;<%=new String(TZdata[k][1].getBytes("ISO8859_1"),"gb2312")%></a> 
                            <%out.println("<br><br>");}}%>
    但结果不是问号了,是@@之类的乱码了
      

  8.   

    代码不用改,还用4.0的那个,只要在web。xml内加入
    <mime-mapping> 
    <extension>htm</extension> 
    <mime-type>text/html; charset=gb2312</mime-type>
    </mime-mapping>
    <mime-mapping>
    <extension>html</extension>
    <mime-type>text/html; charset=gb2312</mime-type>
    </mime-mapping>
    这个就行了
      

  9.   

    我的也是在tomcat4.1.27上没乱码问题.但升级到tomcat5.0.19上用Get方法的时候出现问号的乱码情况。于是搜到了楼上几位说的解决方法,可还是不行啊,还是出现同样的问题。但后来从jakarta网站上下载了改编码之后的tomcat5.0.19的jasper-compiler.jar,jasper-runtime.jar,并替换原来的就好了.现在我又升级到tomcat5.0.27了.幸运的是没有换什么东西就可以跑程序了,什么毛病没有.
      

  10.   

    楼上各位的讲解真的很出色,还有不同的方法,也说不上那种方法好,那种方法不好,我是便向于
    小蜜蜂的,可是,由于编码习惯不同我经常使用:
    str = new String(str.getBytes("8859_1"),"gb2312"); 
    还要提醒搂主,千万不要对从数据中提取的值直接进行转换,从数据库中取出的值,只要暂时不显示就不要转换,应该在最前端进行转换,也就是jsp页面要显示的地方,这点与楼上的某位仁兄比较接近你出现的??变成了更乱的乱码可能是因为你进行多次转换造成的后果,这种情况我以前经常遇到。能力有限,最好能够帮到你。