中文亂碼問題真的是一個很棘手的問題,特別是從前臺傳到后臺之后,都不知道問題出在哪里了。現(xiàn)在分享解決javaWEB中前后臺中文亂碼問題的3種方法。
方法一:
tomcat的自帶編碼是ISO-8859-1的格式,是不兼容中文的編碼的。所以我們從后臺接收的時候要注意。
采用相同的格式去接收(ISO-8859-1),然后用能解析的編碼(utf-8)去轉(zhuǎn)換。這樣我們就能得到能兼容中文的格式了。這樣處理之后發(fā)往前臺。注意:發(fā)往前臺的時候也需要設(shè)置一下
resp.setContentType("text/html;charset=utf-8");//設(shè)置頁面的字符編碼,解決界面顯示中文亂碼的問題
1
2
3
|
protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { //因為tomcat自帶編碼是ISO-8859-1格式 |
1
2
3
4
5
6
|
//解決亂碼方法之一 <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" ); //設(shè)置頁面的字符編碼<span style="white-space:pre"> </span> |
1
2
3
4
5
6
|
<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); |
方法二:
由于方法一比較繁瑣,采用用了簡單的設(shè)置。只需要簡單的一句就可以搞定
req.setCharacterEncoding("utf-8");//必須寫在第一位,因為采用這種方式去讀取數(shù)據(jù),否則數(shù)據(jù)會出錯。
這樣就不用像之前的那樣繁瑣的設(shè)置了
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { //因為tomcat自帶編碼是ISO-8859-1格式 //解決亂碼二《法一比較繁瑣》 req.setCharacterEncoding( "utf-8" ); //必須寫在第一位,因為采用這種方式去讀取數(shù)據(jù),否 則數(shù)據(jù)會出錯。 //設(shè)置這樣方式去讀。這樣中文就能夠讀取出來了,但是需要注意。表單的發(fā)送方式必須是<span style="color:#ff0000;"> method='post'</span> resp.setContentType( "text/html;charset=utf-8" ); //設(shè)置傳過去的頁面顯示的編碼 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); |
方法三:
這是在法二的基礎(chǔ)上修改的。雖然我們能修改編碼格式去讀,但是考慮到用戶肯定不會修改,所以我們需要采用比較通用的辦法,讓用戶修改配置文件。也就是web.xml文件
需要修改web.xml里面的內(nèi)容,就是說,字符編碼從xml接收過來。需要在xml文件中配置參數(shù)。
代碼如下:
1
2
3
4
5
6
7
8
|
< 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 >//這里面的內(nèi)容可供用戶自己填寫(必須是編碼格式) </ init-param > </ servlet > |
我們知道前臺和后臺進行交換必須經(jīng)過web.xml配置
我們需要獲取web.xml的設(shè)置的參數(shù)
1
2
3
|
public void init(ServletConfig config) throws ServletException { charset=config.getInitParameter( "charset" ); //獲得初始化參數(shù)。當然charset需要設(shè)置為全局變量。后面的service函數(shù)需要設(shè)置req.setCharacterEncoding(charset); } |
1
2
3
4
5
6
7
8
|
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); |
解決的效果圖:
解決之前:
解決之后:
前臺代碼:
1
2
3
4
5
6
7
|
< body > < form action = "login" method = "post" >//login在web.xml中配置,就能實現(xiàn)到后臺去讀取數(shù)據(jù) 用戶名:< input type = "text" name = "username" />< br /> 密碼:< input type = "password" name = "pwd" />< br /> < input type = "submit" value = "登錄" /> </ form > </ body > |
以上就是為大家分享的解決javaWEB中前后臺中文亂碼問題的3種方法,特別是從前臺傳到后臺之后產(chǎn)生的中文亂碼問題,希望對大家的學習有所幫助。