encodeURIComponent

<script type="text/javascript">
  function show(){
       var f="#wer中文测试";
       f = encodeURIComponent(encodeURIComponent(f));
       test.value = f;
        var url = "servlet/Test?f="+f;
        var rtv = showModalDialog(url,"","dialogWidth:820px; dialogHeight:620px; status:0;help:0;scrolling:auto");
    }
  </script>
  <body><input id="test" value="">   <input type="button" value="TEST" onclick="show()" /> 
  </body>
package com.yourcompany.struts.action;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import com.yourcompany.struts.form.TestForm;

public class TestAction extends Action {
    public ActionForward execute(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response) throws UnsupportedEncodingException {
        TestForm testForm = (TestForm) form;
        
        response.setCharacterEncoding("UTF-8");

        String name=testForm.getName();
        String pwd=testForm.getPwd();
        
        response.setCharacterEncoding("UTF-8");
        String myname=request.getParameter("myname");
        myname=URLDecoder.decode(myname, "UTF-8");
        String mypwd=request.getParameter("mypwd");
        mypwd=URLDecoder.decode(mypwd, "UTF-8");
        
        System.out.println(name);
        System.out.println(pwd);
        System.out.println(myname);
        System.out.println(mypwd);
        
        request.setAttribute("name", name);
        request.setAttribute("pwd", pwd);
        request.setAttribute("myname", myname);
        request.setAttribute("mypwd", mypwd);
        
        return mapping.findForward("go");
    }
}

 

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。