In JSP, how does one solve garbled characters when passing parameters through the get method in Chinese?

Source: Internet
Author: User

During web development, although the encodingfilter is added to the web. xml file<Filter> <br/> <filter-Name> encodingfilter </filter-Name> <br/> <filter-class> Org. springframework. web. filter. characterencodingfilter </filter-class> <br/> <init-param> <br/> <param-Name> encoding </param-Name> <br/> <param-Value> UTF-8 </param-value> <br/> </init-param> <br/> </filter> <br/> <filter-mapping> <br/> <filter-Name> encodingfilter </filter-Name> <br/> <URL-pattern> *. shtml </url-pattern> <br/> </filter-mapping>

This allows you to use Chinese characters when adding data, but when you update and delete a category, because the categoryid is transmitted through the URL using the get method,
At this time, if categoryid is Chinese, garbled characters will be displayed when passed to the action through URL, and exceptions will occur, interrupting the program.

Solution

Write a tool class

Public class stringdecode {</P> <p> Public static string decode (string value) throws exception <br/>{< br/> try {<br/> If (value = NULL) return NULL; <br/> return new string (value. getbytes ("ISO-8859-1"), "UTF-8"); <br/> // Note: The UTF-8 here depends on the encoding of the submitted page. <br/>}< br/> catch (exception ex) {<br/> return value; <br/>}< br/>

 

Then, call the decode method in the action.String id = stringdecode. Decode (request. getparameter ("categoryid "));

 

The obtained ID will not be garbled.

From http://gforge.ce-oss.com/mwiki/index.php/Jsp%E4%B8%AD%E9%80%9A%E8%BF%87get%E6%96%B9%E6%B3%95%E4%BC%A0%E9%80%92%E5%8F%82%E6%95%B0%E4%B8%BA%E4%B8%AD%E6%96%87%E6%97%B6%E5%87%BA%E7%8E%B0%E4%B9%B1%E7%A0%81%E5%A6%82%E4%BD%95%E8%A7%A3%E5%86%B3%EF%BC%9F" There is another method:Modify the Tomcat server. the XML file is as follows: <connector uriencoding = "UTF-8" connectiontimeout = "20000" Port = "8080" protocol = "HTTP/1.1" redirectport = "8443"/> This method is relatively simple, I tried it. No problem.
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.