這篇文章給大家分享的是有關(guān)幾種常見(jiàn)的java亂碼情況的解決方法,小編覺(jué)得挺實(shí)用的,因此分享給大家學(xué)習(xí),希望大家閱讀完這篇文章后可以有所收獲。
1、在Servlet中獲得通過(guò)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)闆](méi)有直接的辦法一行代碼解決,可以就當(dāng)此方法為固定用法。
2、在Servlet中獲得通過(guò)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傳遞方式下解決亂碼問(wèn)題很簡(jiǎn)單,就req.setCharacterEncoding(“UTF-8”); 這一行代碼,但需注意,要將這句話放在獲取數(shù)據(jù)之前。
3、Servlet通過(guò)服務(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í)要寫(xiě)在輸出方法之前,另外,兩種方式效果一樣,因?yàn)榉绞揭惠^簡(jiǎn)潔,常用方式一。
4、HTML或JSP頁(yè)面在客戶端展示時(shí)出現(xiàn)的亂碼情況。
<head> <meta charset="UTF-8"> <meta http-equiv="Content-Type" content="text/html;charset=utf-8"> <title>form表單</title> </head>
以上就是幾種常見(jiàn)的java亂碼情況的解決方法了,看完之后是否有所收獲呢?如果想了解更多相關(guān)內(nèi)容,歡迎關(guān)注創(chuàng)新互聯(lián)成都網(wǎng)站設(shè)計(jì)公司行業(yè)資訊!
另外有需要云服務(wù)器可以了解下創(chuàng)新互聯(lián)scvps.cn,海內(nèi)外云服務(wù)器15元起步,三天無(wú)理由+7*72小時(shí)售后在線,公司持有idc許可證,提供“云服務(wù)器、裸金屬服務(wù)器、高防服務(wù)器、香港服務(wù)器、美國(guó)服務(wù)器、虛擬主機(jī)、免備案服務(wù)器”等云主機(jī)租用服務(wù)以及企業(yè)上云的綜合解決方案,具有“安全穩(wěn)定、簡(jiǎn)單易用、服務(wù)可用性高、性價(jià)比高”等特點(diǎn)與優(yōu)勢(shì),專為企業(yè)上云打造定制,能夠滿足用戶豐富、多元化的應(yīng)用場(chǎng)景需求。
當(dāng)前標(biāo)題:幾種常見(jiàn)的java亂碼情況的解決方法-創(chuàng)新互聯(lián)
文章地址:http://aaarwkj.com/article36/idesg.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供營(yíng)銷型網(wǎng)站建設(shè)、商城網(wǎng)站、小程序開(kāi)發(fā)、自適應(yīng)網(wǎng)站、網(wǎng)站維護(hù)、服務(wù)器托管
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容