package com.digfa;public class Util
{
public static String codeString(String s)
{ String str=s;
try{byte b[]=str.getBytes("ISO-8859-1");
str=new String(b);
return str;
}
catch(Exception e)
{ return str;
}
}
}

解决方案 »

  1.   

    request.setCharacterEncoding("gb2312");
      

  2.   

    bsh614(牙膏的末日) :的方法如果传入中文那么会编程乱码
    lijunjiejava(不眠之夜):我这个函数要在formbean中调用的,好像不能在formbean中request.setCharacterEncoding("gb2312")吧
      

  3.   

    你用的是tomcat吗?如果是,请在你的web工程中的/WEB-INF/web.xml中加入下面部分。
       <filter>     
        <filter-name>Set Character Encoding</filter-name>
        <filter-class>filters.SetCharacterEncodingFilter</filter-class>
        <init-param>
          <param-name>encoding</param-name>
          <param-value>SHIFT_JIS</param-value>
        </init-param>
       </filter>
      
        <filter-mapping>
         <filter-name>Set Character Encoding</filter-name>
         <url-pattern>/*</url-pattern>
       </filter-mapping>注意:要加在<listener>前面、<context-param>后面。
      

  4.   

    这好像是个难题yeah,看来我回去得好好思考
    .对了,在jsp中直接设置传入得参数为汉字怎么设置
    比如<form name="replyform" method="post" action="replyAction.do?title=手机测试">
    如何设置可以是的title传入后就是汉字,不需要进行转化
      

  5.   

    jerrykey(钥匙) :
    我用的试tomcat,我按照你得方法做了,但是:(PS :我目前在Jbuilder9下面调试)
    Exception starting filter Set Character Encoding: java.lang.ClassNotFoundException: filters.SetCharacterEncodingFilter
      

  6.   

    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;
        }
      }
      

  7.   

    package filter;import javax.servlet.*;
    import java.io.*;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;
    }
    }有一点要注意,我那个SHIFT_JIS是日文编码。
    你要是用中文改成GB2312或者ISO8859_1就行了