自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

解決Servlet JSP頁面亂碼問題

開發(fā) 后端
本文介紹解決Servlet JSP頁面亂碼問題的方法,包括寫deal處理頁面及代碼等。

從form表單提交信息到Servlet JSP頁面進行處理的時候,提交的中文信息若不加處理的話就會顯示亂碼,如一串????,F(xiàn)在通過一個例子來進行總結如下:
寫一個用戶信息提交頁面,通過這個頁面向Servlet JSP頁面提交用戶信息,代碼如下:

  1. <%@ page language="java" contentType="text/html; charset=gbk"%> 
  2. <html> 
  3. <head> 
  4. <title>表單提交</title> 
  5. </head> 
  6. <body> 
  7. <form action="deal.jsp" method="post"> 
  8. 用戶名:<input type="text" name="username"><br> 
  9. 密&nbsp;&nbsp;碼:<input type="password" name="password"><br> 
  10. 愛&nbsp;&nbsp;好:<input type="radio" name="love" value="運動">運動&nbsp;  
  11. <input type="radio" name="love" value="音樂">音樂<br> 
  12. &nbsp;&nbsp;<input type="submit" value="提交"> 
  13. </form> 
  14. </body> 
  15. </html>  

現(xiàn)在寫deal處理頁面,代碼如下:

  1. <%@ page language="java" contentType="text/html; charset=gbk"%> 
  2. <html> 
  3. <head> 
  4. <title>顯示用戶信息</title> 
  5. </head> 
  6. <body> 
  7. <%  
  8. //request.setCharacterEncoding("gb2312");   
  9. String username = request.getParameter("username");  
  10. //String username = new String(request.getParameter("username").getBytes("iso-8859-1"),"gbk");   
  11. String password = request.getParameter("password");  
  12. //String love = new String(request.getParameter("love").getBytes("iso-8859-1"),"gbk");   
  13. String love = request.getParameter("love");  
  14. %> 
  15. <%= username %>您好,你的密碼是:<%= password %>,您的愛好是:<%= love %>!  
  16. </body> 
  17. </html>  

從前面的信息提交頁面提交來的信息包含中文,這時就會出現(xiàn)亂碼。如:

??????您好,你的密碼是:1234569,您的愛好是:????!
現(xiàn)在,把第8行的注釋符號去掉,重新執(zhí)行頁面(請確保web服務器會自動加載更改后的頁面,否則請重新啟動web服務器),這時可以看到正確的中文信息了,如:

王中玉您好,你的密碼是:9856322,您的愛好是:音樂!
也可以使用另外一種方法進行處理,把deal.jsp的第8行注釋掉,然后把第9行、第13行也注釋掉,去掉第10行和第12行的注釋符號,保存好重新執(zhí)行頁面(方法同上),同樣會顯示正常的信息。

下面通過前面的信息提交頁面向一個servlet提交信息,然后對其中的中文亂碼進行處理。寫一個servlet程序(formdeal.java),如下:

  1. package org.wzhongyu;  
  2. import java.io.IOException;  
  3. import java.io.PrintWriter;  
  4. import javax.servlet.ServletException;  
  5. import javax.servlet.http.HttpServlet;  
  6. import javax.servlet.http.HttpServletRequest;  
  7. import javax.servlet.http.HttpServletResponse;  
  8. public class formdeal extends HttpServlet {  
  9. public void destroy() {  
  10. super.destroy(); // Just puts "destroy" string in log   
  11. // Put your code here   
  12. }  
  13. public void doGet(HttpServletRequest request, HttpServletResponse response)  
  14. throws ServletException, IOException {  
  15. this.doPost(request, response);  
  16. }  
  17. public void doPost(HttpServletRequest request, HttpServletResponse response)  
  18. throws ServletException, IOException {  
  19. //response.setContentType("text/html; charset=gbk");   
  20. PrintWriter out = response.getWriter();  
  21. //request.setCharacterEncoding("gbk");   
  22. String username = request.getParameter("username");  
  23. String password = request.getParameter("password");  
  24. String love = request.getParameter("love");  
  25. out.print("您的用戶名:" + username + "<br>"); //   
  26. out.print("您的密碼:" + password + "<br>"); //   
  27. out.print("您的愛好:" + love); //   
  28. }  
  29. public void init() throws ServletException {  
  30. // Put your code here   
  31. }  

該servlet的部署描述文件(web.xml)如下:

  1. <?xml version="1.0" encoding="UTF-8"?> 
  2. <web-app version="2.5"   
  3. xmlns="http://java.sun.com/xml/ns/javaee"   
  4. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"   
  5. xsi:schemaLocation="http://java.sun.com/xml/ns/javaee   
  6. http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">   
  7. <servlet>   
  8. <description>This is the description of my J2EE component</description>   
  9. <display-name>This is the display name of my J2EE component</display-name>   
  10. <servlet-name>formdeal</servlet-name>   
  11. <servlet-class>org.wzhongyu.formdeal</servlet-class>   
  12. </servlet>   
  13. <servlet-mapping>   
  14. <servlet-name>formdeal</servlet-name>   
  15. <url-pattern>/servlet/formdeal</url-pattern> 
  16. </servlet-mapping> 
  17. </web-app> 

把信息提交頁面的第7行改為:

  1. <form action="./servlet/formdeal" method="post"> 

重新部署并執(zhí)行頁面,同樣看到顯示的中文信息是亂碼?,F(xiàn)在把第23行的注釋符去掉,重新執(zhí)行會看到下面的信息,提交過來的中文信息是亂碼: 您的用戶名:??????
您的密碼:123465
您的愛好:????把第25行的注釋符也去掉,重新執(zhí)行,可以看到可以顯示正常的信息了,如下: 您的用戶名:王中玉
您的密碼:5632215
您的愛好:音樂如果只去掉第25行的注釋,執(zhí)行程序則會顯示下面的信息: ?????????
?????123456 ???????

由此可見,這個兩個都不可以忽略掉,也可以從下面的方式驗證必須寫上兩個,把formdeal.java里的第29,30,31行的中文換成英文,同樣注釋掉第23行,而不要注釋掉第25行,執(zhí)行后顯示的信息如下:

  1. username???  
  2. password65462458  
  3. love??  

這是由于沒有設置servlet響應的頁面的字符編碼造成的。
在servlet里也可以這樣進行處理,把第25行注釋掉,而不要注釋第23行,把第26行和第28行分別改為如下代碼:

  1. String username = new String(request.getParameter("username").getBytes("iso-8859-1"),"gbk");  
  2. String love = new String(request.getParameter("love").getBytes("iso-8859-1"),"gbk"); 

以上是Servlet JSP頁面亂碼修改方法,這樣也可以正常顯示中文信息。

【編輯推薦】

  1. Servlet引擎的安裝
  2. 配置Servlet開發(fā)環(huán)境
  3. 標簽庫中JSP Servlet調(diào)用
  4. 學習Java Servlet時遇到的小問題
  5. Servlet在session中共享鏈接
責任編輯:佚名 來源: IT168
相關推薦

2009-07-07 18:20:54

JSP頁面顯示亂碼

2009-06-30 15:22:55

JSP頁面

2012-06-19 14:35:24

JSPJava亂碼

2009-07-01 18:14:36

JSP亂碼

2009-07-02 13:26:32

JSP中文亂碼

2009-07-06 17:50:13

Java JSP

2009-07-06 09:49:26

Servlet JSP

2009-07-01 18:05:54

JSP中文亂碼

2009-06-30 13:49:21

excel文檔Jsp

2009-07-07 13:29:33

Servlet和JSP

2010-08-06 10:49:16

FlexJsp

2012-06-29 13:31:56

ServletJSPJava

2009-07-03 14:02:51

2009-07-01 17:34:03

Servlet和JSP

2009-08-10 09:19:28

Servlet JSP

2009-06-30 17:26:56

JSP頁面

2010-02-06 16:13:49

Ubuntu Auda

2009-07-08 16:10:36

Servlet和JSPJSP頁面

2012-03-21 10:16:31

JavaJSP

2011-05-17 15:03:34

JSP
點贊
收藏

51CTO技術棧公眾號