日韩无码专区无码一级三级片|91人人爱网站中日韩无码电影|厨房大战丰满熟妇|AV高清无码在线免费观看|另类AV日韩少妇熟女|中文日本大黄一级黄色片|色情在线视频免费|亚洲成人特黄a片|黄片wwwav色图欧美|欧亚乱色一区二区三区

RELATEED CONSULTING
相關咨詢
選擇下列產(chǎn)品馬上在線溝通
服務時間:8:30-17:00
你可能遇到了下面的問題
關閉右側(cè)工具欄

新聞中心

這里有您想知道的互聯(lián)網(wǎng)營銷解決方案
解決Servlet JSP頁面亂碼問題

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

成都創(chuàng)新互聯(lián)公司專注于企業(yè)營銷型網(wǎng)站、網(wǎng)站重做改版、虞城網(wǎng)站定制設計、自適應品牌網(wǎng)站建設、H5網(wǎng)站設計商城網(wǎng)站開發(fā)、集團公司官網(wǎng)建設、外貿(mào)網(wǎng)站建設、高端網(wǎng)站制作、響應式網(wǎng)頁設計等建站業(yè)務,價格優(yōu)惠性價比高,為虞城等各大城市提供網(wǎng)站開發(fā)制作服務。

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

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

 
 
 
  1. <%@ page language="java" contentType="text/html; charset=gbk"%> 
  2.  
  3.  
  4. </strong>顯示用戶信息<strong> 
  5.  
  6.  
  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.  
  17.   

從前面的信息提交頁面提交來的信息包含中文,這時就會出現(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 + "
    "); //   
  26. out.print("您的密碼:" + password + "
    "); //   
  27. out.print("您的愛好:" + love); //   
  28. }  
  29. public void init() throws ServletException {  
  30. // Put your code here   
  31. }  

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

 
 
 
  1.  version="1.0" encoding="UTF-8"?> 
  2.  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.    
  8. This is the description of my J2EE component   
  9. This is the display name of my J2EE component   
  10. formdeal   
  11. org.wzhongyu.formdeal   
  12.    
  13.    
  14. formdeal   
  15. /servlet/formdeal 
  16.  
  17.  

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

 
 
 
  1.  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中共享鏈接

本文標題:解決Servlet JSP頁面亂碼問題
瀏覽地址:http://www.5511xx.com/article/dhjipci.html