JSP uses filters to solve Chinese request garbled characters. jsprequest

Source: Internet
Author: User

JSP uses filters to solve Chinese request garbled characters. jsprequest

(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"> 


(6) OK! We hope you have succeeded!


Why can't JSP solve Chinese garbled characters with filters? The Code is as follows:

Do I have to use a filter?
Just use UTF-8 directly.
Jsp character encoding UTF-8
MyEclipse is also set to UTF-8

How to solve jsp Chinese garbled characters

When the program encounters a problem, you should find the problem step by step,
First, you must set the method to Post for form-based submission. The get method cannot pass Chinese characters.
Second, when receiving data, you need to set request. setCharacterEncoding ("gbk ");
Get the passed parameter, String name = request. getParameter ("name ");
Here we can check whether Chinese characters can be correctly transmitted. If so, we can look at the insert statement last inserted into mysql.
Follow the steps above to test, if you can not solve it, you can contact my tianligen@msn.com

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.