I am very unfamiliar with java,jsp and other related pages, today in order to modify others left behind in the Java background HTML nested JSP to do the interface of some content in the project was tortured. When I pass in the text or textarea, the GetParameter method always gets a bunch? , no one can ask, had to look blindly on the Internet. There are many ways to change the JSP code on the Internet, there are a lot of code in Java to do a variety of conversion, tried and tried, no use. May be successful, but my situation is that the current JSP reference other JSP, but the other JSP from GBK to Utf-8 after the reported html-500 error, in order not to cause more unknown errors, I gave up the attempt to modify the direction of the interface encoding. Then in Csdn a post in the high score of the 18 floor of the solution to help me, in order to avoid forgetting, I will refer to this post, address http://bbs.csdn.net/topics/390360298. Let's take a look at this magical class:
1 Public classHttprequestreader {2 3Hashtable pairs =NewHashtable ();4 5 /**6 * Rawparameterreader constructor comment.7 */8 PublicHttprequestreader (httpservletrequest request, String encoding)9 throwsIOException {Ten Super(); One Parse (request.getquerystring (), encoding); A Parse (Request.getreader (). ReadLine (), encoding); - } - the Public StaticString decode (string s, string encoding)throwsException { -StringBuffer SB =NewStringBuffer (); - for(inti = 0; I < s.length (); i++) { - Charc =S.charat (i); + Switch(c) { - Case+: +Sb.append (' '); A Break; at Case‘%‘: - Try { -Sb.append ((Char) Integer.parseint ( -S.substring (i + 1, i + 3), 16)); -}Catch(NumberFormatException e) { - Throw Newillegalargumentexception (); in } -i + = 2; to Break; + default: - Sb.append (c); the Break; * } $ }Panax Notoginseng //Undo conversion to external encoding -String result =sb.tostring (); the byte[] inputbytes = Result.getbytes ("8859_1"); + return NewString (inputbytes, encoding); A } the + /** - * Insert The method ' s description here. Creation Date: (2001-2-4 17:30:59) $ * $ * @returnjava.lang.String - * @paramname - * java.lang.String the */ - Publicstring GetParameter (string name) {Wuyi if(Pairs = =NULL|| !Pairs.containskey (name)) the return NULL; - return(String) (((ArrayList) pairs.get (name)). Get (0)); Wu } - About /** $ * Insert The method ' s description here. Creation Date: (2001-2-4 17:28:17) - * - * @returnjava.util.Enumeration - */ A PublicEnumeration Getparameternames () { + if(Pairs = =NULL) the return NULL; - returnPairs.keys (); $ } the the /** the * Insert The method ' s description here. Creation Date: (2001-2-4 17:33:40) the * - * @returnjava.lang.string[] in * @paramname the * java.lang.String the */ About Publicstring[] Getparametervalues (String name) { the if(Pairs = =NULL|| !Pairs.containskey (name)) the return NULL; theArrayList Al =(ArrayList) pairs.get (name); +String[] values =Newstring[al.size ()]; - for(inti = 0; i < values.length; i++) theValues[i] =(String) al.get (i);Bayi returnvalues; the } the - /** - * Insert The method ' s description here. Creation Date: (2001-2-4 20:34:37) the * the * @paramUrlenc the * java.lang.String the */ - Private voidParse (string urlenc, string encoding) the throwsjava.io.IOException { the if(Urlenc = =NULL) the return;94StringTokenizer tok =NewStringTokenizer (Urlenc, "&"); the Try { the while(Tok.hasmoretokens ()) { theString Apair =Tok.nexttoken ();98 intpos = apair.indexof ("="); AboutString name =NULL; -String value =NULL;101 if(pos! =-1) {102Name = Decode (apair.substring (0, POS), encoding);103Value = Decode (apair.substring (pos + 1), encoding);104}Else { theName =Apair;106Value = "";107 }108 if(Pairs.containskey (name)) {109ArrayList values =(ArrayList) pairs.get (name); the Values.add (value);111}Else { theArrayList values =NewArrayList ();113 Values.add (value); the pairs.put (name, values); the } the }117}Catch(Exception e) {118 Throw Newjava.io.IOException (E.getmessage ());119 } - }121}
After I introduced my Java project, I added the following packages:
1 Import java.io.IOException; 2 Import java.util.ArrayList; 3 Import java.util.Enumeration; 4 Import java.util.Hashtable; 5 Import Java.util.StringTokenizer; 6 7 import javax.servlet.http.HttpServletRequest;
So that it can be used.
Make the application where it is needed:
1 Public voidSave (HttpServletRequest req, HttpServletResponse resp)2 throwsservletexception, IOException {3 Try {4Httprequestreader Paramreader =NewHttprequestreader (req, "GBK");5String stralertmsg = Paramreader.getparameter ("Device. Alertmsg ");6}Catch(Exception e) {7 This. Findforward ("/device-details.jsp",true, req, resp);8 return;9 }Ten This. Findforward ("/device-details.jsp",true, req, resp); One return; A}
And then it's OK!
JSP must be GBK encoded when Java code getparameter get Chinese garbled solution