Rewrite request parameter with Httpservletrequestwrapper

Source: Internet
Author: User

Purpose: Change the value of the request parameter to meet the requirements of the project (e.g., request to filter the request lang! = en)

Method: 1. Rewrite with Httpservletrequestwrapper

1  Public classChangerequestwrapperextendsHttpservletrequestwrapper {2     PrivateMap<string, string[]> Parametermap;//Map Collection of all parameters3 4      PublicChangerequestwrapper (HttpServletRequest request) {5         Super(request);6Parametermap =Request.getparametermap ();7     }8 9     //rewrite the methods in several HttpservletrequestwrapperTen  One     /** A * Get all parameter names -      * -      * @returnreturns all parameter names the      */ - @Override -      PublicEnumeration<string>Getparameternames () { -vector<string> vector =NewVector<string>(Parametermap.keyset ()); +         returnvector.elements (); -     } +  A     /** at * Gets the value of the specified parameter name, if there is a duplicate parameter name, returns the first value that receives a generic variable, such as the text type -      * -      * @paramname Specifies the name of the parameter -      * @returnSpecify the value of the parameter name -      */ - @Override in      Publicstring GetParameter (string name) { -String[] results =parametermap.get (name); to         returnResults[0]; +     } -  the  *     /** $ * Gets an array of all values for the specified parameter name, such as all data for the checkboxPanax Notoginseng * Receive array variables, such as CHECKOBX type -      */ the @Override +      Publicstring[] Getparametervalues (String name) { A         returnparametermap.get (name); the     } +  - @Override $      PublicMap<string, string[]>Getparametermap () { $         returnParametermap; -     } -  the      Public voidSetparametermap (map<string, string[]>Parametermap) { -          This. Parametermap =Parametermap;Wuyi     } the}


2. Add Filter
1  Public classLangfilterImplementsFilter {2 @Override3      Public voidInit (Filterconfig filterconfig)throwsservletexception {4 5     }6 7 @Override8      Public voidDoFilter (ServletRequest servletrequest, Servletresponse servletresponse, Filterchain filterchain)throwsIOException, servletexception {9 TenChangerequestwrapper Changerequestwrapper =NewChangerequestwrapper ((httpservletrequest) servletrequest); One  Amap<string, string[]> parametermap =NewHashmap<>(Changerequestwrapper.getparametermap ()); -  -String[] strings = Parametermap.get ("lang");
The logical code is given the default parameter value, and if the argument is not Chinese, the exception is returned directly
the if(Strings = =NULL|| Strings.length = = 0) { -strings =NewString[1]; -Strings[0] = "zh"; -Parametermap.put ("Lang", strings); + Changerequestwrapper.setparametermap (parametermap); -}Else{ +String language = Strings[0]; A if(!language.equals ("zh")) { atmap<string,string> error =NewHashmap<>(); -Error.put ("code", "500"); - -Servletoutputstream OutputStream =NULL; - Try { -OutputStream =Servletresponse.getoutputstream (); in outputstream.write (Jsonutils.obj2byte (Error)); -}finally { to if(OutputStream! =NULL) { + Outputstream.flush (); - outputstream.close (); the } * } $ Panax Notoginseng return; - } the } + //use of wrapper after replication A Filterchain.dofilter (Changerequestwrapper, servletresponse); the } + - @Override $ Public voiddestroy () { $ - } -}

3.web.xml Add a filter handle

1     <Filter>2         <Filter-name>Languagefilter</Filter-name>3         <Filter-class>Com.intercepor.LangFilter</Filter-class>4         <Init-param>5             <Param-name>Encoding</Param-name>6             <Param-value>UTF-8</Param-value>7         </Init-param>8         <Init-param>9             <Param-name>Forceencoding</Param-name>Ten             <Param-value>True</Param-value> One         </Init-param> A     </Filter> -  -     <filter-mapping> the         <Filter-name>Languagefilter</Filter-name> -         <Url-pattern>*.do</Url-pattern> -     </filter-mapping>

All. Do requests will validate this parameter

Rewrite request parameter with Httpservletrequestwrapper

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.