garbled problems in Java

Source: Internet
Author: User

1 if the Tomcat server is used, add uriencoding= "UTF-8" after connector the label in Server.xml;

2 Using the Web filter:

(1), create a new class of Setcharacterencodingfilter.java:


Package com.util;
Import java.io.IOException;
Import javax.servlet.*;

public class Setcharacterencodingfilter implements filter{
protected String encoding = NULL;

protected Filterconfig filterconfig =null;

Protected Boolean ignore = true;

public void Destroy ()
{
this.encoding = null;
This.filterconfig = null;

}

 public void DoFilter (servletrequest request,servletresponse response,
     Filterchain chain) throws IOException, Servletexception
 {
    if (ignore| | (request.getcharacterencoding () = = null))
    {
    string encoding = selectencoding (request);
    if (encoding! = null)
     request.setcharacterencoding ( encoding);
   }
   chain.dofilter (request, response);
 }

public void init (Filterconfig filterconfig) throws Servletexception
{
This.filterconfig = Filterconfig;
Get initialization parameters
this.encoding = Filterconfig.getinitparameter ("encoding");
String value= filterconfig.getinitparameter ("ignore");
if (value ==null)
{
This.ignore = true;
} else if (Value.equalsignorecase ("true"))
{
This.ignore = true;
} else if (Value.equalsignorecase ("yes"))
{
This.ignore = true;
} else
This.ignore = false;
}

Protected String selectencoding (servletrequestrequest)
{
return (this.encoding);
}


}

(2) added in Web-app in Web. xml:

<!--define a filter and set its initialization parameters--><filter>
<filter-name>set characterencoding</filter-name>
<filter-class>com.util.SetCharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>GB2312</param-value>
</init-param>
</filter>
<!--to develop a filter map--
<filter-mapping>
<filter-name>set characterencoding</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>

3.<%@ page language= "java" import= "java.util.*,com.scce.entity.*" pageencoding= "Utf-8"%>

  <Meta http-equiv= "content-type" content= "text/html; Charset=utf-8 ">

4.String name=new String (request.getparameter ("name"). GetBytes ("Iso8859-1"), "GBK");

New String (Filename.getbytes ("UTF-8"), "GBK")

5.request.setcharacterencoding ("GBK");

Response.setcontenttype ("TEXT/HTML;CHARSET=GBK");

garbled problems in Java

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.