JSP ultimate solution to garbled code

Source: Internet
Author: User

Today, I found that the project had a Chinese garbled problem !? Remember to configure the page encoding and filter. How can this problem be solved?

The code displayed on the page is as follows:

 
<% @ Page Language = "Java" Import = "Java. util. *, pojo. *" pageencoding = "UTF-8" %>

The filter is also normal:

Because Ajax is also taken into account, it is a little different from the general filter, but the red part is indeed executed.

Public class encoding implements filter {private string charset; private string ajaxpostcontenttype; public static final string ajax_post_content_type_default = "application/X-WWW-form-urlencoded"; Public void destroy () {// todo auto-generated method stub} public void dofilter (servletrequest arg0, servletresponse arg1, filterchain arg2) throws ioexception, servletexception {// todo auto-generated met Hod stubhttpservletrequest request = (httpservletrequest) arg0; string requestsrc = request. getquerystring (); If (requestsrc! = NULL ){ Arg0.setcharacterencoding ("UTF-8"); arg1.setcharacterencoding ("UTF-8 "); Arg0.getparameter ("");} else {arg0.setcharacterencoding (charset); arg1.setcharacterencoding ("text/html; charset =" + charset);} arg2.dofilter (arg0, arg1 );} public void Init (filterconfig arg0) throws servletexception {// todo auto-generated method stubcharset = arg0.getinitparameter ("charset"); Signature = arg0.getinitparameter ("ajaxcontent "); if (ajaxpostcontenttype = NULL) {ajaxpostcontenttype = ajax_post_content_type_default ;}}}

The configuration in Web. XML is as follows:

 
<Filter> <filter-Name> encode </filter-Name> <filter-class> filter. encoding </filter-class> <init-param> <param-Name> charset </param-Name> <param-value> utf8 </param-value> </init- param> </filter> <filter-mapping> <filter-Name> encode </filter-Name> <URL-pattern>/* </url-pattern> </filter- mapping>

It seems that everything has been done. What is the problem? After some tests, the original problems were:

<Form ID = "trheaderform" name = "trheaderform" Action = "lagp. Do">

I checked it carefully and did not specify the submission method. Therefore, the get method is used by default! Changed:

 
<Form ID = "trheaderform" name = "trheaderform" Action = "lagp. Do" method = "Post">

Test OK!

Summary:

1. Ensure that the JSP page code is UTF-8

2. Configured filter, forcibly encoded as UTF-8

3. Set form submission method to post

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.