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>