Use filters to solve JSP Chinese garbled characters

Source: Internet
Author: User

First, write a filter class, as shown below:

1 package COM. util; 2 3 Import Java. io. ioexception; 4 Import Java. io. unsupportedencodingexception; 5 6 Import javax. servlet. filter; 7 Import javax. servlet. filterchain; 8 Import javax. servlet. filterconfig; 9 Import javax. servlet. servletexception; 10 Import javax. servlet. servletrequest; 11 import javax. servlet. servletresponse; 12 Import javax. servlet. HTTP. httpservletrequest; 13 Import javax. servlet. HTTP. Httpservletrequestwrapper; 14 Import javax. servlet. HTTP. httpservletresponse; 15 Import javax. servlet. HTTP. httpsession; 16 17 18 // garbled Issue 19 public class myfilter implements filter {20 // replace the original request by writing a request, override the getparemeter method in it. You can set the encoding to 21 class myrequest extends httpservletrequestwrapper {22 23 @ override24 Public String getparameter (string PARAM) {25 string value = NULL; 26 try {27 // post2 8 super. setcharacterencoding (encoding); // convert the encoding to encoding29 value = super. getparameter (PARAM); 30 if (super. getmethod (). repeated signorecase ("get") {31 if (value! = NULL) {32 value = new string (value. getbytes ("iso8859-1"), encoding); 33} 34} 35} catch (unsupportedencodingexception e) {36 // todo auto-generated catch block37 E. printstacktrace (); 38} 39 return value; 40} 41 42 Public myrequest (httpservletrequest request) {43 super (request); 44} 45 46} 47 protected string encoding = NULL; 48 Public void destroy () {// destroy 49 // todo auto-generated method stub50 this. encod Ing = NULL; 51} 52 // convert the encoding Question 53 public void dofilter (servletrequest request, servletresponse response, 54 filterchain chain) throws ioexception, servletexception {55 // todo auto-generated method stub56 response. setcontenttype ("text/html; charset =" + encoding); 57 // filter Unlogged users 58 httpservletrequest Req = (httpservletrequest) request; 59 httpservletresponse resp = (httpservletresponse) response; 60 string Path = Req. getservletpath (); 61 string Param = Req. getquerystring (); 62 if (path! = NULL) {63 Path = path + "? "+ Param; // full Request Path 64} 65 if (path. endswith ("myaddress") | path. endswith ("myjingdong") | path. indexof ("myshoucang ")! =-1 | path. endswith ("updateuser") | path. indexof ("showorder ")! =-1 | path. indexof ("showvalidorder ")! =-1 | path. indexof ("showcancelorder ")! =-1 | path. indexof ("fillorder ")! =-1) {66 httpsession session = req. getsession (); 67 string username = (string) session. getattribute ("username"); 68 if (username = NULL) {69 session. setattribute ("url", path. replacefirst ("/", ""); 70 system. out. println (Session. getattribute ("url"); 71 resp. sendredirect ("user. do? OP = loginaction "); 72 return; 73} 74} 75 // send the filter to the next filter or resource processor 76 chain. dofilter (New myrequest (httpservletrequest) request), response); 77} 78 79 public void Init (filterconfig) throws servletexception {80 // todo auto-generated method stub81 this. encoding = filterconfig. getinitparameter ("encoding"); // encoding in the web. specify 82} 83 84} in XML}

Register and map the filter in Web. xml:

<filter>    <filter-name>EncodingFilter</filter-name>    <filter-class>com.util.MyFilter</filter-class>    <init-param>       <param-name>encoding</param-name>       <param-value>utf-8</param-value>    </init-param>  </filter> <filter-mapping>     <filter-name>EncodingFilter</filter-name>     <url-pattern>/*</url-pattern> </filter-mapping>

The filter myfilter class written above can only process data submitted by post (post is first processed and then received, and get is first received and then processed ).

However, when myfilter is used to filter any page, a credential is returned: the original client request is replaced with a custom request, that is, the internal class myrequest, however, this class inherits an httpservletrequestwrapper class.

In the custom internal class myrequest, a powerful function is implemented, that is, the getparameter () method of the request is rewritten. This method processes post submission and get submission. The returned value is the processed value. Therefore, this filter can handle post and get submission garbled characters!

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.