Class TrimWhiteSpaceFilter
- java.lang.Object
-
- com.twelvemonkeys.servlet.GenericFilter
-
- com.twelvemonkeys.servlet.TrimWhiteSpaceFilter
-
- All Implemented Interfaces:
java.io.Serializable
,javax.servlet.Filter
,javax.servlet.FilterConfig
@Deprecated public class TrimWhiteSpaceFilter extends GenericFilter
Deprecated.Removes extra unneccessary white space from a servlet response. White space is defined as perCharacter.isWhitespace(char)
.This filter has no understanding of the content in the reponse, and will remove repeated white space anywhere in the stream. It is intended for removing white space from HTML or XML streams, but this limitation makes it less suited for filtering HTML/XHTML with embedded CSS or JavaScript, in case white space should be significant here. It is strongly reccommended you keep CSS and JavaScript in separate files (this will have the added benefit of further reducing the ammount of data communicated between server and client).
At the moment this filter has no concept of encoding. This means, that if some multi-byte escape sequence contains one or more bytes that individually is treated as a white space, these bytes may be skipped. As UTF-8 guarantees that no bytes are repeated in this way, this filter can safely filter UTF-8. Simple 8 bit character encodings, like the ISO/IEC 8859 standard, or Windows-1252" are always safe.
Configuration
To useTrimWhiteSpaceFilter
in your web-application, you simply need to add it to your web descriptor (web.xml
). If using a servlet container that supports the Servlet 2.4 spec, the newdispatcher
element should be used, and set toREQUEST/FORWARD
, to make sure the filter is invoked only once for requests. If using an older web descriptor, set theinit-param
"once-per-request"
to"true"
(this will have the same effect, but might perform slightly worse than the 2.4 version). Please see the examples below.Servlet 2.4 version, filter section:
<!-- TrimWS Filter Configuration --> <filter> <filter-name>trimws</filter-name> <filter-class>com.twelvemonkeys.servlet.TrimWhiteSpaceFilter</filter-class> <!-- auto-flush=true is the default, may be omitted --> <init-param> <param-name>auto-flush</param-name> <param-value>true</param-value> </init-param> </filter>
Filter-mapping section:
<!-- TimWS Filter Mapping --> <filter-mapping> <filter-name>trimws</filter-name> <url-pattern>*.html</url-pattern> <dispatcher>REQUEST</dispatcher> <dispatcher>FORWARD</dispatcher> </filter-mapping> <filter-mapping> <filter-name>trimws</filter-name> <url-pattern>*.jsp</url-pattern> <dispatcher>REQUEST</dispatcher> <dispatcher>FORWARD</dispatcher> </filter-mapping>
- Version:
- $Id: TrimWhiteSpaceFilter.java#2 $
- Author:
- Harald Kuhr, last modified by $Author: haku $
- See Also:
- Serialized Form
-
-
Field Summary
-
Fields inherited from class com.twelvemonkeys.servlet.GenericFilter
oncePerRequest
-
-
Constructor Summary
Constructors Constructor Description TrimWhiteSpaceFilter()
Deprecated.
-
Method Summary
All Methods Instance Methods Concrete Methods Deprecated Methods Modifier and Type Method Description protected void
doFilterImpl(javax.servlet.ServletRequest pRequest, javax.servlet.ServletResponse pResponse, javax.servlet.FilterChain pChain)
Deprecated.Invoked once, or each time a request/response pair is passed through the chain, depending on theGenericFilter.oncePerRequest
member variable.void
init()
Deprecated.A convenience method which can be overridden so that there's no need to callsuper.init(config)
.void
setAutoFlush(boolean pAutoFlush)
Deprecated.-
Methods inherited from class com.twelvemonkeys.servlet.GenericFilter
destroy, doFilter, getFilterConfig, getFilterName, getInitParameter, getInitParameterNames, getServletContext, init, log, log, setFilterConfig, setOncePerRequest
-
-
-
-
Method Detail
-
setAutoFlush
@InitParam public void setAutoFlush(boolean pAutoFlush)
Deprecated.
-
init
public void init() throws javax.servlet.ServletException
Deprecated.Description copied from class:GenericFilter
A convenience method which can be overridden so that there's no need to callsuper.init(config)
.- Overrides:
init
in classGenericFilter
- Throws:
javax.servlet.ServletException
- if an error occurs during init- See Also:
GenericFilter.init(FilterConfig)
-
doFilterImpl
protected void doFilterImpl(javax.servlet.ServletRequest pRequest, javax.servlet.ServletResponse pResponse, javax.servlet.FilterChain pChain) throws java.io.IOException, javax.servlet.ServletException
Deprecated.Description copied from class:GenericFilter
Invoked once, or each time a request/response pair is passed through the chain, depending on theGenericFilter.oncePerRequest
member variable.- Specified by:
doFilterImpl
in classGenericFilter
- Parameters:
pRequest
- the servlet requestpResponse
- the servlet responsepChain
- the filter chain- Throws:
java.io.IOException
- if an I/O error occursjavax.servlet.ServletException
- if an exception occurs during the filter process- See Also:
GenericFilter.oncePerRequest
,doFilter
,Filter.doFilter
-
-