Mega Code Archive

 
Categories / Java Tutorial / Servlet
 

Servlet Unicode

import java.io.*; import java.text.*; import java.util.*; import javax.servlet.*; import javax.servlet.http.*; public class MyServlet extends HttpServlet {   public void doGet(HttpServletRequest req, HttpServletResponse res)                                throws ServletException, IOException {     Locale locale;     DateFormat full;     try {       res.setContentType("text/plain; charset=UTF-8");       //PrintWriter out = res.getWriter();       PrintWriter out = new PrintWriter(         new OutputStreamWriter(res.getOutputStream(), "UTF8"), true);       locale = new Locale("en", "US");       full = DateFormat.getDateTimeInstance(DateFormat.LONG,                                              DateFormat.LONG,                                             locale);       out.println("In English appropriate for the US:");       out.println("Hello World!");       out.println(full.format(new Date()));       out.println();       locale = new Locale("es", "");       full = DateFormat.getDateTimeInstance(DateFormat.LONG,                                              DateFormat.LONG,                                             locale);       out.println("En Espa\u00f1ol:");       out.println("\u00a1Hola Mundo!");       out.println(full.format(new Date()));       out.println();       locale = new Locale("ja", "");       full = DateFormat.getDateTimeInstance(DateFormat.LONG,                                             DateFormat.LONG,                                             locale);       out.println("In Japanese:");       out.println("\u4eca\u65e5\u306f\u4e16\u754c");       out.println(full.format(new Date()));       out.println();       locale = new Locale("zh", "");       full = DateFormat.getDateTimeInstance(DateFormat.LONG,                                             DateFormat.LONG,                                             locale);       out.println("In Chinese:");       out.println("\u4f60\u597d\u4e16\u754c");       out.println(full.format(new Date()));       out.println();       locale = new Locale("ko", "");       full = DateFormat.getDateTimeInstance(DateFormat.LONG,                                             DateFormat.LONG,                                             locale);       out.println("In Korean:");       out.println("\uc548\ub155\ud558\uc138\uc694\uc138\uacc4");       out.println(full.format(new Date()));       out.println();       locale = new Locale("ru", "");       full = DateFormat.getDateTimeInstance(DateFormat.LONG,                                             DateFormat.LONG,                                             locale);       out.println("In Russian (Cyrillic):");       out.print("\u0417\u0434\u0440\u0430\u0432\u0441\u0442");       out.println("\u0432\u0443\u0439, \u041c\u0438\u0440");       out.println(full.format(new Date()));       out.println();     }     catch (Exception e) {       e.printStackTrace();     }   } } <?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>