JSP uses filters to solve Chinese request garbled characters
(1) client data is generally submitted to the server through http get/POST, and request. getParameter ()
When reading parameters, Chinese characters may be garbled.
(2) Use a filter to solve the Chinese garbled request.
(3) The Code is as follows:
Package my; import java. io. *; import javax. servlet. *; import javax. servlet. http. *; public class ChineseFilter implements Filter {// defines a Filter implementation Filter interface private FilterConfig = null; public void init (FilterConfig config) throws ServletException {this. config = config;} public void destroy () {config = null;} public void doFilter (ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {request. setCharacterEncoding ("GB2312"); chain. doFilter (request, response); // forward the filtered request object to the next filter }}
(4) Deploy the filter. Edit the WEB-INF \ web. xml file and add the following:
cf
my.ChineseFilter
cf
/*
REQUEST
FORWARD
INCLUDE
Here It is mainly used with RequestDispatcher.
1. When the value is REQUEST, the filter can be activated only when a REQUEST comes directly from the client. If the REQUEST comes from RequestDispatcher. forward, It is not activated;
2. If the value is FORWARD, the filter is activated if the request is from RequestDispatcher. forward;
3. When the value is INCLUDE, this filter is activated if the request is from RequestDispatcher. include;
4. When the value is "ERROR", it indicates that this filter is activated only when the request is from RequestDispatcher using the "ERROR information page;
5. The default value is REQUEST.
(5) create a jsp page for verification
<% @ Page contentType = "text/html; charset = gb2312" language = "java" import = "java. SQL. *" errorPage = "" %>
Untitled document<% String s = request. getParameter ("data"); out. print (s); %>
(6) OK! We hope you have succeeded!