前段时间我在用window.open传递参数时老出现乱码,我整体都用的UTF-8编码   
 <td nowrap><a href="#" onclick="openScript('AudioShiTing.jsp?AudioPath='+encodeURIComponent('<%=str_4%>'),'pur',400,300)">试听</a></td>
然后一位高人说要用encodeURIComponent于是就用上面的方法解决了,现在用普通的页面传递时<a href="ImageShow.jsp?image_path=<%=imagePath%>" target="_blank";><img src=<%=imagePathsmall%> alt=""/>这里面的imagePath可能为中文字符串,有的字符能够解析,譬如说咖啡,但是如果咖啡后面再加个豆,咖啡能解析但是豆是乱码,所以我还想采用encodeURIComponent的方法,但是<a href="ImageShow.jsp?image_path=<%=imagePath%>" target="_blank";><img src=<%=imagePathsmall%> alt=""/>不知道怎么改了,关键是加上encodeURIComponent就不知道怎么写了,希望大家能够帮帮我,小弟在此先谢了

解决方案 »

  1.   

    加个过滤器,统一的转一下。tomcat下有个例子,可以直接拷过来用。
      

  2.   

    用一个fitler过滤一下就可以了
      在web.xml中配置
         <!-- codefilter -->
      <filter>
        <filter-name>EncodingFilter</filter-name>
        <filter-class>org.apache.commons.filters.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>false</param-value>
        </init-param>
      </filter>
      <filter-mapping>
        <filter-name>EncodingFilter</filter-name>
        <url-pattern>/*</url-pattern>
      </filter-mapping>
      <!--  end codefilter --> 引入 commons-filters.jar
      

  3.   

    那种过滤器更不行,我刚开始在测试<td nowrap> <a href="#" onclick="openScript('AudioShiTing.jsp?AudioPath='+encodeURIComponent(' <%=str_4%>'),'pur',400,300)">试听 </a> </td>代码的时候,没有加encodeURIComponent,至少用了在网上下的两种过滤器都不起作用,最后加了encodeURIComponent才行的啊
      

  4.   

    这个如果我没猜错的话,imagePath的值是奇数的话,那边接收的就是乱码,偶数的就能转换,你还不如写一个过滤器把所有的编码都改成gbk,包括页面中编码。
      

  5.   

    也不是了,你看这两个传递过来的变量获得之后打印出来是这样:第一个那个豆显示不出来:image_path1:upload\image\咖啡??4.jpg  第二个image_path:\\192.168.74.146\upload\image\老虎1.jpg能显示出来,好像有的它无法转换过来有的能转化过来,这该怎么解决,你说的用gbk我这utf-8都是最开始从gb2312换成utf-8编码的,如果用gb2312 <img src= <%=imagePathsmall%> alt=""/>这里面如果imagePathsmall有中文的话图片就显示不出来,还要保证window.open传递过去的中文也不能有乱码,所以估计gb2312更不行,现在问题就是仿照<a href="#" onclick="openScript('AudioShiTing.jsp?AudioPath='+encodeURIComponent(' <%=str_4%>'),'pur',400,300)">里面的encodeURIComponent的写法把<a href="ImageShow.jsp?image_path= <%=imagePath%>" target="_blank";> <img src= <%=imagePathsmall%> alt=""/>也换成带encodeURIComponent的写法该怎么写,大家帮忙测试一下,分不够了我一会再加,我头都大了
      

  6.   

    你的想法能实现。JS操作JSP页面。
    $(document).ready(function(){    
        var abc= encodeURIComponent(中文路径)
        $("img").attr({ 
              src: abc",
              title: "jQuery",
              alt: "jQuery Logo"
         });
    }

    延缓了你的页面速度,不如写组件。
      

  7.   

    如果你认为写组件不可行就:<%@ page language="java" contentType="text/html; charset=UTF-8"
        pageEncoding="UTF-8"%>
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">加上过滤器基本就能解决显示问题。文件一概UTF-8处理。
      

  8.   

    自己可以写个转换的插件    这样当你在进入struct_config 填充formbean的时候就已经帮你转化好了 绝对不会出现乱码
      

  9.   

    加个过滤器.
    在tomcat,conf文件里
    server.xml里Connector标签下加上URIEncoding="UTF-8"也可以试试...
      

  10.   

    你这种方法倒是挺简单的,但是那个commons-filters.jar貌似没有啊,我去http://commons.apache.org/网站上都找不到
      

  11.   


    这个我目前做了的:在conf文件夹下修改server.xml的<Connector port="8080" protocol="HTTP/1.1"  maxThreads="150" connectionTimeout="20000"               redirectPort="8443" URIEncoding="utf-8">,在每个接受和发送页面都有<%@ page contentType="text/html; charset=utf-8" pageEncoding="utf-8"%>并且还有 request.setCharacterEncoding("utf-8"); response.setCharacterEncoding("utf-8");还是解决不了问题的,我想要做的是保证<a href="ImageShow.jsp?image_path= <%=imagePath%>" target="_blank";> <img src= <%=imagePathsmall%> alt=""/>传递参数image_path没乱码imagePathsmall图片显示没乱码,还有保证window.open参数传递也没乱码,就这几个要求,后两个已经通过上面方法实现了,现在就剩下第一个解决不了
      

  12.   

    过滤器我是在网上下的package myAddrBook;
    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.UnavailableException;/**
    * @author Administrator
    *
    * TODO 要更改此生成的类型注释的模板,请转至
    * 窗口 - 首选项 - Java - 代码样式 - 代码模板
    */
    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 {   // 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);}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);
    }}
    //配置web.xml<!--   Set Character Encoding-->
        
         <filter>
       <filter-name>Set Character Encoding</filter-name>
       <filter-class>myAddrBook.SetCharacterEncodingFilter</filter-class>
       <init-param>
        <param-name>encoding</param-name>
        <param-value>GBK</param-value>
       </init-param>
    </filter><filter-mapping>
       <filter-name>Set Character Encoding</filter-name>
       <url-pattern>/*</url-pattern>
    </filter-mapping>     <!--    Set Character Encoding-->
    加上这个貌似我以前的参数传递都会出问题,也不知道该怎么解决了,大家帮忙看看
      

  13.   

    <a href="ImageShow.jsp?image_path=<%=java.net.URLEncoder.encode(imagePath, "UTF-8")%>" target="_blank">xxxx</a>