Summary of Java Web application Chinese garbled processing

Source: Internet
Author: User

four ways to handle server garbled
1.jsp Encoding setting error causes garbled charactersuse the page command in a JSP file to specify the MIME type of the response result, such as <%@ page language= "java" contenttype= "Text/html;charset=utf-8"%>
2. When the request parameter is accepted, the encoding format is set in the response responserequest.setcharacterencoding ("UTF-8" )response.setcharacterencoding ("UTF-8")
3. Handling garbled characters by using string to set character setsindexstring = Java.net.URLDecoder.decode (indexstring, "UTF-8");indexstring = new String (indexstring.getbytes ("UTF-8"), "UTF-8");
4. Using Custom Filters
Package com.cdu.yige.filter;
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;
Import Javax.servlet.http.HttpServletRequest;

public class Encodingfilter implements Filter {
Private String charset = "UTF-8";
@Override
public void Destroy () {}
@Override
public void DoFilter (ServletRequest request, servletresponse response,
Filterchain chain) throws IOException, Servletexception {
HttpServletRequest req = (httpservletrequest) request;
if (Req.getmethod (). Equalsignorecase ("GET")) {
if (! ( Req instanceof Getrequest)) {
req = new Getrequest (req, CharSet);//Processing GET request encoding
}
} else {
Req.setcharacterencoding (charset);//processing Post request encoding
}
Chain.dofilter (req, response);
}

@Override
public void init (Filterconfig fconfig) throws Servletexception {
String charset = Fconfig.getinitparameter ("charset");
if (charset! = null &&!charset.isempty ()) {
This.charset = CharSet;
}
}
} filtering for Get modePackage com.cdu.yige.filter;
Import java.io.UnsupportedEncodingException;
Import Java.util.Map;
Import Javax.servlet.http.HttpServletRequest;
Import Javax.servlet.http.HttpServletRequestWrapper;
/**
* Handle the GET request parameters!
* @author Lucien
*
*/
public class Getrequest extends Httpservletrequestwrapper {
private HttpServletRequest request;
Private String CharSet;

Public Getrequest (HttpServletRequest request, String CharSet) {
Super (Request);
This.request = Request;
This.charset = CharSet;
}

@Override
public string GetParameter (string name) {
Get parameters
String value = request.getparameter (name);
if (value = = null) return null;//if NULL, returns null directly
try {
Returns after encoding a parameter
return new String (Value.getbytes ("Iso-8859-1"), CharSet);
} catch (Unsupportedencodingexception e) {
throw new RuntimeException (e);
}
}

@SuppressWarnings ({"Unchecked", "Rawtypes"})
@Override
Public Map Getparametermap () {
map<string,string[]> map = Request.getparametermap ();
if (map = = null) return map;
Traverse map to encode each value
For (String Key:map.keySet ()) {
String[] values = Map.get (key);
for (int i = 0; i < values.length; i++) {
try {
Values[i] = new String (values[i].getbytes ("Iso-8859-1"), CharSet);
} catch (Unsupportedencodingexception e) {
throw new RuntimeException (e);
}
}
}
return after processing
return map;
}
@Override
Public string[] Getparametervalues (String name) {
String[] values = super.getparametervalues (name);
for (int i = 0; i < values.length; i++) {
try {
Values[i] = new String (values[i].getbytes ("Iso-8859-1"), CharSet);
} catch (Unsupportedencodingexception e) {
throw new RuntimeException (e);
}
}
return values;
}
}
Filter Web. xml file Configuration< Filter >           <Filter-name>Encodingfilter</Filter-name>           <Filter-class>Com.cdu.yige.filter.EncodingFilter</Filter-class>      </ Filter>      < filter-mapping>            <Filter-name>Encodingfilter</Filter-name>           <Url-pattern>/*</Url-pattern>      </ filter-mapping>
5. Modify the character set configuration of the Tomcat server Server.xml, the final processing of the Get mode and post mode garbled. <connector port= "8080" protocol= "http/1.1"
connectiontimeout= "20000"
Uriencoding= "UTF-8"
Usebodyencodingforuri= "true"
redirectport= "8443"/>
6.get Request mode and Post request method generated by the garbled processing method slightly different, please Baidu see. Other methods see: Http://wenku.baidu.com/view/f27afc4bfe4733687e21aa6c.html http://blog.csdn.net/pcxbest/article/details/ 24418303 http://my.oschina.net/u/267081/blog/137383 http://huifeideyu.blog.51cto.com/6764601/1176432/http blog.163.com/shuangyuehuixin%40126/blog/static/16276137420113242322311/http://www.iteye.com/topic/251743

Summary of Java Web application Chinese garbled processing

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.