JSP Tutorials - Herong's Tutorial Examples - v5.11, by Herong Yang
Java Strings as non-Unicode Encoded Byte Sequences
This section provides a tutorial example on how to use Java String data type to enter non-ASCII character as byte sequences encoded with a local language encoding schema.
Let's try option 1 mentioned in the previous section first, which uses Java strings to enter non-ASCII characters as byte sequences encoded with a local language encoding schema. Here is my sample JSP page, HelpGB2312Java.jspx:
<?xml version="1.0"?> <jsp:root xmlns:jsp="http://java.sun.com/JSP/Page" version="2.3"> <!-- HelpGB2312Java.jspx - Copyright (c) 2006 HerongYang.com. All Rights Reserved. --> <jsp:directive.page contentType="text/html; charset=gb2312"/> <jsp:declaration><![CDATA[ private java.io.OutputStream outStream; private void writeGB(String s) throws Throwable { for (int i=0; i<s.length(); i++) { char c = s.charAt(i); byte b = (byte) (c>>8 & 0x00FF); if (b!=0) outStream.write(b); b = (byte) (c & 0x00FF); outStream.write(b); } } ]]></jsp:declaration> <jsp:scriptlet><![CDATA[ outStream = response.getOutputStream(); writeGB("<html>"); writeGB("<meta http-equiv=\"Content-Type\"" + " content=\"text/html; charset=gb2312\"/>"); writeGB("<body>"); writeGB("<b>\uCBB5\uC3F7</b>"); writeGB("<p>\uD5E2\uCAC7\uD2BB\uB7DD\uB7C7\uB3A3\uBCE4\uB5A5" + "\uB5C4\uCBB5\uC3F7\uCAE9\uA1AD</p>"); writeGB("</body>"); writeGB("</html>"); ]]></jsp:scriptlet> </jsp:root>
When I opened HelpGB2312Java.jspx with IE, I saw Chinese characters correctly displayed on the screen. So option 1 works! But note that:
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