用js如何将url带的特殊字符传到后台???急!急!急!在线等!请高手指教!

解决方案 »

  1.   

    window.location   =   /dosomething.jsp?par=abc&abc  
      这段代码假设是在index.jsp这个页面里面的,在输出这段话的时候就要做编码处理了(即在index.jsp里面处理)。如下  
      <%  
      String   urlNotEncoded="/dosomething.jsp?par=abc&abc";  
      String   urlEncoded=java.net.URLEncoder.encode(urlNotEncoded);  
      out.println("window.location="+urlEncoded);  
      %>   
      

  2.   

    加入我在script中写了个方法,如下:
    function fun(){
    window.location = "a.action?title='我我我'";
    }
    title属性在后台得到的是乱码 如何才能在前台就给它做处理从而在后台能得到“我我我”。
    急急急!!!
      

  3.   

    我知道 因为用form表单提交的话就行 容器用的是url编码,但用js带参数提交就不行 !有啥方法吗?
      

  4.   

    给你你个例子。
    前台 JS:userName = encodeURIComponent(userName);
    var url = "xxxAction.do?userName=" + userName;
    后台:
        1、Action 代码public ActionForward excute(ActionMapping mapping,
    ActionForm form, HttpServletRequest request,
    HttpServletResponse response) throws IOException {
    response.setCharacterEncoding("UTF-8"); String userName = request.getParameter("userName"); // 转换编码
    userName = EncodeUtil.changeCharacterEncode(userName, "ISO8859-1",
    "utf-8"); PrintWriter pw = response.getWriter(); for (String name : nameArray) {
    if (name.equalsIgnoreCase(userName)) {
    pw.write("用户名已注册!");
    return null;
    }
    } pw.write("用户名可用!"); return null;
    }    2、EncodeUtil(转码类)import java.io.UnsupportedEncodingException;public class EncodeUtil { /**
     * 转换编码
     * 
     * @param srcString
     *            待转码的字符串(源字符串)
     * @param srcEncode
     *            源字符串的编码
     * @param targetEncode
     *            目标编码
     * @param defaultValue
     *            如果待转码字符串为 null 则返回的默认值
     * 
     * @return 编码后的字符串
     * 
     */
    public static String changeCharacterEncode(String srcString,
    String srcEncode, String targetEncode, String defaultValue) {
    if (srcString == null) {
    return defaultValue;
    } try {
    return new String(srcString.getBytes(srcEncode), targetEncode);
    } catch (UnsupportedEncodingException e) {
    throw new EncodeException(e);
    }
    } /**
     * 转换编码
     * 
     * @param srcString
     *            待转码的字符串(源字符串)
     * @param srcEncode
     *            源字符串的编码
     * @param targetEncode
     *            目标编码
     * 
     * @return 编码后的字符串
     * 
     */
    public static String changeCharacterEncode(String srcString,
    String srcEncode, String targetEncode) {
    return changeCharacterEncode(srcString, srcEncode, targetEncode, "");
    }
    }
    试试。
      

  5.   

    可以在传入的参数加上encodeURI()转义,再在函数中做一些decodeURI()反转义。
      

  6.   

    function fun(){ 
    window.location.href = "a.action?title="+encodeURIComponent('我我我'); 
      

  7.   

    很有可能是你在用tomcat服务器的时候,没有把服务器的URIEncoding=“UTF-8”加到端口那个位置
      

  8.   

    在server.xml中配置容器编码方式,可以过滤所有get提交的正文乱码,URL后面挂参数是使用get提交的
    如下: <Connector port="8088" protocol="HTTP/1.1" 
                   connectionTimeout="20000" 
                   redirectPort="8443"  URIEncoding=“UTF-8”    />