Receiving Multilingual Input
We need to discuss one more aspect of internationalization: receiving
multilingual input. It’s actually
quite simple for a servlet to receive multilingual character data.
The ServletRequest.getReader() method handles the
task automatically. It returns a BufferedReader
specially built to read the character encoding of the input data. For
example, if the Content-Type of the
servlet’s input is "text/html; charset=Shift_JIS", the BufferedReader
is one that reads Shift_JIS characters.
Because getReader() works automatically, it means
our Deblink servlet and other chained servlets
found throughout the book are already multilingual friendly. No
matter what charset is used for the content they receive, they always
read the input characters correctly using
getReader().
Example 12.13 shows another servlet that uses
getReader(). This servlet is designed to be the
last servlet in a chain. It uses getReader() to
read its input as character data, then outputs the characters using
the UTF-8 encoding.
Example 12-13. UTF-8 encoder
import java.io.*; import javax.servlet.*; import javax.servlet.http.*; public class UTF8 extends HttpServlet { public void doGet(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException { try { // Get a reader to read the incoming data BufferedReader reader = req.getReader(); // Get a writer to write the data in UTF-8 res.setContentType("text/html; charset=UTF-8"); PrintWriter out = res.getWriter(); // Read and write ...