Solve the Problem of dwr1.0 Chinese in jdk1.3 Environment

Source: Internet
Author: User
Tags url decode

Dwr1.0 is used in weblogic7. When saving data to the backend, if it is Chinese, it is garbled.

Modify the source code of DWR as follows.

/********************************** Localutil class of DWR *** ****************************/

Package UK. Ltd. getahead. DWR. util;

Import java. Lang. Reflect. invocationtargetexception;
Import java. Lang. Reflect. method;
Import java.net. urldecoder;

Import javax. servlet. http. httpservletresponse;

Import UK. Ltd. getahead. DWR. conversionconstants;
Import com. minght. SYS. util. mht_urldecoder;

/**
* Various utilities, mostly to make up for JDK 1.4 functionallity that is not
* In JDK 1.3
* @ Author Joe Walker [Joe at getahead dot Ltd dot uk]
*/
Public final class localutil
{
/**
* Splitinbound () returns the type info in this parameter
*/
Public static final int inbound_index_type = 0;

/**
* Splitinbound () returns the value info in this parameter
*/
Public static final int inbound_index_value = 1;

/**
* Prevent instansiation
*/
Private localutil ()
{
}

/**
* Replacement for string # replaceall (string, string) in JDK 1.4 +
* @ Param text source text
* @ Param repl the stuff to get rid
* @ Param with the stuff to replace it
* @ Return replaced text or null if any ARGs are null
*/
Public static string Replace (string text, string repl, string)
{
If (text = NULL | repl = NULL | with = NULL | REPL. Length () = 0)
{
Return text;
}

Stringbuffer Buf = new stringbuffer (text. Length ());
Int searchfrom = 0;
While (true)
{
Int foundat = text. indexof (repl, searchfrom );
If (foundat =-1)
{
Break;
}

Buf. append (text. substring (searchfrom, foundat). append ();
Searchfrom = foundat + REPL. Length ();
}
Buf. append (text. substring (searchfrom ));

Return Buf. tostring ();
}

/**
* True if C1 is Java. Lang. boolean and C2 is Boolean, etc.
* @ Param C1 the first class to test
* @ Param C2 the second class to test
* @ Return true if the classes are equivalent
*/
Public static Boolean isequivalent (Class C1, Class C2)
{
If (C1 = Boolean. Class | C1 = Boolean. type)
{
Return C2 = Boolean. Class | C2 = Boolean. type;
}
Else if (C1 = byte. Class | C1 = byte. type)
{
Return C2 = byte. Class | C2 = byte. type;
}
Else if (C1 = character. Class | C1 = character. type)
{
Return C2 = character. Class | C2 = character. type;
}
Else if (C1 = short. Class | C1 = short. type)
{
Return C2 = short. Class | C2 = short. type;
}
Else if (C1 = integer. Class | C1 = integer. type)
{
Return C2 = integer. Class | C2 = integer. type;
}
Else if (C1 = long. Class | C1 = long. type)
{
Return C2 = long. Class | C2 = long. type;
}
Else if (C1 = float. Class | C1 = float. type)
{
Return C2 = float. Class | C2 = float. type;
}
Else if (C1 = double. Class | C1 = double. type)
{
Return C2 = double. Class | C2 = double. type;
}
Else if (C1 = void. Class | C1 = void. type)
{
Return C2 = void. Class | C2 = void. type;
}

Return false;
}

/**
* @ Param type the class to de-primitivize
* @ Return the non-privitive version of the class
*/
Public static class getnonprimitivetype (class type)
{
If (! Type. isprimitive ())
{
Return type;
}
Else if (type = Boolean. type)
{
Return Boolean. Class;
}
Else if (type = byte. type)
{
Return byte. Class;
}
Else if (type = character. type)
{
Return character. Class;
}
Else if (type = short. type)
{
Return short. Class;
}
Else if (type = integer. type)
{
Return integer. Class;
}
Else if (type = long. type)
{
Return long. Class;
}
Else if (type = float. type)
{
Return float. Class;
}
Else if (type = double. type)
{
Return double. Class;
}
Else if (type = void. type)
{
Return void. Class;
}

Return NULL;
}

/**
* Add headers to prevent browers and proxies from caching this reply.
* @ Param resp the response to add headers
*/
Public static void addnocacheheaders (httpservletresponse resp)
{
// Set standard HTTP/1.1 no-Cache headers.
Resp. setheader ("cache-control", "No-store, no-cache, must-revalidate"); // $ NON-NLS-1 $ // $ NON-NLS-2 $

// Set ie extended HTTP/1.1 no-Cache headers (use addheader ).
Resp. addheader ("cache-control", "post-check = 0, pre-check = 0"); // $ NON-NLS-1 $ // $ NON-NLS-2 $

// Set standard HTTP/1.0 no-Cache header.
Resp. setheader ("Pragma", "No-Cache"); // $ NON-NLS-1 $ // $ NON-NLS-2 $

// Set to expire far in the past. Prevents caching at the Proxy Server
Resp. setheader ("expires", "SAT, 6 May 1995 12:00:00 GMT"); // $ NON-NLS-1 $ // $ NON-NLS-2 $
}

/**
* URL decode a value. This method gets around the lack of
* Decode (string, string) method in JDK 1.3.
* @ Param value the string to decode
* @ Return the decoded string
*/
Public static string decode (string value)
{
Log. Warn ("decode before:" + value );
If (! Testeddecoder)
{
Try
{
Decode14 = urldecoder. Class. getmethod ("decode", new class [] {string. Class, String. Class}); // $ NON-NLS-1 $
}
Catch (exception ex)
{
If (! Warn13)
{
Log. Warn ("urldecoder. Decode (string), string) is not available. Falling back to 1.3 variant."); // $ NON-NLS-1 $
Warn13 = true;
}
}

Testeddecoder = true;
}

If (decode14! = NULL)
{
Try
{
Return (string) decode14.invoke (null, new object [] {value, "UTF-8"}); // $ NON-NLS-1 $
}
Catch (exception ex)
{
Log. Warn ("failed to use JDK 1.4 decoder", ex); // $ NON-NLS-1 $
}
}
Try {
Value = mht_urldecoder.decode (value, "UTF-8 ");
} Catch (exception ex ){
Log. Warn ("decode to UTF-8 error.", ex );
}
Return value;
}

/**
* Set a property on an object Using Reflection
* @ Param object the object to call the setter on
* @ Param key the name of the property to set.
* @ Param value the new value to use for the property
* @ Throws nosuchmethodexception passed on from reflection code
* @ Throws securityexception passed on from reflection code
* @ Throws illegalaccessexception passed on from reflection code
* @ Throws illegalargumentexception passed on from reflection code
* @ Throws invocationtargetexception passed on from reflection code
*/
Public static void setproperty (Object object, string key, object Value) throws nosuchmethodexception, securityexception, illegalaccessexception, illegalargumentexception, invocationtargetexception
{
Class real = object. getclass ();

String settername = "set" + key. substring (0, 1). touppercase () + key. substring (1); // $ NON-NLS-1 $
Method method = real. getmethod (settername, new class [] {value. getclass ()});

If (Method = NULL)
{
Throw new nosuchmethodexception ("missing property:" + key); // $ NON-NLS-1 $
}

Method. Invoke (object, new object [] {value });
}

/**
* The javascript outbound extends aller prefixes the tostring value with
* Colon and the original type information. This undoes that.
* @ Param data the string to be split up
* @ Return a string array containing the split data
*/
Public static string [] splitinbound (string data)
{
String [] Reply = new string [2];

Int colon = data. indexof (conversionconstants. inbound_type_separator );
If (colon =-1)
{
Log. Error ("Missing: In conversion data (" + Data + '); // $ NON-NLS-1 $
Reply [localutil. inbound_index_type] = conversionconstants. type_string;
Reply [localutil. inbound_index_value] = data;
}
Else
{
Reply [localutil. inbound_index_type] = data. substring (0, colon );
Reply [localutil. inbound_index_value] = data. substring (colon + 1 );
}

Return reply;
}

/**
* The log stream
*/
Private Static final logger log = logger. getlogger (localutil. Class );

Private Static Boolean warn13 = false;

Private Static Boolean testeddecoder = false;

Private Static Method decode14 = NULL;
}

 

/******************************** Self-built class ***** **************************************** *****/

Package com. minght. SYS. util;

Import java. Io .*;

/**

@ Author minghaitang
* @ Version 1.0
*/

Public class mht_urldecoder {

// The Platform default encoding
Static string dfltencname = "GBK ";

Public static string decode (string s ){

String STR = NULL;

Try {
STR = decode (S, dfltencname );
} Catch (unsupportedencodingexception e ){
// The system shoshould always have the platform default
}

Return STR;
}

Public static string decode (string S, string ENC)
Throws unsupportedencodingexception {

Boolean needtochange = false;
Stringbuffer sb = new stringbuffer ();
Int numchars = S. Length ();
Int I = 0;

If (Enc. Length () = 0 ){
Throw new unsupportedencodingexception ("urldecoder: empty string ENC parameter ");
}

While (I <numchars ){
Char c = S. charat (I );
Switch (c ){
Case '+ ':
SB. append ('');
I ++;
Needtochange = true;
Break;
Case '% ':
/*
* Starting with this instance of %, process all
* Consecutive substrings of the form % xy. Each
* Substring % XY will yield a byte. Convert all
* Consecutive bytes obtained this way to whatever
* Character (s) they represent in the provided
* Encoding.
*/

Try {

// (Numchars-I)/3 is an upper bound for the number
// Of remaining bytes
Byte [] bytes = new byte [(numchars-I)/3];
Int Pos = 0;

While (I + 2) <numchars )&&
(C = '% ')){
Bytes [POS ++] =
(Byte) integer. parseint (S. substring (I + 1, I + 3), 16 );
I + = 3;
If (I <numchars)
C = S. charat (I );
}

// A trailing, incomplete Byte encoding such
// "% X" will cause an exception to be thrown

If (I <numchars) & (C = '% '))
Throw new illegalargumentexception (
"Urldecoder: incomplete trailing escape (%) pattern ");

SB. append (new string (bytes, 0, POs, ENC ));
} Catch (numberformatexception e ){
Throw new illegalargumentexception (
"Urldecoder: Illegal hex characters in escape (%) pattern -"
+ E. getmessage ());
}
Needtochange = true;
Break;
Default:
SB. append (C );
I ++;
Break;
}
}

Return (needtochange? SB. tostring (): S );
}
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.