解決Servlet JSP頁面亂碼問題
從form表單提交信息到Servlet JSP頁面進行處理的時候,提交的中文信息若不加處理的話就會顯示亂碼,如一串????,F(xiàn)在通過一個例子來進行總結如下:
寫一個用戶信息提交頁面,通過這個頁面向Servlet JSP頁面提交用戶信息,代碼如下:
- <%@ page language="java" contentType="text/html; charset=gbk"%>
- <html>
- <head>
- <title>表單提交</title>
- </head>
- <body>
- <form action="deal.jsp" method="post">
- 用戶名:<input type="text" name="username"><br>
- 密 碼:<input type="password" name="password"><br>
- 愛 好:<input type="radio" name="love" value="運動">運動
- <input type="radio" name="love" value="音樂">音樂<br>
- <input type="submit" value="提交">
- </form>
- </body>
- </html>
現(xiàn)在寫deal處理頁面,代碼如下:
- <%@ page language="java" contentType="text/html; charset=gbk"%>
- <html>
- <head>
- <title>顯示用戶信息</title>
- </head>
- <body>
- <%
- //request.setCharacterEncoding("gb2312");
- String username = request.getParameter("username");
- //String username = new String(request.getParameter("username").getBytes("iso-8859-1"),"gbk");
- String password = request.getParameter("password");
- //String love = new String(request.getParameter("love").getBytes("iso-8859-1"),"gbk");
- String love = request.getParameter("love");
- %>
- <%= username %>您好,你的密碼是:<%= password %>,您的愛好是:<%= love %>!
- </body>
- </html>
從前面的信息提交頁面提交來的信息包含中文,這時就會出現(xiàn)亂碼。如:
??????您好,你的密碼是:1234569,您的愛好是:????!
現(xiàn)在,把第8行的注釋符號去掉,重新執(zhí)行頁面(請確保web服務器會自動加載更改后的頁面,否則請重新啟動web服務器),這時可以看到正確的中文信息了,如:
王中玉您好,你的密碼是:9856322,您的愛好是:音樂!
也可以使用另外一種方法進行處理,把deal.jsp的第8行注釋掉,然后把第9行、第13行也注釋掉,去掉第10行和第12行的注釋符號,保存好重新執(zhí)行頁面(方法同上),同樣會顯示正常的信息。
下面通過前面的信息提交頁面向一個servlet提交信息,然后對其中的中文亂碼進行處理。寫一個servlet程序(formdeal.java),如下:
- package org.wzhongyu;
- import java.io.IOException;
- import java.io.PrintWriter;
- import javax.servlet.ServletException;
- import javax.servlet.http.HttpServlet;
- import javax.servlet.http.HttpServletRequest;
- import javax.servlet.http.HttpServletResponse;
- public class formdeal extends HttpServlet {
- public void destroy() {
- super.destroy(); // Just puts "destroy" string in log
- // Put your code here
- }
- public void doGet(HttpServletRequest request, HttpServletResponse response)
- throws ServletException, IOException {
- this.doPost(request, response);
- }
- public void doPost(HttpServletRequest request, HttpServletResponse response)
- throws ServletException, IOException {
- //response.setContentType("text/html; charset=gbk");
- PrintWriter out = response.getWriter();
- //request.setCharacterEncoding("gbk");
- String username = request.getParameter("username");
- String password = request.getParameter("password");
- String love = request.getParameter("love");
- out.print("您的用戶名:" + username + "<br>"); //
- out.print("您的密碼:" + password + "<br>"); //
- out.print("您的愛好:" + love); //
- }
- public void init() throws ServletException {
- // Put your code here
- }
- }
該servlet的部署描述文件(web.xml)如下:
- <?xml version="1.0" encoding="UTF-8"?>
- <web-app version="2.5"
- xmlns="http://java.sun.com/xml/ns/javaee"
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
- http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
- <servlet>
- <description>This is the description of my J2EE component</description>
- <display-name>This is the display name of my J2EE component</display-name>
- <servlet-name>formdeal</servlet-name>
- <servlet-class>org.wzhongyu.formdeal</servlet-class>
- </servlet>
- <servlet-mapping>
- <servlet-name>formdeal</servlet-name>
- <url-pattern>/servlet/formdeal</url-pattern>
- </servlet-mapping>
- </web-app>
把信息提交頁面的第7行改為:
- <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í)行后顯示的信息如下:
- username???
- password65462458
- love??
這是由于沒有設置servlet響應的頁面的字符編碼造成的。
在servlet里也可以這樣進行處理,把第25行注釋掉,而不要注釋第23行,把第26行和第28行分別改為如下代碼:
- String username = new String(request.getParameter("username").getBytes("iso-8859-1"),"gbk");
- String love = new String(request.getParameter("love").getBytes("iso-8859-1"),"gbk");
以上是Servlet JSP頁面亂碼修改方法,這樣也可以正常顯示中文信息。
【編輯推薦】