Appendix A.2 Character code conversion between the browser and application

This section describes the methods to be used for character code conversion between the browser and application, and the notes on the implementation of the JSPs and JavaScript.

Organization of this subsection
(1) Methods used for character code conversion between the browser and application
(2) Notes on including a file in a JSP
(3) Notes on creating a query string using JavaScript

(1) Methods used for character code conversion between the browser and application

Use the method of the HttpServletRequest class to obtain the query string and POST data sent using the browser. From among the methods of the HttpServletRequest class, the methods related to character code conversion and the notes on usage are as follows:

(a) setCharacterEncoding method

With this method, you set the character code used in the message body of the request, and in the parameters sent with the GET request query. This method must be executed before the input stream is read by using the getParameter method and the getReader method.

(a) setCharacterEncoding method

With this method, you set the character code used in the message body of the request, and in the parameters sent with the GET request query. This method must be executed before the input stream is read by using the getParameter method and the getReader method.

The following table describes the range in which the character code set up with the setCharacterEncoding method is used.

Table A-3 Range in which the character code set up with the setCharacterEncoding method is used

MethodData sent from the HTML form (including queries)Message body other than that described at the left
getParameterA--
getParameterNamesA--
getParameterValuesA--
getParameterMapA--
getInputStreamCC
getReaderCB
getQueryStringC--
Legend:
A: The obtained value is converted to Unicode as the character code set up with the setCharacterEncoding method.
B: The character data is converted into the character code set up with the setCharacterEncoding method.
C: The encoded string is obtained.
--: Cannot be obtained.

(b) getParameter method

With this method, you specify the parameter name and obtain the value of the parameter included in the request. The obtained value is URL-decoded and converted to Unicode. Before you obtain the parameter name and parameter value, you must specify the character code by using the setCharacterEncoding method. If the character code is not specified, Unicode conversion is performed assuming ISO-8859-1. The same applies to the getParameterNames method, getParameterValues method, and getParameterMap method of the HttpServletRequest class.

(c) getInputStream method

With this method, you obtain the stream for reading the binary data included in the message body of the request. The encoded string is obtained as is from the data sent from the HTML form; therefore, the obtained string must be decoded using an appropriate character code.

(d) getReader method

With this method, you use the BufferedReader class in the message body of the request and extract the data as the character data. The character data is converted to the same character code as the message body. The encoded string is obtained as is from the data sent from the HTML form; therefore, the obtained string must be decoded using an appropriate character code.

(e) getQueryString method

This method returns the query string included behind the requested URL path. The encoded string is obtained as is from the data sent from the HTML form; therefore, the obtained string must be decoded using an appropriate character code.

(2) Notes on including a file in a JSP

To include a file using the include directive of the JSP file, use the contentType attribute to specify the encoding in the JSP file that forms the include source. Also, specify the character code for the JSP file in the pageEncoding attribute. If the character code is not specified, the include destination characters might not be displayed normally.

(3) Notes on creating a query string using JavaScript