幾種常見的java亂碼情況的解決方法

這篇文章給大家分享的是有關幾種常見的java亂碼情況的解決方法,小編覺得挺實用的,因此分享給大家學習,希望大家閱讀完這篇文章后可以有所收獲。

創(chuàng)新互聯(lián)專注于馬鞍山企業(yè)網站建設,響應式網站設計,成都做商城網站。馬鞍山網站建設公司,為馬鞍山等地區(qū)提供建站服務。全流程定制網站制作,專業(yè)設計,全程項目跟蹤,創(chuàng)新互聯(lián)專業(yè)和態(tài)度為您提供的服務

1、在Servlet中獲得通過get方式傳遞到服務器的數(shù)據(jù)時出現(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方法中定義一個name變量獲得封裝在服務器的客戶端數(shù)據(jù)userName,然后將name以“ISO8859-1”的方式賦值給字節(jié)數(shù)組bytes,最后將此bytes數(shù)組以UTF-8的方式賦值給一個新創(chuàng)建的String變量newName,此時newName就是能正常顯示的數(shù)據(jù),之所以這么麻煩,是因為沒有直接的辦法一行代碼解決,可以就當此方法為固定用法。

2、在Servlet中獲得通過post方式傳遞到服務器的數(shù)據(jù)時出現(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");
        //得到前臺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傳遞方式下解決亂碼問題很簡單,就req.setCharacterEncoding(“UTF-8”); 這一行代碼,但需注意,要將這句話放在獲取數(shù)據(jù)之前。

3、Servlet通過服務器將數(shù)據(jù)響應到客戶端時出現(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);
    }
}

解析:注意,以上兩種方法在應用時要寫在輸出方法之前,另外,兩種方式效果一樣,因為方式一較簡潔,常用方式一。

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

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

以上就是幾種常見的java亂碼情況的解決方法了,看完之后是否有所收獲呢?如果想了解更多相關內容,歡迎關注創(chuàng)新互聯(lián)行業(yè)資訊!

網站題目:幾種常見的java亂碼情況的解決方法
標題URL:http://bm7419.com/article32/pcgpsc.html

成都網站建設公司_創(chuàng)新互聯(lián),為您提供小程序開發(fā)、營銷型網站建設、品牌網站設計、搜索引擎優(yōu)化、網站設計、移動網站建設

廣告

聲明:本網站發(fā)布的內容(圖片、視頻和文字)以用戶投稿、用戶轉載內容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內容未經允許不得轉載,或轉載時需注明來源: 創(chuàng)新互聯(lián)

商城網站建設