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.1"> 
<!-- HelpGB2312Java.jspx
 - Copyright (c) 2012, 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.jsp with IE, I saw Chinese characters correctly displayed on the screen. So option 1 works! But note that:

Last update: 2012.

Table of Contents

 About This Book

 JSP (JavaServer Pages) Overview

 Tomcat 7 Installation on Windows Systems

 JSP Scripting Elements

 Java Servlet Introduction

 JSP Implicit Objects

 Syntax of JSP Pages and JSP Documents

 JSP Application Session

 Managing Cookies in JSP Pages

 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 HTML Page

 Static HTML Text in JSP Page in Standard Syntax

 Static HTML Text in JSP Page in XML Syntax

 Supporting Characters in Multiple Languages

 Performance of JSP Pages

 EL (Expression Language)

 Overview of JSTL (JSP Standard Tag Libraries)

 JSTL Core Library

 JSP Custom Tags

 JSP Java Tag Interface

 Custom Tag Attributes

 Multiple Tags Working Together

 File Upload Test Application

 Outdated Tutorials

 References

 PDF Printing Version