JSP uses filters to solve Chinese request garbled characters,
This article shares with you the question of using a filter in JSP to solve Chinese request garbled characters. The specific content is as follows:
(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:
<filter> <filter-name>cf</filter-name> <filter-class>my.ChineseFilter</filter-class> </filter> <filter-mapping> <filter-name>cf</filter-name> <url-pattern>/*</url-pattern> <dispatcher>REQUEST</dispatcher> <dispatcher>FORWARD</dispatcher> <dispatcher>INCLUDE</dispatcher> </filter-mapping>
The <dispatcher> </dispatcher> here 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 = "" %> <! DOCTYPE html PUBLIC "-// W3C // dtd xhtml 1.0 Transitional // EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
The above is the method for JSP to solve the problem of Chinese request garbled characters. I hope it will be helpful for your learning.
Articles you may be interested in:
- Jsp Chinese garbled jsp mysql garbled Solution
- JSP XMLHttpRequest: dynamically refresh and process Chinese garbled characters
- Solution to garbled Chinese Characters in jsp hyperlinks
- JSP + MySQL Chinese garbled problem post submit garbled Solution
- Solve Chinese garbled characters in Post and Request of JSP Through Filter
- JSP uses filters to solve Chinese garbled requests getParameter