各位请指教:好久没用JAVA,忘了中文显示乱码问题的解决方法

解决方案 »

  1.   


    String s = new String(request.getParameter("name").getBytes("ISO-8859-1"));
      

  2.   

    写一个方法:
    public String ToChinese(String str)
    {
       String rt=null;
       if(str!=null)
       {   }
    }
      

  3.   

    写一个方法转换:
    public String ToChinese(String str)
    {
       String rt=null;
       if(str!=null)
       {
          try
          {
               rt=new String(str.getBytes("ISO-8859-1"),"gbk");
          }
         }
        return rt;
    }
      

  4.   

    import java.io.UnsupportedEncodingException;public class CharsetTool
    {    private CharsetTool() {
        }
        public static String getISOToGBK(String str)
        {
            try
            {
                return new String(str.getBytes("iso-8859-1"), "gbk");
            }
            catch (UnsupportedEncodingException ex)
            {
                return null;
            }
        }
        public static String getGBKToISO(String str)
        {
            try
            {
                return new String(str.getBytes("gbk"), "iso-8859-1");
            }
            catch (UnsupportedEncodingException ex)
            {
                return null;
            }
        }
        public static String getISOToUTF8(String str)
        {
            try
            {
                return new String(str.getBytes("iso-8859-1"), "utf-8");
            }
            catch (UnsupportedEncodingException ex)
            {
                return null;
            }
        }
        public static String getEncodingString(String str, String encoding)
        {
            String[] encodings = encoding.toLowerCase().replaceAll("\\s", "").split("\\|");
            if (encodings.length == 2)
            {
                String encodingStr = "";
                try
                {
                    encodingStr = new String(str.getBytes(encodings[0]), encodings[1]);
                }
                catch (UnsupportedEncodingException ex)
                {
                }
                finally
                {
                    return encodingStr;
                }
            }
            return str;
        }
    }
      

  5.   

    在取表单中的数据之前先request.setCharacterEncoding("gb2312");这样即使表单中提交的数据有中文也是用简体中文方式编码的.其中request为请求页面的对象.
      

  6.   

    设置过滤器过滤所有请求为中文编码在JSP界面和SERVLET的开头都把响应设置成中文编码这样还会有问题吗?
      

  7.   

    jsp页面保存到数据库有乱码解决方法Jsp+tomcat+bean中文乱码问题解决方法javabean中参数有乱码
    1) 所有的jsp页面指定字符编码方式,如:Charest=gb2312,Charest=UTF-8等等
    2) 在应用服务器中的server.xml方件中找到设置服务器端口的行,一般是这样开头:”<Connector port="8080"”,
    3) 在找到的行"<Connector"开头的字符串后加上:URIEncoding="UTF-8" ,保存文件
    --------------------------------------------------------------------------
    jsp页面有乱码解决方法    所有的jsp页面指定字符编码方式,如:Charest=gb2312,Charest=UTF-8等等
        <%@ page contentType="text/html; charset=UTF-8">
    --------------------------------------------------------------------------
    jsp单个中文参数乱码解决方法    用这个转换一下: 
        <%!String trans(String chi)
           {
            string result =null;
            byte temp[];
            temp=chi.getBytes("iso=8859-1");
            result= new String(temp);
            }
        %>
    或者直接这样:
        <% 
          request.setCharacterEncoding("UTF-8");
          out.println(request.getParameter("参数ID")
        %>
    --------------------------------------------------------------------------