久久久久久久视色,久久电影免费精品,中文亚洲欧美乱码在线观看,在线免费播放AV片

<center id="vfaef"><input id="vfaef"><table id="vfaef"></table></input></center>

    <p id="vfaef"><kbd id="vfaef"></kbd></p>

    
    
    <pre id="vfaef"><u id="vfaef"></u></pre>

      <thead id="vfaef"><input id="vfaef"></input></thead>

    1. 站長(zhǎng)資訊網(wǎng)
      最全最豐富的資訊網(wǎng)站

      java幾種亂碼問題解決方法

      java幾種亂碼問題解決方法

      幾種java亂碼情況解決方法:

      1、在Servlet中獲得通過get方式傳遞到服務(wù)器的數(shù)據(jù)時(shí)出現(xiàn)亂碼;

       public class RegistServlet extends HttpServlet{     @Override     protected void doGet(HttpServletRequest req, HttpServletResponse resp)             throws ServletException, IOException {         String name = req.getParameter("userName");         byte[] bytes = name.getBytes("ISO8859-1");         String newName = new String(bytes,"UTF-8");     }      @Override     protected void doPost(HttpServletRequest req, HttpServletResponse resp)             throws ServletException, IOException {         doGet(req, resp);     } }

      解析:在doGet方法中定義一個(gè)name變量獲得封裝在服務(wù)器的客戶端數(shù)據(jù)userName,然后將name以“ISO8859-1”的方式賦值給字節(jié)數(shù)組bytes,最后將此bytes數(shù)組以UTF-8的方式賦值給一個(gè)新創(chuàng)建的String變量newName,此時(shí)newName就是能正常顯示的數(shù)據(jù),之所以這么麻煩,是因?yàn)闆]有直接的辦法一行代碼解決,可以就當(dāng)此方法為固定用法。

      2、在Servlet中獲得通過post方式傳遞到服務(wù)器的數(shù)據(jù)時(shí)出現(xiàn)亂碼;

      public class RegistServlet extends HttpServlet{     @Override     protected void doGet(HttpServletRequest req, HttpServletResponse resp)             throws ServletException, IOException {         //注意:post方式提交數(shù)據(jù)的亂碼解決方式,放在getParameXXX方法之前         req.setCharacterEncoding("UTF-8");         //得到前臺(tái)input框中name="username"和password="password"的value值         String username = req.getParameter("username");         String password = req.getParameter("password");     }      @Override     protected void doPost(HttpServletRequest req, HttpServletResponse resp)             throws ServletException, IOException {         doGet(req, resp);     } }

      解析:post傳遞方式下解決亂碼問題很簡(jiǎn)單,就req.setCharacterEncoding(“UTF-8”); 這一行代碼,但需注意,要將這句話放在獲取數(shù)據(jù)之前。

      3、Servlet通過服務(wù)器將數(shù)據(jù)響應(yīng)到客戶端時(shí)出現(xiàn)亂碼;

      public class RegistServlet extends HttpServlet{     @Override     protected void doGet(HttpServletRequest req, HttpServletResponse resp)             throws ServletException, IOException {         //方式一:         resp.setContentType("text/html;charset=utf-8");         //方式二:         resp.setHeader("Content-type", "text/html");         resp.setCharacterEncoding("UTF-8");     }      @Override     protected void doPost(HttpServletRequest req, HttpServletResponse resp)             throws ServletException, IOException {         doGet(req, resp);     } }

      解析:注意,以上兩種方法在應(yīng)用時(shí)要寫在輸出方法之前,另外,兩種方式效果一樣,因?yàn)榉绞揭惠^簡(jiǎn)潔,常用方式一。

      4、HTML或JSP頁面在客戶端展示時(shí)出現(xiàn)的亂碼情況。

      <head>         <meta charset="UTF-8">         <meta http-equiv="Content-Type" content="text/html;charset=utf-8">         <title>form表單</title> </head>

      贊(0)
      分享到: 更多 (0)
      網(wǎng)站地圖   滬ICP備18035694號(hào)-2    滬公網(wǎng)安備31011702889846號(hào)