你可以從HttpServletResponse的getWriter()取得PrintWriter物件,透過該物件對客戶端進行字元輸出。在沒有設定任何內容型態或編碼之前,容器所使用的字元編碼是ISO-8859-1。
你可以使用HttpServletResponse的setLocale()來設定地區(Locale)資訊,setLocale()必須在回應確認前設定,回應確認後設定地區資訊並沒有效果。容器如何與客戶端溝通並沒有規範,通常使用Content-Language標頭來設定。例如以下會將HTTP 回應的Content-Language設定為zh-TW:
你可以使用HttpServletResponse的getCharacterEncoding()方法取得編碼設定, 上面的例子設定過後,預設編碼是Big5,你可以在web.xml 中設定區域與編碼的對應。例如:
<locale-encoding-mapping-list>
<locale-encoding-mapping>
<locale>zh_TW</locale>
<encoding>UTF-8</encoding>
</locale-encoding-mapping>
</locale-encoding-mapping-list>
則在若搭配先前的程式片段,或者是以下的程式片段,getCharacterEncoding()取得的就是UTF-8:
你也可以呼叫HttpServletResponse的setCharacgerEncoding()設定字元編碼:
或者是在使用HttpServletResponse的setContentType()時,指定charset,charset的值會用來呼叫setCharacterEncoding()。例如:
如果使用了setCharacterEncoding()或setContentType()時指定了charset,則setLocale()就會被忽略。
結合 請求參數、標頭 與這邊的說明,可以得知的是,如果你要可以接收中文請求參數並在回應時於瀏覽器顯示出來,必須設定HttpServletRequest的setCharacterEncoding()以及HttpServletResponse的setCharacterEncoding()或setContentType()為正確的編碼。
例如 以下這個範例,可以讓你透過表單發送中文請求參數值,Servlet可正確地接收處理並顯示在結果畫面上。你可以使用表單發送名稱、郵件與多選喜愛的寵物類型代表。首先是表單的部份::
接著是Servlet的部份:
實際上若頁面很多,逐個頁面設定編碼是件很麻煩的事,所以設定編碼的動作其實不會直接在Servlet中進行,而會在過濾器(Filter)中設定,之後還會談到過濾器的細節。
你可以使用HttpServletResponse的setLocale()來設定地區(Locale)資訊,setLocale()必須在回應確認前設定,回應確認後設定地區資訊並沒有效果。容器如何與客戶端溝通並沒有規範,通常使用Content-Language標頭來設定。例如以下會將HTTP 回應的Content-Language設定為zh-TW:
resp.setLocale(Locale.TAIWAN);
你可以使用HttpServletResponse的getCharacterEncoding()方法取得編碼設定, 上面的例子設定過後,預設編碼是Big5,你可以在web.xml 中設定區域與編碼的對應。例如:
<locale-encoding-mapping-list>
<locale-encoding-mapping>
<locale>zh_TW</locale>
<encoding>UTF-8</encoding>
</locale-encoding-mapping>
</locale-encoding-mapping-list>
則在若搭配先前的程式片段,或者是以下的程式片段,getCharacterEncoding()取得的就是UTF-8:
resp.setLocale(new Locale("zh", "TW"));
你也可以呼叫HttpServletResponse的setCharacgerEncoding()設定字元編碼:
resp.setCharacterEncoding("UTF-8");
或者是在使用HttpServletResponse的setContentType()時,指定charset,charset的值會用來呼叫setCharacterEncoding()。例如:
resp.setContentType("text/html; charset=UTF-8");
如果使用了setCharacterEncoding()或setContentType()時指定了charset,則setLocale()就會被忽略。
結合 請求參數、標頭 與這邊的說明,可以得知的是,如果你要可以接收中文請求參數並在回應時於瀏覽器顯示出來,必須設定HttpServletRequest的setCharacterEncoding()以及HttpServletResponse的setCharacterEncoding()或setContentType()為正確的編碼。
例如 以下這個範例,可以讓你透過表單發送中文請求參數值,Servlet可正確地接收處理並顯示在結果畫面上。你可以使用表單發送名稱、郵件與多選喜愛的寵物類型代表。首先是表單的部份::
<html> <head> <title>寵物類型大調查</title> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> </head> <body> <form action="pet.view" method="post"> 姓名:<input type="text" name="user" value=""><br> 郵件:<input type="text" name="email" value=""><br> 你喜愛的寵物代表:<br> <select name="type" size="6" multiple="true"> <option value="貓">貓</option> <option value="狗">狗</option> <option value="魚">魚</option> <option value="鳥">鳥</option> </select><br> <input type="submit" value="送出"/> </form> </body> </html>
接著是Servlet的部份:
package cc.openhome; import java.io.*; import javax.servlet.*; import javax.servlet.annotation.*; import javax.servlet.http.*; @WebServlet(name="Pet", urlPatterns={"/pet.view"}) public class Pet extends HttpServlet { @Override protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { request.setCharacterEncoding("UTF-8");
response.setContentType("text/html; charset=UTF-8");
PrintWriter out = response.getWriter(); out.println("<html>"); out.println("<head>"); out.println("<title>感謝</title>"); out.println("</head>"); out.println("<body>"); out.println("聯絡人:<a href=\"mailto:"+ request.getParameter("email") + "\">" + request.getParameter("user") + "</a>"); out.println("<br>喜愛的寵物類型"); out.println("<ul>"); for(String type : request.getParameterValues("type")) { out.println("<li>" + type + "</li>"); } out.println("</ul>"); out.println("</body>"); out.println("</html>"); out.close(); } }
實際上若頁面很多,逐個頁面設定編碼是件很麻煩的事,所以設定編碼的動作其實不會直接在Servlet中進行,而會在過濾器(Filter)中設定,之後還會談到過濾器的細節。