• 招生咨詢熱線:4008-569-579 
  • 手機(jī)版
    用手機(jī)掃描二維碼直達(dá)商品手機(jī)版
招生咨詢熱線
4008-569-579
機(jī)構(gòu)主頁 > 機(jī)構(gòu)新聞 > 如何解決網(wǎng)頁傳值過程中文亂碼問題
機(jī)構(gòu)主頁 > 機(jī)構(gòu)新聞>如何解決網(wǎng)頁傳值過程中文亂碼問題

如何解決網(wǎng)頁傳值過程中文亂碼問題

來源:北京達(dá)內(nèi)教育        時間:2023-05-26        熱度:32℃        返回列表

解決中文亂碼問題

網(wǎng)頁傳值主要有兩種情況:

1、使用form表單傳值

    使用form表單傳值主要有兩種方式:get和post(即:method="get/post",默認(rèn)是get方式)

    1)解決使用post方式傳遞中文的亂碼問題:

        方法一:

        <form action="login-handler.jsp" method="post"></form>

        在接收請求參數(shù)之前設(shè)置請求編碼即可,request.setCharacterEncoding("編碼");

        示例:

        <%

            request.setCharacterEncoding("UTF-8");

            String name = request.getParameter("name");

            String pwd = request.getParameter("pwd");

        %>

        方法二:

        可以寫一個Filter過濾該項(xiàng)目下的所有請求編碼

        web.xml文件

        <filter>

          <filter-name>charsetEncoding</filter-name>

          <filter-class>com.lym.filter.CharsetEncodingFilter</filter-class>

          <init-param>

              <param-name>code</param-name>

              <param-value>UTF-8</param-value>

          </init-param>

       </filter>

       <filter-mapping>

          <filter-name>charsetEncoding</filter-name>

          <url-pattern>/*</url-pattern>

       </filter-mapping>

       CharsetEncodingFilter.java文件

       public class CharsetEncodingFilter implements Filter {

          private static String CODE = "UTF-8";//默認(rèn)編碼

          public void destroy() {}

          public void doFilter(ServletRequest arg0, ServletResponse

arg1,FilterChain arg2) throws IOException, ServletException {

              HttpServletRequest req = (HttpServletRequest)arg0;

              HttpServletResponse resp = (HttpServletResponse)arg1;

              req.setCharacterEncoding(CODE);

              resp.setCharacterEncoding(CODE);

              arg2.doFilter(arg0, arg1);

         }

         public void init(FilterConfig arg0) throws ServletException {

             String code = arg0.getInitParameter("code");

             if(!"".equals(code) && code!= null){

                 CODE = code;

             }

        }

     }     

    2)解決使用get方式傳遞中文的亂碼問題:(兩種方法)

        <form action="login-handler.jsp" method="get"></form>

        方法一:

            對接受到的請求參數(shù)進(jìn)行轉(zhuǎn)碼,使用String name = new String(name.getBytes("默認(rèn)編碼"),"轉(zhuǎn)換后的編碼");

            <%

                String name = request.getParameter("name");

                name = new String(name.getBytes("ISO-8859-1"),"UTF-8");//將接收到的name參數(shù)的編碼轉(zhuǎn)換為UTF-8編碼

            %>

        方法二:

            客戶端用戶提交數(shù)據(jù)之前,使用JS把用戶要提交的中文值進(jìn)行編碼,然后再服務(wù)器端對接收到的值進(jìn)行解碼即可。

            客戶端編碼:

                <script>

                    function login(){

                        var loginForm = document.forms["loginForm"];

                        loginForm.name.value = encodeURI(loginForm.name.value);//對中文字符串進(jìn)行編碼

                        return true;

                    }

                </script>

                <form action="login-handler.jsp" name="loginForm" method="get"></form>

            服務(wù)端解碼:

                <%

                    String name = request.getParameter("name");

                    name = URLDecoder.decode(name, "UTF-8");

                %>

                 

        注意:使用encodeURI對字符串進(jìn)行一次編碼,再提交表單時又對字符串進(jìn)行了一次的編碼,實(shí)際進(jìn)行了兩次編碼。

               在使用URLDecoder.decode()進(jìn)行解碼時,實(shí)際上是對字符串進(jìn)行兩次解碼。

            

2、使用<a href=""></a>超鏈接的方式傳值

    使用JS對a標(biāo)簽的href屬性值進(jìn)行編碼,在對a標(biāo)簽的href屬性值編碼時,要兩次編碼才能在服務(wù)端使用URLDecoder.decode()進(jìn)行解碼。

    客戶端編碼:

        <script>

            function a(){

                var doc = document.getElementById("aa");

                doc.href = "a-handler.jsp?info="+encodeURI(encodeURI("你好"));

            }

        </script>

        <a href="" id="aa">使用超鏈接傳送中文參數(shù)</a>

    服務(wù)端解碼:

        <%

            String user = request.getParameter("info");

            user = URLDecoder.decode(user, "UTF-8");

            out.println(user);

        %>

電話咨詢

電話咨詢

咨詢電話:
4008-569-579
回到頂部

回到頂部