Adobe COLDFUSION 9 Manual page 381

Developing applications
Hide thumbs Also See for COLDFUSION 9:
Table of Contents

Advertisement

DEVELOPING COLDFUSION 9 APPLICATIONS
Developing CFML Applications
One common method used to support non-ASCII characters within a URL is to include a name-value pair within the
URL that defines the character encoding of the URL. For example, the following URL uses a parameter called encoding
to define the character encoding of the URL parameters:
http://company.com/prod_page.cfm?name=Stephen;ID=7645;encoding=Latin-1
Within the prod_page.cfm page, you can check the value of the encoding parameter before processing any of the other
name-value pairs. This guarantees that you handle the parameters correctly.
You can also use the SetEncoding function to specify the character encoding of URL parameters. The
function takes two parameters: the first specifies a variable scope and the second specifies the character encoding used
by the scope. Since ColdFusion writes URL parameters to the URL scope, you specify "URL" as the scope parameter to
the function.
For example, if the URL parameters are passed using Shift-JIS, you could access them as follows:
<cfscript>
setEncoding("URL", "Shift_JIS");
writeoutput(URL.name);
writeoutput(URL.ID);
</cfscript>
Note: To specify the Shift-JIS character encoding, use the Shift_JIS attribute, with an underscore (_), not a hyphen (-).
Handling form data
The HTML
tag and the ColdFusion
form
The form tags are designed to work only with single-byte character data. Since ColdFusion uses 2 bytes per character
when it stores strings, ColdFusion converts each byte of the form input into a two-byte representation.
However, if a user enters double-byte text into the form, the form interprets each byte as a single character, rather than
recognize that each character is 2 bytes. This corrupts the input text, as the following example shows:
A customer enters three double-byte characters in a form, represented by 6 bytes.
1
The form returns the six bytes to ColdFusion as six characters. ColdFusion converts them to a representation using
2
2 bytes per input byte for a total of 12 bytes.
3
Outputting these characters results in corrupt information displayed to the user.
To work around this issue, use the
function takes two parameters: the first specifies the variable scope and the second specifies the
SetEncoding
character encoding used by the scope. Since ColdFusion writes form parameters to the Form scope, you specify "Form"
as the scope parameter to the function. If the input text is double-byte, ColdFusion preserves the two-byte
representation of the text.
The following example specifies that the form data contains Korean characters:
<cfscript>
setEncoding("FORM", "EUC-KR");
</cfscript>
<h1> Form Test Result </h1>
<strong>Form Values :</strong>
<cfset text = "String = #form.input1# , Length = #len(Trim(form.input1))#">
<cfoutput>#text#</cfoutput>
tag let users enter text on a page, then submit that text to the server.
cfform
function to specify the character encoding of input form text. The
SetEncoding
Last updated 8/5/2010
376
SetEncoding

Advertisement

Table of Contents
loading

Table of Contents