Solve the Problem of garbled Chinese data input to the database

Source: Internet
Author: User
For example, when data is entered in the SQL Server database during the JSP code test, all the entered data is garbled and then the data is queried to solve the problem.
It's easy to add <% request. setcharacterencoding ("gb2312"); %>.

Or set a filter

/*
* To change this template, choose tools | templates
* And open the template in the editor.
*/

Package jdbcbook. Pub. filters;

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;

/**
* Set the correct encoding format for Tomcat
* @ Author Administrator
*/
Public class setcharacterencodingfilter implements filter {

Protected string encoding = NULL;
Protected filterconfig = NULL;
Protected Boolean ignore = true;

// Initialization method, set the identifier variable through the configured parameters
Public void Init (filterconfig) throws servletexception {
This. filterconfig = filterconfig;
This. Encoding = filterconfig. getinitparameter ("encoding ");
String value = filterconfig. getinitparameter ("Ignore ");
If (value = NULL)
This. Ignore = false;
Else if (value. inclusignorecase ("false "))
This. Ignore = true;
Else if (value. inclusignorecase ("no "))
This. Ignore = true;
Else
This. Ignore = false;
}

Public void dofilter (servletrequest request, servletresponse response, filterchain chain) throws ioexception, servletexception {
// Determine whether encoding is required
If (! Ignore | (request. getcharacterencoding () = NULL ))
{
// Obtain the encoding type in the configuration file
String encoding = selectencoding (request );
If (encoding! = NULL)
Request. setcharacterencoding (encoding );
}
Chain. dofilter (request, response );
}

Public void destroy (){
This. Encoding = NULL;
This. filterconfig = NULL;
}

// Obtain the configured encoding type
Protected string selectencoding (servletrequest request)
{
Return this. Encoding;
}
}

Configure in Web. xml

<Filter>
<Filter-Name> set character encoding </filter-Name>
<Filter-class> jdbcbook. Pub. Filters. setcharacterencodingfilter </filter-class>
<Init-param>
<Param-Name> encoding </param-Name>
<Param-value> GBK </param-value>
</Init-param>
</Filter>
<Filter-mapping>
<Filter-Name> set character encoding </filter-Name>
<URL-pattern>/* </url-pattern>
</Filter-mapping>

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.