Struts Chinese Solution Filter

xiaoxiao2021-03-06  39

Plus set character encoding com.huahang.tj.struts.Filters.SetCharacterencodingfilter Encoding gb2312 ignore True set character encoding action This will involve a bean, the source code is as follows: / * * XP forum * * Copyright (c) 2002-2003 Redsoft group. All rights reserved. * * / Package com.huahang.tj.struts.Filters ;

Import javax.servlet. *; import java.io.ioException;

/ ** *

Filter that sets the character encoding to be used in parsing the * incoming request, either unconditionally or only if the client did not * specify a character encoding. Configuration of this filter is based on * the following initialization parameters : *

    *
  • Encoding - the character encoding to be configured * for this request, Either confoundation or unconditionally based on * the ignore initialization Parameter. this parameter * is Required, So there is no default. *
  • ignore - if set to "true", any character encoding * Specified by the client is ignored, and The value returned by the * selectEncoding () method is set. if set to "false, * selectencoding () is caled only if the * Client Has Not Already Specified An Encoding. by Default, this * Parameter Is Set To "True". * * *

    although this filter can be used unchanged, it is also easy to * subclass it and make the selectEncoding () method more * intelligent about what encoding to choose, based on characteristics of * the incoming request (such as the values ​​of the Accept -Language * and user-agent headers, or a value stashed in the current * user's session. * * @Author john wong * @

    Version $ ID: setcharacterencodingfilter.java, V 1.1 2002/04/10 13:59:27 Johnwong Exp $ * / public class setcharacterencodingfilter imports filter {// ----------------- ------------------------------------ Instance Variables

    / ** * The default character Encoding to set for reviews That pass * this filter. * / Protected string encoding = null;

    / ** * The filter configuration Object We are associated with. If this value * is null, this filter instance is not currently configured. * / Protected filterconfig filterfig = null;

    / ** * SHOULD a Character Encoding Specified by the client be ignored? * / Protected boolean ignore = true;

    / / -------------------------------------------------------------------------------------------- --------- Public Methods

    / ** * Take this filter out of service. * / Public void destroy () {

    THIS.Encoding = null; this.filterconfig = NULL;

    }

    / ** * Select and set (if specified) the character encoding to be used to * interpret request parameters for this request. * * @Param request The servlet request we are processing * @param result The servlet response we are creating * @param chain The filter chain we are processing * * @exception IOException if an input / output error occurs * @exception ServletException if a servlet error occurs * / public void doFilter (ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {

    // Conditionally select and set the character encoding to be used if (ignore || (request.getCharacterEncoding () == null)) {String encoding = selectEncoding (request); if (! Encoding = null) request.setCharacterEncoding (encoding) } // Pass Control on to the next filter chain.dofilter (request, response);

    }

    / ** * Place this filter into service. * * @Param FilterConfig the filter configuration Object * / public void init (filterconfig filterconfig) throws servletexception {

    this.filterConfig = filterConfig; this.encoding = filterConfig.getInitParameter ( "encoding"); String value = filterConfig.getInitParameter ( "ignore"); if (value == null) this.ignore = true; else if (value.equalsIgnoreCase ("True")) this.Ignore = true; Else IF (Value.Equalsignorecase ("YES")) this.Ignore = true; else this.ignore = false;

    }

    / / -------------------------------------------------------------------------------------------- ------ Protected Methods

    / ** * Select an appropriate character encoding to be used, based on the * characteristics of the current request and / or filter initialization * parameters. If no character encoding should be set, return * null . *

    * The default implementation unconditionally returns the value configured * by the encoding initialization parameter for this * filter. * * @param request The servlet request we are processing * / protected String selectEncoding (ServletRequest request) {

    Return (this.encoding);

    }

    } // eoc

转载请注明原文地址:https://www.9cbs.com/read-78008.html

New Post(0)