Understanding Character Encoding in a Typical Java Web Application

Some pseudo codes:

String a = "A bunch of text"; //UTF-16
saveTextInDb(a); //Write to Oracle VARCHAR(15) column
String b = readTextFromDb(); //UTF-16
out.write(b); //Write to http response

When you save Java String(UTF-16) to Oracle VARCHAR (15), does Oracle also save this as UTF-16? Does Oracle VARCHAR length mean number of Unicode characters (not number of bytes)?

When we write bto ServletResponse, is it written as UTF-16 or are we by default converted to another encoding such as UTF-8?

+5
source share
3 answers

UTF-16 " " . Java - , , . , . saveTextInDb, readTextFromDb . , , . saveTextInDb ( ) , , . , . write Writer , , . Writer HttpServletResponse, ( ).

response.setEncoding("UTF-8");
Writer out = response.getWriter();

Writer, UTF-8. , :

Writer fileout = new OutputStreamWriter(new FileOutputStream(myfile), "ISO8859-1");

, .

+4

Oracle ( ) ( ). AL32UTF8 CHAR ( VARCHAR/VARCHAR2), Unicode, , , AL16UTF16/AL32UTF32.

, , Oracle JDBC UTF-16 AL32UTF8. "" , . VARCHAR, VARCHAR2 Oracle - VARCHAR2 (n) , n ( , NLS_LENGTH_SEMANTICS ); , VARCHAR2 (n CHAR).

, ServletResponse, , ServletResponse.setCharacterEncoding() ServletResponse.setContentType() API. , Unicode-, Oracle,

  • ( , ServletRequest). , HTML- accept-charset. , ServletRequest.setCharacterEncoding(). . ISO-Latin1, , , . , Java , . -, , ServletRequest.getParameter , String. ( UTF-16).
  • , , , JVM. , , , . , , UTF-16, . , String . , (, HttpServletResponse). , , JVM. , , , , String. , . ,

    • String , , . , .
    • , , , HTTP-,
    • , ( ), .
  • Oracle. , Oracle ( CHAR). Oracle JDBC UTF-16 AL32UTF8 ( ) CHAR NCHAR. resultSet.getString(), JDBC String UTF-16. , . , ( UTF-16 UTF-8 ) JDBC.
+4

ServletResponse ISO 8859-1 ( 1) . UTF-8 , HTTP, Unicode, .

According to this document, Oracle can support UTF-8 or UTF-16 in the database. Your Oracle read / write methods will need to use the appropriate encoding appropriate to the database setup and translate it to / from the internal Java view.

+3
source

All Articles