中文亂碼問題真的是一個很棘手的問題,特別是從前台傳到後台之後,都不知道問題出在哪裡了。現在分享解決javaWEB中前後台中文亂碼問題的3種方法。
方法一:
tomcat的自帶編碼是ISO-8859-1的格式,是不兼容中文的編碼的。所以我們從後台接收的時候要注意。
採用相同的格式去接收(ISO-8859-1),然後用能解析的編碼(utf-8)去轉換。這樣我們就能得到能兼容中文的格式了。這樣處理之後發往前台。注意:發往前台的時候也需要設置一下
resp.setContentType("text/html;charset=utf-8");//設置頁面的字符編碼,解決界面顯示中文亂碼的問題
protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { //因為tomcat自帶編碼是ISO-8859-1格式
//解決亂碼方法之一<span style="white-space:pre"> </span>String name=req.getParameter("username"); <span style="white-space:pre"> </span >String pwd=req.getParameter("pwd"); <span style="white-space:pre"> </span>byte[] b=name.getBytes("ISO-8859-1");//用tomcat的格式(iso-8859-1)方式去讀。 <span style="white-space:pre"> </span>String str=new String(b,"utf-8");//採用utf-8去接string <span style="white-space:pre "> </span>resp.setContentType("text/html;charset=utf-8");//設置頁面的字符編碼<span style="white-space:pre"> </span>
<span style="white-space:pre"> </span>PrintWriter pw =resp.getWriter(); <span style="white-space:pre"> </span>String str1="<html><body ><font size='5px' color='red'>username:"+name+"pwd:"+pwd+"</font></body></html>"; <span style="white-space:pre "> </span>pw.print(str1); PrintWriter pw =resp.getWriter(); String str1="<html><body><font size='5px' color='red'>username:"+name+ "pwd:"+pwd+"</font></body></html>"; pw.print(str1);
方法二:
由於方法一比較繁瑣,採用用了簡單的設置。只需要簡單的一句就可以搞定
req.setCharacterEncoding("utf-8");//必須寫在第一位,因為採用這種方式去讀取數據,否則數據會出錯。
這樣就不用像之前的那樣繁瑣的設置了
protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { //因為tomcat自帶編碼是ISO-8859-1格式//解決亂碼二《法一比較繁瑣》 req.setCharacterEncoding("utf-8") ;//必須寫在第一位,因為採用這種方式去讀取數據,否則數據會出錯。 //設置這樣方式去讀。這樣中文就能夠讀取出來了,但是需要注意。表單的發送方式必須是<span style="color:#ff0000;"> method='post'</span> resp.setContentType("text/html;charset=utf-8");//設置傳過去的頁面顯示的編碼String name=req.getParameter("username"); String pwd=req.getParameter("pwd"); PrintWriter pw =resp.getWriter(); String str1="<html><body><font size ='5px' color='red'>username:"+name+"pwd:"+pwd+"</font></body></html>"; pw.print(str1);
方法三:
這是在法二的基礎上修改的。雖然我們能修改編碼格式去讀,但是考慮到用戶肯定不會修改,所以我們需要採用比較通用的辦法,讓用戶修改配置文件。也就是web.xml文件需要修改web.xml裡面的內容,就是說,字符編碼從xml接收過來。需要在xml文件中配置參數。
代碼如下:
<servlet> <servlet-name>Encodeing</servlet-name> <servlet-class>cn.hncu.com.encode.Encodeing</servlet-class> <init-param> <param-name>charset</param- name> <param-value>utf-8</param-value>//這裡面的內容可供用戶自己填寫(必須是編碼格式) </init-param> </servlet>
我們知道前台和後台進行交換必須經過web.xml配置我們需要獲取web.xml的設置的參數
public void init(ServletConfig config) throws ServletException { charset=config.getInitParameter("charset");//獲得初始化參數。當然charset需要設置為全局變量。後面的service函數需要設置req.setCharacterEncoding(charset); }
req.setCharacterEncoding(charset); resp.setContentType("text/html;charset=utf-8"); String name=req.getParameter("username"); String pwd=req.getParameter("pwd"); PrintWriter pw =resp.getWriter(); String str1="<html><body><font size='5px' color='red'>username:"+name+"pwd:"+pwd+"</font></body> </html>"; pw.print(str1);
解決的效果圖:
解決之前:
解決之後:
前台代碼:
<body> <form action="login" method="post">//login在web.xml中配置,就能實現到後台去讀取數據用戶名:<input type="text" name="username" /><br/> 密碼:<input type="password" name="pwd"/><br/> <input type="submit" value="登錄"/> </form> </body>
以上就是為大家分享的解決javaWEB中前後台中文亂碼問題的3種方法,特別是從前台傳到後台之後產生的中文亂碼問題,希望對大家的學習有所幫助。