KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > springframework > web > filter > CharacterEncodingFilter


1 /*
2  * Copyright 2002-2007 the original author or authors.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16
17 package org.springframework.web.filter;
18
19 import java.io.IOException JavaDoc;
20
21 import javax.servlet.FilterChain JavaDoc;
22 import javax.servlet.ServletException JavaDoc;
23 import javax.servlet.http.HttpServletRequest JavaDoc;
24 import javax.servlet.http.HttpServletResponse JavaDoc;
25
26 /**
27  * Servlet 2.3 Filter that allows one to specify a character encoding for requests.
28  * This is useful because current browsers typically do not set a character
29  * encoding even if specified in the HTML page or form.
30  *
31  * <p>This filter can either apply its encoding if the request does not already
32  * specify an encoding, or enforce this filter's encoding in any case.
33  *
34  * @author Juergen Hoeller
35  * @since 15.03.2004
36  * @see #setEncoding
37  * @see #setForceEncoding
38  */

39 public class CharacterEncodingFilter extends OncePerRequestFilter {
40
41     private String JavaDoc encoding;
42
43     private boolean forceEncoding;
44
45
46     /**
47      * Set the encoding to use for requests. This encoding will be passed
48      * into a <code>ServletRequest.setCharacterEncoding</code> call.
49      * <p>Whether this encoding will override existing request
50      * encodings depends on the "forceEncoding" flag.
51      * @see #setForceEncoding
52      * @see javax.servlet.ServletRequest#setCharacterEncoding
53      */

54     public void setEncoding(String JavaDoc encoding) {
55         this.encoding = encoding;
56     }
57
58     /**
59      * Set whether the encoding of this filter should override existing
60      * request encodings. Default is "false", i.e. do not modify encoding if
61      * <code>ServletRequest.getCharacterEncoding</code> returns a non-null value.
62      * @see #setEncoding
63      * @see javax.servlet.ServletRequest#getCharacterEncoding
64      */

65     public void setForceEncoding(boolean forceEncoding) {
66         this.forceEncoding = forceEncoding;
67     }
68
69
70     protected void doFilterInternal(
71             HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response, FilterChain JavaDoc filterChain)
72             throws ServletException JavaDoc, IOException JavaDoc {
73
74         if (this.encoding != null && (this.forceEncoding || request.getCharacterEncoding() == null)) {
75             request.setCharacterEncoding(this.encoding);
76         }
77         filterChain.doFilter(request, response);
78     }
79
80 }
81
Popular Tags