com.googlecode.webutilities.filters
Class CharacterEncodingFilter
java.lang.Object
com.googlecode.webutilities.filters.common.AbstractFilter
com.googlecode.webutilities.filters.CharacterEncodingFilter
- All Implemented Interfaces:
- IgnoreAcceptContext, javax.servlet.Filter
public class CharacterEncodingFilter
- extends AbstractFilter
The CharacterEncodingFilter can be used to specify a character encoding for requests in Servlet 2.3/2.4.
Using the CharacterEncodingFilter you can specify the encoding to be applied on request (if request
doesn't have any encoding set already by client). If client has already set the encoding on request but you still want
to enforce to use this encoding then you can even force it (using force = true). If you are using Servlet 2.4 and force
is true then not just request but the response will also be forced to the specified encoding.
This is very similar to CharacterEncodingFilter in Spring framework.
Look at http://static.springsource.org/spring/docs/2.0.x/api/org/springframework/web/filter/CharacterEncodingFilter.html
Usage
Put the webutilities-x.y.z.jar (See dependency mentioned below) in your classpath (WEB-INF/lib folder of your webapp).
Declare this filter in your web.xml (web descriptor file).
...
<filter>
<filter-name>characterEncodingFilter</filter-name>
<filter-class>com.googlecode.webutilities.CharacterEncodingFilter</filter-class>
<!-- init params -->
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
<init-param>
<param-name>force</param-name>
<param-value>true</param-value> <!-- true if you wanted to force encoding -->
</init-param>
<init-param>
<param-name>ignoreURLPattern</param-name>
<param-value>.*\.(png|jpg)</param-value> <!-- regular expression to be matched against URL to skip setting encoding on -->
</init-param>
</filter>
...
Map this filter on your web requests
...
<filter-mapping>
<filter-name>characterEncodingFilter</filter-name>
<url-pattern>*</url-pattern>
</filter-mapping>
...
And you are all done!
Init Parameters
All the init parameters are optional and explained below.
encoding - name of the encoding that you wanted to set. e.g. UTF-8
force - true or false. If true this encoding will be forced and overwrite already set encoding for both request and response.
And all infra the six filtering criteria parameters for better customization.
ignoreURLPattern - regular expression. e.g. .*\.(jpg|png|gif).
acceptURLPattern - regular expression. e.g. .*\.(css|js|html).
ignoreMIMEPattern - regular expression. e.g. images/.*|video/.*.
acceptMIMEPattern - regular expression. e.g. text/css|text/html.
ignoreUAPattern - regular expression for user agent. e.g. MSIE|KHTML.
acceptUAPattern - regular expression. e.g. WebKit|Opera.
Visit http://code.google.com/p/webutilities/wiki/CharacterEncodingFilter for more details.
- Version:
- 1.0
- Author:
- rpatil
|
Method Summary |
void |
doFilter(javax.servlet.ServletRequest request,
javax.servlet.ServletResponse response,
javax.servlet.FilterChain chain)
|
void |
init(javax.servlet.FilterConfig config)
|
| Methods inherited from class java.lang.Object |
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
CharacterEncodingFilter
public CharacterEncodingFilter()
init
public void init(javax.servlet.FilterConfig config)
throws javax.servlet.ServletException
- Specified by:
init in interface javax.servlet.Filter- Overrides:
init in class AbstractFilter
- Throws:
javax.servlet.ServletException
doFilter
public void doFilter(javax.servlet.ServletRequest request,
javax.servlet.ServletResponse response,
javax.servlet.FilterChain chain)
throws IOException,
javax.servlet.ServletException
- Throws:
IOException
javax.servlet.ServletException
Copyright © 2011. All Rights Reserved.