Java solve Chinese garbled problem __ Big Data

Source: Internet
Author: User
One: Service-side resolution client
<% request.setcharacterencoding ("GB18030")%>

The client-side coding settings (that is, the server receives the data sent by the browser) are resolved in a GB18030 way, but this is a bit bad, If I have 1000 pages (. jsp) that need to be set to write a repeat of this statement 1000, repeat the work, to solve this problem, here is the solution two: Client Resolution server

<%@ page language= "java" contenttype= "text/html; charset=gb18030 "pageencoding=" GB18030 "%>

The client resolves the encoding of the data returned by the server (that is, the way the page is rendered by the browser), and if one. html wants to be changed to. jsp, you must add this sentence three: Filter settings encoding

public class Charsetencodingfilter implements filter {private String encoding= ""; @Override public void Destroy () {//TODO auto-generated a stub} @Override public void DoF Ilter (ServletRequest request, Servletresponse Response,filterchain chain) throws IOException, Servletexception {/
        /Set up the character set to intercept request request.setcharacterencoding (this.encoding);
    Continue to carry on, this manifests the responsibility chain pattern Chain.dofilter (request, response); @Override public void init (Filterconfig filterconfig) throws Servletexception {//initialization, the configuration of the filter is read by parameter
    The value of the part, that is, the encoding of the reading setting this.encoding = Filterconfig.getinitparameter ("encoding"); }
}

A few things to note: Filter is new when Tomcat is started, and life is controlled by Tomcat filter only on post requests the filter actually intercepts the request and response requests. Filter can be seen as a crosscutting technology, the turn of the plane to filter to reflect the "responsibility chain mode" to implement the filter needs to implement the Javax.servlet.Filter interface, but also in the Web.xml file configuration filter provides a declarative service , declarative services are powerful, with pluggable capabilities
Declarative Services
only need to declare where to do what what, Rather than care about how to implement how (embodied in the Web.xml declaration to which file wher set that encoding what, specifically how to implement in the filter Class) programmatic services
need to express in concrete code where where to do what what, how to achieve how The
configuration file Web.xml is set as follows:

<filter>
        <filter-name>CharsetEncodingFilter</filter-name>
        <filter-class> Com.bjpowernode.drp.util.filter.charsetencodingfilter</filter-class> <!--specific Classpath-->  
        < init-param>
            <param-name>encoding</param-name>
            <param-value>gbk</param-value >
            <!--embodies the key-value pairs, set parameters, in the filter class can be based on encodign parameters, GBK-->   
        </init-param>
    </filter>

    <filter-mapping>
        <!--to all. JSP functions--> 
        <url-pattern>*.jsp</url-pattern>    
    </filter-mapping>

If you want to work with another file: add a mapping

    <filter-mapping>
        <filter-name>CharsetEncodingFilter</filter-name>
        <url-pattern >/servlet/*</url-pattern>
    </filter-mapping>

Matching file of a writing: accurate matching write complete path extension matching, with asterisk * and extension, such as *.jsp path prefix matching, including a directory and A/*, such as/servlet/*, but can not write/servlet/*.jsp all match, general use *
Execution process:

This does not have to be set in each. JSP, each time on the request server, will go first filter, set the code four: Get submit Chinese garbled

As mentioned above, filter only works on post submissions, so when get commits, how to set that, can only each. jsp write it again, of course not, get commit can consider changing the configuration of the server, For example, Tomcat needs to modify the Server.xml file under Conf, such as:

    <connector port= "8080" protocol= "http/1.1" connectiontimeout= "20000"
               redirectport= 
               "8443" uriencoding= "GB18030"/>
Use Java.net.URLEncoder.encode () in Chinese to encode, such as redirects when the URL has Chinese:
Response.sendredirect (Request.getcontextpath () + "/item_maint.jsp?errormessage=" + urlencoder.encode (ErrorMessage, "GB18030"));
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.