以下为dic_left.jsp代码片断,是utf-8编码,点击查询,会把limitTest(品名)通过js提交给dic_left.jsp,limitValue接受到值后做处理。问题是:在js里alert出来的中文是好的,但是提交给dic_left.jsp的limitValue打出来的是乱码,用什么方法可以解决,前提是要用utf-8,不能用gbk。<%@ page contentType="text/html; charset=utf-8" %><script language="javascript">
function query(){
alert(window.document.formX.limitTest.value);
window.location ="dic_left.jsp?limitValue="+window.document.formX.limitTest.value;
}
</script><form name="formX">
limitValue = request.getParameter("limitValue");
System.out.println("limitValue"+limitValue);品名:<input type="test" name="limitTest" value="">
<input type="button" value="查询" class="an1" onClick="query()">
</form>

解决方案 »

  1.   

    环境:
    WindowsXP中文
    Eclipse3.2.1+Myeclipse5.1.0GA
    Tomcat5.5
    JDK1.5.0
    Hibernate3.1
    Mysql5.0+ mysql-connector-java-5.0.4-bin.jar方案:
    1.集成开发环境Eclipse中设置文本文件存储编码为UTF-8。
    //我想是因为….如果所做工程项目最终要在别的版本操作系统的服务器上跑,这里需要设置(未经证实)
    2.数据库mysql,默认编码使用utf8;
    并且创建数据库时在语句后面追加DEFAULT CHARSET=utf8;set names utf8;
    //如果数据库默认编码是utf8,那这个也不是必需的吧(未经证实)
    3.跟数据库连接的URL:Hibernate URL:
    jdbc:mysql://127.0.0.1:3306/addressbook?useUnicode=true&characterEncoding=utf8
    //如果数据库默认编码是utf8,那这个也不是必需的吧(未经证实)
    4.使用过滤器,过滤器文件代码见后面附1。
    <filter>
        <filter-name>Set Character Encoding</filter-name>
        <filter-class>org.biti.filters.SetCharacterEncodingFilter</filter-class>
        <init-param>
          <param-name>encoding</param-name>
          <param-value>UTF-8</param-value>
        </init-param>
     </filter>
     <filter-mapping>
        <filter-name>Set Character Encoding</filter-name>
        <url-pattern>/*</url-pattern>
     </filter-mapping>
    //过滤的是post还是get还没弄明白,据说只过滤器中一个,另一个见5。
    5.修改Tomcat配置文件server.xml中Connector部分
    <Connector port="80"               
    maxHttpHeaderSize="8192"
        maxThreads="150" minSpareThreads="25" maxSpareThreads="75"
        enableLookups="false" redirectPort="8443" acceptCount="100"
    connectionTimeout="20000" disableUploadTimeout="true" />
    加入URIEncoding="UTF-8"一项。
    //我现在这个没乱码的就没有设置….附1:SetCharacterEncodingFilter.java(可在Tomcat示例源码中找到)
    package org.biti.filters;
    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;public class SetCharacterEncodingFilter implements Filter {
    protected String encoding = null;
    protected FilterConfig filterConfig = null;
    protected boolean ignore = true;
    public void destroy() {
    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;
    }
    protected String selectEncoding(ServletRequest request) {
    return (this.encoding);
    }
    }
    ********************
    不知道有没有帮助
      

  2.   

    多谢楼上,过滤器我已经设置了(用的是utf-8),我用的是weblogic7。
      

  3.   

    写个转码方法
    public String newStr(String str) throws UnsupportedEncodingException{
    return new String(str.getBytes("iso8859-1"),"gb2312");
    }
    将取得的字符串处理一下就搞定了
    <form   name= "formX "> 
    limitValue   =   newStr(request.getParameter( "limitValue ")); 
    System.out.println( "limitValue "+limitValue); 
      

  4.   

    你</form>下的那个方法。。
    limitValue   =   request.getParameter( "limitValue "); 
    System.out.println( "limitValue "+limitValue); 是不是该用。<%%>括起来啊<%
    limitValue   =   request.getParameter( "limitValue "); 
    System.out.println( "limitValue "+limitValue); 
    %>这样写才对吧。
      

  5.   

    <%@   page   contentType= "text/html;   charset=utf-8 "   %>
    <%request.setCharactorEncoding("UTF-8");%>
      

  6.   

    把你收到的数据处理一下就行了<%=new String(request.getParameter("title").getBytes("ISO-8859-1"), "UTF-8")%>
      

  7.   

    get请求传 中文 字符会乱码,因为无论你设置的格式是UTF-8还是GBK或是GB2312,最后都将是ISO-8859-1的编码,所以要转码一下,将字符转为GBK或GB2312,如果你还是转UTF-8仍然会是乱码,
    所以3楼的写法是正确的,7楼的还是不行。
    String limitValue = new String(request.getParameter("limitValue").getBytes("ISO-8859-1"),"GBK");