If you directly use Chinese characters by passing parameters in the url address bar, some may find that garbled characters have been received or the search engine has become messy in the past, next let's take a look at the correct method for cross-page Chinese Character transfer parameter transcoding in jsp.
Transcoding: a. href = "./showCont. jsp? Tcontent = "+ encodeURI (tcontent ));
Decoding: java.net. URLDecoder. decode (String) request. getParameter ("tcontent"), "UTF-8 ");
Example
A. jsp source code
The Code is as follows: |
Copy code |
<% @ Page contentType = "text/html; charset = gb2312" language = "java" import = "java. SQL. *" errorPage = "" %> <! DOCTYPE html PUBLIC "-// W3C // dtd xhtml 1.0 Transitional // EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <Html xmlns = "http://www.w3.org/1999/xhtml"> <Head> <Meta http-equiv = "Content-Type" content = "text/html; charset = gb2312"/> <Title> untitled document </title> </Head> <Body> <% String str_test = ""; %> <Form method = post action = "B. jsp? Test = <% = java.net. URLEncoder. encode (str_test) %> "> <Input type = "submit" value = "Submit" name = "submit"> </Form> </Body> </Html> |
B. jsp source code
The Code is as follows: |
Copy code |
<% @ Page contentType = "text/html; charset = gb2312" language = "java" import = "java. SQL. *" errorPage = "" %> <! DOCTYPE html PUBLIC "-// W3C // dtd xhtml 1.0 Transitional // EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <Html xmlns = "http://www.w3.org/1999/xhtml"> <Head> <Meta http-equiv = "Content-Type" content = "text/html; charset = gb2312"/> <Title> untitled document </title> </Head> <Body> <% String str = new String (request. getParameter ("test"). getBytes ("ISO8859_1 ")); %> <BR> <% = Str %> </Body> </Html> |