Java garbled Processing

Source: Internet
Author: User

Tomcat configuration code:

Locate the server. XML for conf In the Tomcat directory and add uriencoding = "UTF-8" to the connector Node"

<Connector Port = "80" maxhttpheadersize = "8192" maxthreads = "150"
Minsparethreads = "25" maxsparethreads = "75" enablelookups = "false"
Redirectport = "8443" acceptcount = "100" connectiontimeout = "20000"
Disableuploadtimeout = "true"Uriencoding = "UTF-8 "/>

Use global Filter

 Package  Com. bjdata. Favorite. servlet;  Import  Java. Io. ioexception;  Import  Javax. servlet. filter;  Import Javax. servlet. filterchain;  Import  Javax. servlet. filterconfig;  Import  Javax. servlet. servletexception;  Import  Javax. servlet. servletrequest;  Import  Javax. servlet. servletresponse;  Public   Class Globalfilter Implements  Filter {  Public  Void  Destroy (){}  Public   Void  Dofilter (servletrequest request, servletresponse response, filterchain chain)  Throws  Ioexception, servletexception {request. setcharacterencoding ( UTF-8 "); //  Set Encoding  Chain. dofilter (request, response );}  Public   Void Init (filterconfig)Throws  Servletexception {}} 

Configure Filter

<? XML version = "1.0" encoding = "UTF-8"?> <Web-app version = "2.4" xmlns = "http://java.sun.com/xml/ns/j2ee"Xmlns: xsi= "Http://www.w3.org/2001/XMLSchema-instance"Xsi: schemalocation= "Http://java.sun.com/xml/ns/j2ee http://Java.sun.com/xml/ns/j2ee/web-app_2_4.xsd "><Filter> <filter-Name> globalfilter </filter-Name> <filter-Class> Com. bjdata. Favorite. servlet. globalfilter </filter-Class> </Filter> <filter-mapping> <filter-Name> globalfilter </filter-Name> <URL-pattern>/*</Url-pattern> </filter-mapping> </Web-app>

 

Configure encoding in struts

 
<? XML version = "1.0" encoding = "UTF-8"?> <! Doctype struts public "-// Apache Software Foundation // DTD struts configuration 2.1 // en" "http://struts.apache.org/dtds/struts-2.1.dtd"> <struts> <constant name = "struts. i18n. encoding "value =" UTF-8 "/> </struts>

Garbled characters are returned in JSON format:

Response. setcharacterencoding ("UTF-8 ");

 

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.