既然统一用UTF-8
直接使用:
request.setCharacterEncoding("UTF-8");
就好了啊。

解决方案 »

  1.   

    第一种可通过 URLEncoder.decode("%4E%6A%84") 方法解码,它是一种 URL专用的编码方式。至于后一个问题,是因为你用UTF-8构造了字符串,事实上并不是所有的编码都可以构造出字符串的,UTF-8的内容一般以byte[]或Stream流方式存在,强行构造成字符串会使其精度变化,而导致无法还原成原来的字符串
      

  2.   

    to:tigeryu(吴越小虎)能否给个例子?
      

  3.   

    up up up..............
    这里没高手了吗?还是不肯告诉我啊?分不够我可以加
      

  4.   

    如果网站准备用UTF-8,可以按下面的方法进行
    1.web.xml中配置一个filter
    <filter>
    <filter-name>SetCharacterEncodingFilter</filter-name>
    <display-name>Set CharacterEncoding Filter</display-name>
    <filter-class>com.oceansoft.szcx.common.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>
    <filter-mapping>
    <filter-name>SetCharacterEncodingFilter</filter-name>
    <servlet-name>action</servlet-name>
    </filter-mapping>
    过滤器写法
    /*
     * SetCharacterEncodingFilter.java
     * 
     * Created on 2003-9-12
     *
     */
    package com.oceansoft.szcx.common;
    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;/**
     * @author Realfish
     * 2003-9-12
     */
    public class SetCharacterEncodingFilter implements Filter {
    // ----------------------------------------------------- Instance Variables
    /**
     * The default character encoding to set for requests that pass through
     * this filter.
     */
    protected String encoding = null;
    /**
     * The filter configuration object we are associated with.  If this value
     * is null, this filter instance is not currently configured.
     */
    protected FilterConfig filterConfig = null;
    /**
     * Should a character encoding specified by the client be ignored?
     */
    protected boolean ignore = true;
    // --------------------------------------------------------- Public Methods
    /**
     * Take this filter out of service.
     */
    public void destroy() { this.encoding = null;
    this.filterConfig = null; }
    /**
     * Select and set (if specified) the character encoding to be used to
     * interpret request parameters for this request.
     *
     * @param request The servlet request we are processing
     * @param result The servlet response we are creating
     * @param chain The filter chain we are processing
     *
     * @exception IOException if an input/output error occurs
     * @exception ServletException if a servlet error occurs
     */
    public void doFilter(ServletRequest request, ServletResponse response,
     FilterChain chain)
    throws IOException, ServletException { // Conditionally select and set the character encoding to be used
    if (ignore || (request.getCharacterEncoding() == null)) {
    String encoding = selectEncoding(request);
    if (encoding != null)
    request.setCharacterEncoding(encoding);
    } // Pass control on to the next filter
    chain.doFilter(request, response); }
    /**
     * Place this filter into service.
     *
     * @param filterConfig The filter configuration object
     */
    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; }
    // ------------------------------------------------------ Protected Methods
    /**
     * Select an appropriate character encoding to be used, based on the
     * characteristics of the current request and/or filter initialization
     * parameters.  If no character encoding should be set, return
     * <code>null</code>.
     * <p>
     * The default implementation unconditionally returns the value configured
     * by the <strong>encoding</strong> initialization parameter for this
     * filter.
     *
     * @param request The servlet request we are processing
     */
    protected String selectEncoding(ServletRequest request) { return (this.encoding); }
    }2.页面上写上
    <%@ page contentType="text/html;charset=UTF-8" language="java" %>3.数据库编码UTF-8就可以了。问题:
    1.所有post的提交有效,get的可能有时会不行。
    2.我都是基于struts的。其余的应该也可以。
      

  5.   

    也就是说get如果是中文的话有时候会乱码?其实我只是想实现google那样的效果,不知道怎么实现
      

  6.   

    我也想了很久了,我写了个解析%4E%6A%84回中文的方法
    public static String toGbkString(String s) { StringBuffer sb = new StringBuffer();
    if (s != null) {
    byte[] b = new byte[3];
    int temp;
    int index;
    try {
    while (s.length() != 0) {
    index = s.indexOf("%");
    if (index == -1)
    return sb.append(s).toString();
    if (index != 0) {
    sb.append(s.substring(0, index));
    }
    if (index + 9 <= s.length()) {
    temp =
    Integer.parseInt(
    s.substring(index + 1, index + 3),
    16)
    - 256;
    b[0] = Byte.parseByte(String.valueOf(temp));
    temp =
    Integer.parseInt(
    s.substring(index + 4, index + 6),
    16)
    - 256;
    b[1] = Byte.parseByte(String.valueOf(temp));
    temp =
    Integer.parseInt(
    s.substring(index + 7, index + 9),
    16)
    - 256;
    b[2] = Byte.parseByte(String.valueOf(temp)); sb.append(new String(b, "utf-8"));
    index += 9;
    s = s.substring(index);
    } else {
    System.out.println("数据格式错误");
    return null;
    }
    }
    } catch (UnsupportedEncodingException ue) {
    System.out.print(
    "can't convert to gb2312 : " + ue.getMessage());
    } catch (NumberFormatException ne) {
    System.out.print(
    "can't convert to gb2312 : " + ne.getMessage());
    }
    }
    return sb.toString();
    }
    但是我发现没地方用这个方法啊,比方说一个连接 test.jsp?name=%4E%6A%84  当我在test.jsp里面执行request.getParameters(name)竟然取得的不是%4E%6A%84,而是中文乱码
      

  7.   

    最突出的莫过于中文的问题了。这是最典型的。
    对于其难点搞了好长时间,现在基本都能解决了,主要表现在表单页面的提交与数据库中的数据字节转换的问题。可以通过一些相关的setContentType进行设置、用setCharacterEncoding对数据进行统一编码转换、和用getBytes对字符进行转换都可以。
      

  8.   

    很简单,用URLEncoderjava.net.URLEncoder.encode("你要的内容","你的编码")String aa = "你要的内容";test.jsp?name=<%=java.net.URLEncoder.encode(aa,"gb2312")%>
      

  9.   

    楼上说很简单的。。请给出具体代码,我2张页面A,B,A传中文参数到B,B显示中文参数,并且B的地址栏的中文是%4E%6A%84形式的谁给出代码我分数全部给他~~觉得不够我再加~
      

  10.   

    /Test/index.jsp:<%@ page contentType="text/html; charset=gb2312" %>
    <html>
    <head>
    <title>Welcome</title>
    </head>
    <body>
    <%

    if(request.getParameter("name")!=null) {
    String s = new String(request.getParameter("name").getBytes("iso-8859-1"));
    out.println(s);
    }%>
    <form name="form1" method="post" action="/Test/test">
    <input name="name" type="text">
    <input name="submit" type="submit" value="提交">
    </form>
    </body>
    </html>Servlet:
    package servlet;import java.io.IOException;import javax.servlet.ServletException;
    import javax.servlet.http.HttpServlet;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    import com.Convert;
    /**
     * @author WuBin
     *
     * 
     * 
     */
    public class Test extends HttpServlet { protected void doGet(
    HttpServletRequest request,
    HttpServletResponse response)
    throws ServletException, IOException {


    String s = new String(request.getParameter("name").getBytes("iso-8859-1"));
    System.out.print(s);
    response.sendRedirect("/Test/index.jsp?name=" + java.net.URLEncoder.encode(s , "gb2312"));
    }
    protected void doPost(
    HttpServletRequest request,
    HttpServletResponse response)
    throws ServletException, IOException {
    doGet(request,response);
    }
    }
      

  11.   

    : angelheart() ( ) 信誉:100 
    你的这种方法确实是可以解一些乱码,但是碰到原来就是%这种特殊情况还是会出错。
      

  12.   

    如果你直接在 URL 后面加中文参数,用 request.getParameter("abc") 得到的是 ISO-8859-1 编码的字符串,把它转成你想要的字符串编码就成了。
    String abc = request.getParameter("abc");
    abc = new String(abc.getBytes("gbk"), "iso-8859-1");
    其中的 gbk 换成 utf-8 也可以。
    如果以上的转换不成功,把 gbk 和 iso-8859-1 换一下位置再试试。
      

  13.   

    我就是想url里面不出现中文而以%%%的形式,呵呵
    angelheart()的方法能行~~~我马上就结贴
      

  14.   

    to:ManFirst(春梦空陶醉,醒来梦已碎.) 
    基本上不会出现你说的情况,因为只对中文处理,就算中文里混标点符号也没关系的