Servlet UTF8 Page : Internationlization I18N « Servlet « Java Tutorial






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

public class MyServlet extends HttpServlet {
  PrintWriter out = null;
  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();
      out = new PrintWriter(
        new OutputStreamWriter(res.getOutputStream(), "UTF8"), true);

      char[] buf = new char[4 * 1024];  // 4Kchar buffer
      int len;
      while ((len = reader.read(buf, 0, buf.length)) != -1) {
        out.write(buf, 0, len);
      }
      out.flush();
    }
    catch (Exception e) {
      out.println("Problem filtering page to UTF-8");
      getServletContext().log(e, "Problem filtering page to UTF-8");
    }
    out.println("Done");
  }

  public void doPost(HttpServletRequest req, HttpServletResponse res)
                         throws ServletException, IOException {
    doGet(req, res);
  }
}
<?xml version="1.0" encoding="iso-8859-1"?>
<!DOCTYPE web-app
PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd">
<web-app>

    <servlet><servlet-name>MyServletName</servlet-name>
             <servlet-class>MyServlet</servlet-class>
    </servlet>
    
    <servlet-mapping><servlet-name>MyServletName</servlet-name>
        <url-pattern>*.htm</url-pattern>
    </servlet-mapping>

  <context-param>
    <param-name>javax.servlet.jsp.jstl.fmt.timeZone</param-name>
    <param-value>US/Central</param-value>
  </context-param>

  <context-param>
    <param-name>database-driver</param-name>
    <param-value>org.gjt.mm.mysql.Driver</param-value>
  </context-param>

  <context-param>
    <param-name>database-url</param-name>
    <param-value>
    jdbc:mysql://localhost/forum?user=forumuser</param-value>
  </context-param>

  <taglib>
    <taglib-uri>http://java.sun.com/jstl/fmt</taglib-uri>
    <taglib-location>/WEB-INF/fmt.tld</taglib-location>
  </taglib>

  <taglib>
    <taglib-uri>http://java.sun.com/jstl/fmt-rt</taglib-uri>
    <taglib-location>/WEB-INF/fmt-rt.tld</taglib-location>
  </taglib>

  <taglib>
    <taglib-uri>http://java.sun.com/jstl/core</taglib-uri>
    <taglib-location>/WEB-INF/c.tld</taglib-location>
  </taglib>

  <taglib>
    <taglib-uri>http://java.sun.com/jstl/core-rt</taglib-uri>
    <taglib-location>/WEB-INF/c-rt.tld</taglib-location>
  </taglib>

  <taglib>
    <taglib-uri>http://java.sun.com/jstl/sql</taglib-uri>
    <taglib-location>/WEB-INF/sql.tld</taglib-location>
  </taglib>

  <taglib>
    <taglib-uri>http://java.sun.com/jstl/sql-rt</taglib-uri>
    <taglib-location>/WEB-INF/sql-rt.tld</taglib-location>
  </taglib>

  <taglib>
    <taglib-uri>http://java.sun.com/jstl/x</taglib-uri>
    <taglib-location>/WEB-INF/x.tld</taglib-location>
  </taglib>

  <taglib>
    <taglib-uri>http://java.sun.com/jstl/x-rt</taglib-uri>
    <taglib-location>/WEB-INF/x-rt.tld</taglib-location>
  </taglib>

  <taglib>
    <taglib-uri>
    http://java.jeffheaton.com/taglib/jstl/forum</taglib-uri>
    <taglib-location>/WEB-INF/forum.tld</taglib-location>
  </taglib>

  <taglib>
    <taglib-uri>/tlt</taglib-uri>
    <taglib-location>/WEB-INF/taglib.tld</taglib-location>
  </taglib>
</web-app>
  Download:  ServletUTF8Page.zip( 1,066 k)








25.24.Internationlization I18N
25.24.1.Servlet UTF8 Page
25.24.2.Servlet Unicode Escape String
25.24.3.Servlet Unicode
25.24.4.Servlet Spanish
25.24.5.Servlet I18N: Read From File
25.24.6.Servlet I18N: Page Header
25.24.7.Japanese Hello World Servlet
25.24.8.Locale Information Servlet
25.24.9.Set content type to charset=ISO-8850-1
25.24.10.Locale Session Servlet