Detailed solution for using the filter in JSP for content encoding, jsp Filter

Source: Internet
Author: User

Detailed solution for using the filter in JSP for content encoding, jsp Filter

Solution to using filters for content encoding in JSP

Problem

When records are inserted to the database through the JSP page, the new records inserted to the database may be garbled due to JSP page encoding. Therefore, you need to encode the content on the JSP page to ensure that it is consistent with the encoding in the database.

Solution

Use the JSP filter for processing. The procedure is as follows:

1. Create a servlet to implement the javax. servlet. Filter interface.

2. Modify the Servlet/JSP Mapping URL and change it to/EncodingFilter.

3. Implement Chinese filtering of JSP content in the doFilter method of EncodingFilter. java

Public void doFilter (ServletRequest request, ServletResponse response, FilterChain filterChain) throws IOException, ServletException {// filter Chinese requests. setCharacterEncoding ("UTF-8"); filterChain. doFilter (request, response );}

4. Modify the XML file. The default configuration file generated by Myeclipse cannot meet the requirements. You need to make the following changes:

The following is the code automatically generated by Myeclipse:

<Servlet> <description> chinese encoding </description> <display-name> This is the display name of my J2EE component </display-name> <servlet-name> EncodingFilter </ servlet-name> <servlet-class> com. nichao. filter. encodingFilter </servlet-class> </servlet> <servlet-mapping> <servlet-name> EncodingFilter </servlet-name> <url-pattern>/EncodingFilter </url-pattern> </servlet-mapping> --> ''' change it to ''' <filter> <filter-name> EncodingFilter </filter-name> <filter-class> com. sina. filter. encodingFilter </filter-class> </filter> <filter-mapping> <filter-name> EncodingFilter </filter-name> <url-pattern>/* </url-pattern> </filter-mapping>

5. Now, you can filter the JSP page content in Chinese.

If you have any questions, please leave a message or go to the community on this site to discuss and make progress together. Thank you for reading this article and hope to help you. Thank you for your support for this site!

Related Article

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.