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!