About the garbled characters after submitting a Chinese form to the actionform in struts.

Source: Internet
Author: User

Because the Struts framework directly sends the form data to the actionform, there is no setcharacterencoding for httprequestservlet, so the default is according to the ISO-8859-1 (see Org. apache. catalina. connector. protected void parseparameters () method in httprequestbase ),

The solution is to encode the request before the form is submitted to actionform.
The first method is to write a filter to filter all requests.

Filter code:
Package JP. co. Ricoh. gtis. Others. profile. filters;

 

Import java. Io. ioexception;

Import javax. servlet. filter;
Import javax. servlet. filterchain;
Import javax. servlet. filterconfig;
Import javax. servlet. servletexception;
Import javax. servlet. servletrequest;
Import javax. servlet. servletresponse;

Public class setcharacterencodingfilter implements filter {
Private string encoding;
Public void Init (filterconfig) throws servletexception {
// Todo auto-generated method stub
This. Encoding = filterconfig. getinitparameter ("encoding ");
}

Public void dofilter (servletrequest request, servletresponse response,
Filterchain chain) throws ioexception, servletexception {
// Todo auto-generated method stub
Request. setcharacterencoding (this. Encoding );
Chain. dofilter (request, response );
}

Public void destroy (){
// Todo auto-generated method stub

}

}

 

Configuration File web. xml
<Filter>
<Filter-Name> setcharacterencodingfilter </filter-Name>
<Filter-class> JP. co. Ricoh. gtis. Others. profile. Filters. setcharacterencodingfilter </filter-class>
<Init-param>
<Param-Name> encoding </param-Name>
<Param-value> shift_jis </param-value>
</Init-param>
</Filter>

<Filter-mapping>
<Filter-Name> setcharacterencodingfilter </filter-Name>
<URL-pattern>/* </url-pattern>
</Filter-mapping>

 

The second method is to replace the default controller org. Apache. Struts. Action. actionservlet.

 

Subclass code:
Package JP. co. Ricoh. gtis. Others. profile. controllers;

 

Import java. Io. ioexception;

Import javax. servlet. servletexception;
Import javax. servlet. http. httpservletrequest;
Import javax. servlet. http. httpservletresponse;

Import org. Apache. Struts. Action. actionservlet;

Public class setencodingactionservlet extends actionservlet {

Protected void process (httpservletrequest request, httpservletresponse response) throws ioexception, servletexception {
// Todo auto-generated method stub
String encoding = getinitparameter ("encoding ");
Request. setcharacterencoding (encoding );
Super. Process (request, response );
}

}

 

Configuration File web. xml
<Servlet>
<Servlet-Name> testaction </servlet-Name>
<Servlet-class> JP. co. Ricoh. gtis. Others. profile. controllers. setencodingactionservlet </servlet-class>
<Init-param>
<Param-Name> config </param-Name>
<Param-value>/WEB-INF/struts-config.xml </param-value>
</Init-param>
<Init-param>
<Param-Name> encoding </param-Name>
<Param-value> shift_jis </param-value>
</Init-param>
<Load-on-startup> 2 </load-on-startup>
</Servlet>

<Servlet-mapping>
<Servlet-Name> testaction </servlet-Name>
<URL-pattern> *. testdo </url-pattern>
</Servlet-mapping>

In this example, all data requested through *. testdo is encoded by the value set by the encoding parameter.

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.