1. 程式人生 > >servlet讀取表單資料和返回頁面時的中文亂碼問題

servlet讀取表單資料和返回頁面時的中文亂碼問題

用myeclipse做了一個最簡單的表單處理servlet,可是竟然出了N多錯誤.

MyHtml.html

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<!--
Front end to ThreeParams servlet.

Taken from Core Servlets and JavaServer Pages 2nd Edition
from Prentice Hall and Sun Microsystems Press,
http://www.coreservlets.com/.
(C) 2003 Marty Hall; may be freely used or adapted.
-->
<HTML><HEAD><TITLE>Collecting Three Parameters</TITLE></HEAD>
<BODY BGCOLOR="#FDF5E6">
<H1 ALIGN="CENTER">引數例子</H1>

<FORM ACTION="/ThreeParameters/servlet/ThreeParameters " ><--要包含工程名字,不用包含servlet的包名-->
  First Parameter:  <INPUT TYPE="TEXT" NAME="param1"><BR>
  Second Parameter: <INPUT TYPE="TEXT" NAME="param2"><BR>
  Third Parameter:  <INPUT TYPE="TEXT" NAME="param3"><BR>
  <CENTER><INPUT TYPE="SUBMIT"></CENTER>
</FORM>

</BODY></HTML>

ThreeParameters.java

import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;

/** Simple servlet that reads three parameters from the
 *  form data.
 *  <P>
 *  Taken from Core Servlets and JavaServer Pages 2nd Edition
 *  from Prentice Hall and Sun Microsystems Press,
 * 

http://www.coreservlets.com/.
 *  &copy; 2003 Marty Hall; may be freely used or adapted.
 */

public class ThreeParameters extends HttpServlet {
  public void doGet(HttpServletRequest request,
                    HttpServletResponse response)
      throws ServletException, IOException {
   
   
    response.setContentType("text/html;charset=/"GBK/"");
    PrintWriter out = response.getWriter();
    String title = "Reading Three Request Parameters";
    String docType =
      "<!DOCTYPE HTML PUBLIC /"-//W3C//DTD HTML 4.0 " +
      "Transitional//EN/">/n";
   // request.setCharacterEncoding("GB2312");這句話在這裡沒有用。因為GET請求的引數及其值都是通過URL加密傳輸的。只要把瀏覽器的編碼設定成GBK即可。當用POST請求時,這句話才起作用(防止伺服器得到的引數及引數值是亂碼)。究竟伺服器是怎麼讀取客戶端資料的,還是不很明白。
    out.println(docType +
                "<HTML>/n" +
                "<HEAD><TITLE>" + title + "</TITLE></HEAD>/n" +
                "<BODY BGCOLOR=/"#FDF5E6/">/n" +
                "<H1 ALIGN=/"CENTER/">" + title + "</H1>/n" +
                "<UL>/n" +
                "  <LI><B>引數1</B>: "
                + request.getParameter("param1") + "/n" +
                "  <LI><B>param2</B>: "
                + request.getParameter("param2") + "/n" +
                "  <LI><B>param3</B>: "
                + request.getParameter("param3") + "/n" +
                "</UL>/n" +
                "</BODY></HTML>");
  }
}
1.html頁面不能把資料傳給servlet,原因:form內的url沒有包含全,url在myeclipse中比在tomcat中釋出多了“/專案名”

2.一開始servlet不能顯示漢字,就是不管是request的資料,還是response的資料都是亂碼.解決辦法:在response的getWriter()之前,serContenType("text/html;charset="GBK"")

3,上面的解決方法看起來可以了.但當servlet用doGet()處理資料時,就是設定了2中所說的,也是亂碼,原因:資料在還沒有送到servlet前就是亂碼了(瀏覽器位址列編碼了),即使servlet設定好了收到的已經是亂碼.所以當用doGet()處理資料時,還要把瀏覽器的編碼設定成GBK.具體做法:conf/server.xml中的  <Connector
port="80"               maxHttpHeaderSize="8192"
               maxThreads="150" minSpareThreads="25" maxSpareThreads="75"
               enableLookups="false" redirectPort="8443" acceptCount="100"
               connectionTimeout="20000" disableUploadTimeout="true" />中新增URIEncoding="GBK".(tomcat版本不同,裡面的值也會不同,但是總是有這幾個屬性)