String   sname=new   String(userName.getBytes("ISO-8859-1"),"gb2312");
这样是可以 但是太累了 
我想写个方法 进行批量转换
该怎么做

解决方案 »

  1.   

    写1个通用的方法,所有地方调用这个方法就好。public String decodeParam(String name){
       return new  String(name.getBytes("ISO-8859-1"),"gb2312"); 
    }
      

  2.   

    是在servlet里面的执行的方法1楼写的太精简节约了 
    能不能写的简单易解点啊 多点步骤无所谓 要易解
      

  3.   

    先创建1个类比如叫:ServletUtil.java,代码如下:public class ServletUtil{
      public static String decodeParam(String name){
        return new  String(name.getBytes("ISO-8859-1"),"gb2312");
      }
    }在servlet中如果要取1个name参数的值:
    String name = ServletUtil.decodeParam(request.getParameter("name"));
      

  4.   

    干嘛要这么转啊,写个过滤器把请求和响应的编码都设为 GBK 不就得了。
      

  5.   


    package com.gzbb.item.util;import java.io.IOException;import javax.servlet.Filter;
    import javax.servlet.FilterChain;
    import javax.servlet.FilterConfig;
    import javax.servlet.ServletException;
    import javax.servlet.ServletRequest;
    import javax.servlet.ServletResponse;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;public class CharacterFilter implements Filter {

    String encoding="ISO-8859-1";
    public void destroy(){} public void doFilter(ServletRequest res, ServletResponse resp,
    FilterChain chain) throws IOException, ServletException {
    HttpServletRequest request = (HttpServletRequest)res;
    HttpServletResponse response = (HttpServletResponse)resp;
    request.setCharacterEncoding(encoding);
    response.setCharacterEncoding(encoding);
    chain.doFilter(request, response);
    } public void init(FilterConfig config) throws ServletException {

    String s = config.getInitParameter("encoding");
    if(s!=null && !s.equals(""))
    {
    this.encoding=s;
    }
    }<?xml version="1.0" encoding="UTF-8"?>
    <web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
    http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"> <filter>
    <filter-name>filter</filter-name>
    <filter-class>com.gzbb.item.util.CharacterFilter</filter-class>
    <init-param>
    <param-name>encoding</param-name>
    <param-value>utf-8</param-value>
    </init-param>
    </filter>
    <filter-mapping>
    <filter-name>filter</filter-name>
    <url-pattern>/*</url-pattern>
    </filter-mapping>
    </web-app>
    仔细看看,不过你最好呢,就在把JSP,servlet复习一下。这个很多视频教程里面都会讲的。