I have recently made a project based on Tomcat and LDAP technology, I am tired of previously calling request.getParameter (...) or request.getParameterValues each time the method is the operation of the character set conversion.
Using ServletFilter, you can implement a character set conversion filter for the application layer, and do not need to convert the servlets in the application, just need to install a servletfilter for completing character sets in the Web Deployment Description file. Yes.
The content of this servletfilter is as follows:
// **** i18nServletFilter.java source program *************************************************** *****************
Package com.npower.im.servlet;
Import javax.servlet. *; import javax.servlet.http. *; import java.io. *; import java.util. *;
/ ** *
Company: NPOWER Technology Ltd. P> * @Author zhao donglu * @version 1.0 * /
Public Class I18NServletFilter Extends HTTPSERVLET IMPLEments Filter {Private FilterConfig FilterConfig;
Private string charSet = "UTF-8";
// Handle the passed-in FilterConfig public void init (FilterConfig filterConfig) throws ServletException {this.filterConfig = filterConfig; this.charset = filterConfig.getInitParameter ( "charset"); if (this.charset == null || this.charset .trim (). Length () == 0) {this.Charset = null;}}
// Process the request / response pair public void doFilter (ServletRequest request, ServletResponse response, FilterChain filterChain) {try {if (this.charset = null!) {I18nHttpServletRequestWrapper requestWrapper = new I18nHttpServletRequestWrapper ((HttpServletRequest) request, charset); filterChain. doFilter (requestWrapper, response);} else {filterChain.doFilter (request, response);}} catch (ServletException sx) {filterConfig.getServletContext () log (sx.getMessage ());.} catch (IOException iox) {filterConfig . GetServletContext (). log (Iox.getMessage ());}} // clean up resources public void destroy () {super.destroy ();}}
// **** i18nhttpservletRequestWrapper.java source procedure ********************************************************* *************** PACKAGE com.npower.im.servlet;
Import java.util. *;
Import javax.servlet.http.httpservletRequestWrapper; import javax.servlet.http.httpservletRequest; import java.io. *;
Import com.npower.im.config.global; / ** *
Title: p> *
description: p> *
Copyright: Copyright (c) 2004 p> *
company: p> * @Author not attributable * @version 1.0 * /
Public class i18nhttpservletRequestWrapper Extends httpservletRequestWrapper {
Private Map HashMap = new hashmap ();
Private string charSet = "ISO-8859-1";
Public i18nhttpservletRequestWrapper (httpservletRequest Request, String Charset) {Super (Request); this.charset = charset; initparametermap (request);}
Private void initparameterMap (httpservletRequest request) {if (request == null) {return;} map map = request.getParameterMap (); set names = map.keyset (); for (Iterator i = names.Iiterator (); i. Hasnext (); {string name = (string) i.next (); this.hashmap.put (name, map.get (name));}} public string getParameter (String name) {string [] Values = THIS .getParameterValues (name); if (value! = null&iguees.length> 0) {Return Values [0];} else {return null;}}
Public String [] getParameterValues (String Name) {string [] value value = (String []) this.hashmap.get (name); if (value == null) {return null;} String [] Result = new string [VALUES .length]; for (int i = 0; values! = null && i Private string converTcharset (string str) {if (str == null) {Return null;} try {byte [] BS = str.getbytes ("ISO-8859-1"); String Result = New String (BS, this. Charset;} catch (unsupporteencodingexception ex) {global.getlogger (). Fatal ("i18nhttpservletRequestWrapper: this.charset, ex);} return null; } // *** Added in the Web.xml file: Adopt the above method, automatically perform character set by a servletfilter to perform character set, without the need for each call in the application. At the same time, I also give you a reference, we should use servletfilter in some cases. Zhao Donglu, IEI Technology Company