KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > cowsultants > itracker > web > filters > SetRequestCharacterEncoding


1 /*
2  * This software was designed and created by Jason Carroll.
3  * Copyright (c) 2002, 2003, 2004 Jason Carroll.
4  * The author can be reached at jcarroll@cowsultants.com
5  * ITracker website: http://www.cowsultants.com
6  * ITracker forums: http://www.cowsultants.com/phpBB/index.php
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it only under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 2 of the License, or
11  * (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  * GNU General Public License for more details.
17  */

18
19 package cowsultants.itracker.web.filters;
20
21 import java.io.IOException JavaDoc;
22 import javax.servlet.*;
23
24 /**
25   * This class will set the chracter encoding of each request that uses the filter. It
26   * will use the encoding specifried in the init parameter, or if that is not present,
27   * fall back to a default value of UTF-8.
28   */

29 public class SetRequestCharacterEncoding implements Filter {
30     public static final String JavaDoc DEFAULT_ENCODING = "UTF-8";
31
32     private FilterConfig filterConfig = null;
33     private String JavaDoc encoding = null;
34
35     /**
36       * Set the character encoding in the request.
37       * @param request the current ServletRequest object
38       * @param response the current ServletResponse object
39       * @param filterChain the current FilterChain
40       * @throws IOException if any io error occurs
41       * @throws ServletException any other servlet error occurs
42      */

43     public void doFilter(ServletRequest request, ServletResponse response, FilterChain filterChain) throws IOException JavaDoc, ServletException {
44         request.setCharacterEncoding(getEncoding());
45         filterChain.doFilter(request, response);
46     }
47
48     /**
49      * Initialize the filter.
50      * @param filterConfig the current filter configuration
51      */

52     public void init(FilterConfig filterConfig) throws ServletException {
53         this.filterConfig = filterConfig;
54         setEncoding(filterConfig.getInitParameter("encoding"));
55     }
56
57     /**
58       * Returns the encoding of the request.
59       */

60     public String JavaDoc getEncoding() {
61         return (encoding == null ? DEFAULT_ENCODING : encoding);
62     }
63
64     /**
65       * Sets the encoding of the request.
66       */

67     public void setEncoding(String JavaDoc value) {
68         if(value != null) {
69             encoding = value;
70         }
71     }
72
73     /**
74       * Reset the filter settings.
75       */

76     public void destroy() {
77         encoding = null;
78         filterConfig = null;
79     }
80 }
81
82
Popular Tags