Configure filter files in Struts!

xiaoxiao2021-03-06  103

Plus set character encoding com.ytoa.util.setcharacterencodingfilter Encoding GB2312 ignore true set character encoding action Filter file is: package com.ytoa.util;

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

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);

}

}

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

New Post(0)