Solve the Problem of passing parameters in the URI of JSP

Source: Internet
Author: User
Java Code
  1. <%!PublicString gbtoiso (string Str)
  2. {Try{ByteTemp [] = Str. getbytes ("Gb2312");
  3. STR =NewString (temp,ISO-8859-1");
  4. ReturnSTR;
  5. }Catch(Exception e ){ReturnSTR ;}}
  6. Response. sendredirect (gbtoiso ("Errmsg. <SPAN class = hilite1> JSP </span>? Errmsg = customer information added"); %>
<%! Public String gbtoiso (string Str) {try {byte temp [] = Str. getbytes ("gb2312"); STR = new string (temp, "ISO-8859-1"); Return STR;} catch (exception e) {return STR ;}} response. sendredirect (gbtoiso ("errmsg.JSP? Errmsg = customer information added "); %>

In the hyperconnection, the priority sion isChinese

Java code
  1. <A href ="Cust_totallist. <SPAN class = hilite1> JSP </span>? Action = Delete & page = <% = intcurrentpage %> & cust_id = <% = Rs. getint ("ID") %> & Amp; customization sion = <% = java.net. <SPAN class = hilite2> URL </span> encoder. encode (partition Sion ,"Iso-8859-1") %>"> Delete </a>
  2. ///// // Cust_totallist. <SPAN class = hilite1> JSP </span>
  3. string compression sion = java.net. class = hilite2> URL decoder. decode (request. getparameter ( " regression Sion " ). trim (), " ISO-8859-1 " );
<A href = "cust_totallist.JSP? Action = Delete & page = <% = intcurrentpage %> & cust_id = <% = Rs. getint ("ID") %> & define sion = <% = java.net.URLEncoder. encode (plugin Sion, "ISO-8859-1") %> "> Delete </a> // cust_totallist.JSPReturns the string extension sion = java.net.URLDecoder. Decode (request. getparameter ("visibility Sion"). Trim (), "ISO-8859-1 ");

Visible,URLEncoding format in ISO-8859-1, processingChineseJust convert the encoding format to the ISO-8859-1
Method 1:
Http://xxx.do? Ptname = 'I am a China'

Java code
    1. String strptname = request. getparameter ("Ptname");
    2. Strptname =NewString (strptname. getbytes (ISO-8859-1"),UTF-8");
 
String strptname = request. getparameter ("ptname"); strptname = new string (strptname. getbytes ("ISO-8859-1"), "UTF-8 ");

Method 2:

Java code
  1. <% @ Page contenttype ="Text/html; charset = gb2312"%>
  2. <A href ="Ds. <SPAN class = hilite1> JSP </span>? <SPAN class = hilite2> URL </span> = <% = java.net. <SPAN class = hilite2> URL </span> encoder. encode ("It is encoded here","Gb2312") %>"> Click here </a>
  3. <%
  4. // Request. setcharacterencoding ("GBK ");
  5. If(Request. getparameter ("<SPAN class = hilite2> URL </span>")! =Null)
  6. {
  7. STR = request. getparameter ("<SPAN class = hilite2> URL </span>");
  8. STR = java.net. <SpanClass= Hilite2> URL </span> decoder. Decode (STR,"Gb2312");
  9. STR =NewString (Str. getbytes (ISO-8859-1"));
  10. Out. Print (STR );
  11. }
  12. %>
<% @ Page contenttype = "text/html; charset = gb2312" %> <a href = "Ds.JSP?URL= <% = Java.net.URLEncoder. encode ("encode here", "gb2312") %> "> click here </a> <% // request. setcharacterencoding ("GBK"); If (request. getparameter ("URL")! = NULL) {STR = request. getparameter ("URL"); STR = java.net.URLDecoder. Decode (STR, "gb2312"); STR = new string (Str. getbytes ("ISO-8859-1"); Out. Print (STR);} %>

 

Java code
  1. PublicString chinatostring (string Str)
  2. {
  3. String S = STR;
  4. Try
  5. {
  6. ByteTempb [] = S. getbytes (ISO-8859-1");
  7. S =NewString (tempb );
  8. ReturnS;
  9. }
  10. Catch(Exception E)
  11. {
  12. ReturnS;
  13. }
  14. }
 
Public String chinatostring (string Str) {string S = STR; try {byte tempb [] = S. getbytes ("ISO-8859-1"); s = new string (tempb); Return s;} catch (exception e) {return s ;}}

 

Java code
  1. Function <SpanClass= Hilite2> URL </span> encode (SSTR)
  2. {
  3. ReturnEscape (SSTR ).
  4. Replace (/\ +/g,'% 2B').
  5. Replace (/\ "/g,'% 22').
  6. Replace (/\'/G ,'%27').
  7. Replace (/\// G, '% 2f ');
  8. }
 
FunctionURLEncode (SSTR) {return escape (SSTR ). replace (/\ +/g, '% 2B '). replace (/\ "/g, '% 22 '). replace (/\ '/g,' % 27 '). replace (// G, '% 2f ');}

Method 3:
If jstl is used, you can write an El function and callURLEncoder. encode.

IE default pairURLThe following parameters are not encoded, but Tomat is performed by ISO8859-1 by default.URLTherefore, the above error occurs. A good practice is:
1.URLParameters to ensure that the use of UTF-8 encoding, the method can use JS functions encodeuri (), or call the custom El function;
2. Set connector in server. XML to familiarize themselves with uriencoding = "UTF-8" to ensure that the decoding format is consistent with the encoding format;

Method 4:

XML Code
  1. <Script>
  2. For (VARI=0; I<Document. Links. Length; I ++ ){
  3. Document. Links [I]. Href=Encodeuri(Document. Links [I]. href );
  4. }
  5. </Script>
<SCRIPT> for (VAR I = 0; I <document. links. length; I ++) {document. links [I]. href = encodeuri (document. links [I]. href) ;}</SCRIPT>

In action, string S = request. getparameter ("S ");
S = new string (S. getbytes ("iso-8859-1"), "GBK ");

The above method is to collect the solutions mentioned by some netizens.

Related Article

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.