KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > openharmonise > rm > view > servlet > utils > CharsetFilter


1 /*
2  * The contents of this file are subject to the
3  * Mozilla Public License Version 1.1 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at http://www.mozilla.org/MPL/
6  *
7  * Software distributed under the License is distributed on an "AS IS"
8  * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied.
9  * See the License for the specific language governing rights and
10  * limitations under the License.
11  *
12  * The Initial Developer of the Original Code is John King .
13  * Portions created by John King are Copyright (C) John King, 2003.
14  *
15  * All Rights Reserved.
16  *
17  * Contributor(s): John King, Simulacra Media Ltd
18  *
19  * Created: 02-Dec-2004 by jejking
20  * Version: $Revision: 1.1 $
21  * Last Updated: $Date: 2004/12/07 12:25:48 $
22  */

23 package org.openharmonise.rm.view.servlet.utils;
24
25
26 import java.io.*;
27 import javax.servlet.*;
28 import javax.servlet.http.*;
29
30 /**
31  * Simple filter that checks whether a response has a character encoding set,
32  * and if it doesn't, sets that to UTF8.
33  * @author jejking
34  * @version $Revision: 1.1 $
35  */

36 public class CharsetFilter implements Filter {
37     
38     /** Creates a new instance of CharSetFilter */
39     public CharsetFilter() {
40     }
41     
42     public void destroy() {
43     }
44     
45     public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
46         
47         HttpServletRequest req = (HttpServletRequest) servletRequest;
48         if (req.getCharacterEncoding() == null || req.getCharacterEncoding().equals("")) {
49             try {
50                 req.setCharacterEncoding("UTF-8");
51             }
52             catch (UnsupportedEncodingException uee) {
53                 throw new ServletException(uee);
54             }
55         }
56         filterChain.doFilter(servletRequest, servletResponse);
57     }
58     
59     public void init(javax.servlet.FilterConfig JavaDoc filterConfig) throws javax.servlet.ServletException JavaDoc {
60     }
61     
62 }
63
64
Popular Tags