You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@xalan.apache.org by Frank Chen <fr...@ms5.hinet.net> on 2001/05/15 06:41:09 UTC

A Servlet Question

Hi:

When we send form data through GET method,  the browser will do the
URL-encoding. And when we use HttpServletRequest's getParameter() method to
retrieve a value, it should do the URL-decoding back to the original.

I sent form data in Chinese characters that is big5 encoding, the
URL-encoding form is %XX. When I extract a parameter through
HttpServletRequest's getParameter(), I found it didn't convert back to big5
encoding. I use Tomcat 3.2.1.

ParamValue = request.getParameter('option');

If it cannot convert back, I cannot further use the value in Transformer's
setParameter():

transformer.setParameter(ParamName, new XString(ParamValue));

Is there any way to walk around, or any method/class to manually do
the URL-decoding?

When I use a PrintWriter to print ParamValue, it does convert back to big5:

PrintWriter out = response.getWriter();
out.println(ParamValue);  // ParamValue is correct in this context.

Frank




Re: A Servlet Question

Posted by Frank Chen <fr...@ms5.hinet.net>.
Hi, jason:

Thanks for your pattern code.
Tomcat does the conversion like the way your code shows.
The problem is that a Chinese character represents two %XX%XX or %XXx, %XX_
and the like. This literal-by-literal conversion loses its original encoding
meaning. However, your pattern code inspires me a solution to this, stupid,
but workable! I made a simple hashtable table to get the right thing out.

Thanks again.

Frank
----- Original Message -----
From: "jason heddings" <Ja...@Sun.COM>
To: <xa...@xml.apache.org>
Sent: Tuesday, May 15, 2001 10:30 PM
Subject: Re: A Servlet Question


> Frank-
>
> Here are the methods I have to get back the encoded "HTTP-safe"
> characters:
>
>     /** Decodes the cookie into human-redable characters.
>      *
>      *  @param string the string to decode
>      *  @return the decoded string
>      */
>     public static String decode( String str ) {
>         int len = str.length( ) ;
>         StringBuffer newstr = new StringBuffer( len ) ;
>
>         for ( int i = 0 ; i < len ; i++ ) {
>             if ( str.charAt( i ) == '+' ) {
>                 newstr.append( ' ' ) ;
>             } else if ( str.charAt( i ) == '%' ) {
>                 newstr.append(
>                   dd2c( str.charAt( i + 1 ), str.charAt( i + 2 ) ) ) ;
>                 i += 2 ;
>             } else {
>                 newstr.append( str.charAt( i ) ) ;
>             }
>         }
>
>         return newstr.toString( ) ;
>     }
>
>     /** Transform two hex digits to corresponding char.
>      */
>     public static char dd2c(char d1, char d2) {
>         int d1int = Character.digit( d1 , 16 ) ;
>         int d2int = Character.digit( d2 , 16 ) ;
>         return (char) ( d1int * 16 + d2int ) ;
>     }
>
>
> As far as Tomcat automatically decoding it for you, I'm not sure.  I'm
> sure there are some Tomcat-heads around here or on the other lists in
> the Jakarta project that would be able to help you out.
>
> HTH,
> -- jah
>
>  ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
>       \\\|///                 Jason Heddings             ((
>      \\ ~ ~ //                303.272.5166 (x75166)    C|~~|
>      (/ @ @ /)                Jason.Heddings@Sun.COM    `__'
>  ~~oOOo~(_)~oOOo~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~


Re: A Servlet Question

Posted by jason heddings <Ja...@Sun.COM>.
Frank-

Here are the methods I have to get back the encoded "HTTP-safe"
characters:  

    /** Decodes the cookie into human-redable characters.
     *
     *  @param string the string to decode
     *  @return the decoded string
     */
    public static String decode( String str ) {
        int len = str.length( ) ;
        StringBuffer newstr = new StringBuffer( len ) ;

        for ( int i = 0 ; i < len ; i++ ) {
            if ( str.charAt( i ) == '+' ) {
                newstr.append( ' ' ) ;
            } else if ( str.charAt( i ) == '%' ) {
                newstr.append(
                  dd2c( str.charAt( i + 1 ), str.charAt( i + 2 ) ) ) ;
                i += 2 ;
            } else {
                newstr.append( str.charAt( i ) ) ;
            }
        }

        return newstr.toString( ) ;
    }

    /** Transform two hex digits to corresponding char.
     */
    public static char dd2c(char d1, char d2) {
        int d1int = Character.digit( d1 , 16 ) ;
        int d2int = Character.digit( d2 , 16 ) ;
        return (char) ( d1int * 16 + d2int ) ;
    }


As far as Tomcat automatically decoding it for you, I'm not sure.  I'm
sure there are some Tomcat-heads around here or on the other lists in
the Jakarta project that would be able to help you out.

HTH,
-- jah

 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
      \\\|///                 Jason Heddings             ((
     \\ ~ ~ //                303.272.5166 (x75166)    C|~~|
     (/ @ @ /)                Jason.Heddings@Sun.COM    `__'
 ~~oOOo~(_)~oOOo~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~