////////////////中文转为字符串(进入数据库)
  public static String toISO8859(String str) {
    if (str == null) {
      return null;
    }
    try {
      return new String(str.getBytes("ISO8859_1"));
    }
    catch (UnsupportedEncodingException e) {
      e.printStackTrace();
    }
    return null;
  }

解决方案 »

  1.   

    是啊,因为我的form同时接收jsp数据和action甚至bean的数据
    所以我希望做成一个通用的函数
    但是竟然把我的正确的汉字转化成乱码了,晕死
    我不知道原理
    哪个高手可以告诉我
      

  2.   

    你试试这么写:public static String codeString(String s){
        String str = "error" ;
        try{
          str=new String(URLDecoder.decode(s).getBytes("ISO-8859-1"), "gb2312"));
          return str;
        }
        catch(Exception e){
          return str;
        }
      }
      

  3.   

    package com.ogi.filter;import javax.servlet.*;
    import java.io.*;
    /**
     * @author guest1
     *
     * TODO To change the template for this generated type comment go to
     * Window - Preferences - Java - Code Style - Code Templates
     *
     * setup on web.xml
     *<filter>
     *<filter-name>Set Character Encoding</filter-name>
     *<filter-class>com.learndiary.website.util.SetCharacterEncodingFilter</filter-class>
     *<init-param>
     *<param-name>encoding</param-name>
     *<param-value>utf-8</param-value>  
     *</init-param>
     *<init-param>
     *<param-name>ignore</param-name>
     *<param-value>true</param-value>
     *</init-param>
     *</filter>
     */
    public abstract class SetCharacterEncodingFilter implements Filter{
    protected String encoding = null;
    protected FilterConfig filterConfig = null;
    protected boolean ignore = true;

    public void destory(){
    this.encoding = null;
    this.filterConfig = null;
    }

    public void doFilter(ServletRequest request,ServletResponse response,FilterChain chain) throws IOException,ServletException{
    if(ignore || request.getCharacterEncoding() == null){
    String encoding = selectEncoding(request);
    }
    if(encoding != null){
    request.setCharacterEncoding(encoding);
    }
    chain.doFilter(request,response);
    }

    public void init(FilterConfig filterConfig) throws ServletException{
    this.filterConfig = filterConfig;
    this.encoding = filterConfig.getInitParameter("encoding");
    String value = filterConfig.getInitParameter("ignore");
    if(value == null){
    this.ignore = true;
    }else if(value.equalsIgnoreCase("true")){
    this.ignore = true;
    }else if(value.equalsIgnoreCase("yes")){
    this.ignore = true;
    }else{
    this.ignore = false;
    }
    }

    public String selectEncoding(ServletRequest request){
    return this.encoding;
    }
    }import下面的注释是在web.xml中的配置方法。
      

  4.   

    小弟不算太精通不过也做过jsp项目,遇到过你的问题,在bean中得到jsp传过来的值,有时候就把中文变成乱码,就用你写的程序就能解决,但jsp之间的乱码我没遇到过,是不是jsp中有这两个的缘故我也不知道,你加上试试,<%@ page contentType="text/html;charset=gb2312" language="java"%>
    <%request.setCharacterEncoding("GB2312");%>
      

  5.   

    <%@ page contentType="text/html;charset=gb2312"%> 你没有这句吧
    别的不要了