JSP Tutorials - Herong's Tutorial Examples - v5.11, by Herong Yang
Java Strings as Unicode Encoded Byte Sequences
This section provides a tutorial example on how to use Java String data type to enter non-ASCII character as Unicode characters. Java Servlet technology is smart to convert Unicode characters into the encoding specified in the setContentType() call.
Let's try option 2 mentioned in the previous section now, which uses Java Strings to enter non-ASCII characters as Unicode characters. Java Servlet technology can automatically convert them in the HTML response in any other specified encoding. Here is my sample JSP page, HelpGB2312Unicode.jspx:
<?xml version="1.0"?> <jsp:root xmlns:jsp="http://java.sun.com/JSP/Page" version="2.3"> <!-- HelpGB2312Unicode.jspx - Copyright (c) 2006 HerongYang.com. All Rights Reserved. --> <jsp:scriptlet><![CDATA[ response.setContentType("text/html; charset=gb2312"); out.println("<html>"); out.println("<meta http-equiv=\"Content-Type\"" + " content=\"text/html; charset=gb2312\"/>"); out.println("<body>"); out.println("<b>\u8bf4\u660e</b><br/>"); out.println("<p>\u8fd9\u662f\u4e00\u4efd\u975e\u5e38\u95f4\u5355" + "\u7684\u8bf4\u660e\u4e66\u2026</p>"); out.println("</body>"); out.println("</html>"); ]]></jsp:scriptlet> </jsp:root>
When I opened HelpGB2312Unicode.jspx with IE, I saw Chinese characters correctly displayed on the screen. Remember I have Unicode Chinese fonts installed on my system. So option 2 works! But note that:
If your Chinese text is in GB2312 encoding format, you need to convert the text to Unicode codes in "\u" format. One good tool for this is native2ascii from JDK 1.8 or older. Here is a sample command to convert HelpGB2312.html:
herong> native2ascii -encoding gb2312 HelpGB2312.html test.html
You could also enter non ASCII characters as Unicode codes in UTF-8 format. This is very easy to do, if you have a special text editor that supports Unicode UTF-8 encoding and input interface for your local language characters.
Table of Contents
JSP (JavaServer Pages) Overview
Tomcat Installation on Windows Systems
Syntax of JSP Pages and JSP Documents
JavaBean Objects and "useBean" Action Elements
Managing HTTP Response Header Lines
►Non-ASCII Characters Support in JSP Pages
Characters Traveling from JSP Files to Browser Screens
Handling ASCII Characters in JSP Pages
Presenting Non ASCII Characters in HTML Documents
Entering Non ASCII Characters in JSP Pages
Java Strings as non-Unicode Encoded Byte Sequences
►Java Strings as Unicode Encoded Byte Sequences
Entering Non-ASCII Characters as Static Text
Static HTML Text in JSP Page in Standard Syntax
Static HTML Text in JSP Page in XML Syntax
Supporting Characters in Multiple Languages
Overview of JSTL (JSP Standard Tag Libraries)
Multiple Tags Working Together
Using Tomcat on CentOS Systems
Connecting to SQL Server from Servlet