A method to solve the garbled problem of Java EE _java

Source: Internet
Author: User

Garbled is a more common problem in the Java EE. If you encounter one or two problems, you can use the new String (Request.getparameter (XXX). GetBytes ("Iso-8859-1"), "UTF-8") to resolve. When you encounter many situations, it is best to use a filter.
The filter only needs to notice 2 places--Class and Web.xml
1. The publication of the above Web.xml is as follows:

<fileter> 
  <!--class name--> 
  <filter-name>SetCharsetEncodingFilter</filter-name> 
  <!---Class path--> 
  <filter-class>SetCharacter</filter-class> 
  <init-param> 
    < param-name>encoding</param-name> 
    <param-value>utf-8</param-value> 
  </ init-param> 
  <filter-mapping> 
    <filter-name>setcharsetencodingfilter</filter-name > 
    <!--setup All files encountered filters must be intercepted--> 
    <url-pattern>/*</url-pattern> 
  </ Filter-mapping> 
</fileter> 

2. Filtration Class

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 Setcharacter implements Filter {protected String encoding = NULL; 
  protected Filterconfig filterconfig = null; 
  Protected Boolean ignore = true; 
    public void init (Filterconfig arg0) throws servletexception {this.encoding = Arg0.getinitparameter ("encoding"); 
    String value = Arg0.getinitparameter ("Imnore"); 
    if (value = = null) {This.ignore = true; 
    else if (Value.equalsignorecase ("true") {This.ignore = true; 
    else if (value.equalsignorecase ("yes")) {This.ignore = true; } public void Dofilter (ServletRequest arg0, Servletresponse arg1, Filterchain arg2) throws IOException, S ervletexception {if (Ignore | | (arg0.getcharacterencoding () = null)) {String encoding = selectencoding (arg0); 
    if (encoding!= NULL) arg0.setcharacterencoding (encoding); 
  } arg2.dofilter (arg0, arg1); 
  Private String selectencoding (ServletRequest arg0) {return (this.encoding); 
    public void Destroy () {this.encoding = null; 
  This.filterconfig = null; } 
 
}

In the Web.xml file, the following syntax is used to define mappings:
1, the "/" at the beginning and the "/*" End is used to do path mapping.
2, before the prefix "*." The beginning is used to do extension mapping.
3, with "/" is used to define the default servlet mapping.
4, the rest is used to define detailed mapping. For example:/aa/bb/cc.action

The above is to solve the Java EE garbled problem of ideas, to share with everyone, I hope that you encounter similar problems can be successfully resolved.

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.