背景
terasoluna serverでWeb開発をしています。
- Java7
- Tomcat8
- terasoluna server 2.0.5.0 (https://osdn.jp/projects/terasoluna/releases/63465 )
エンコーディングフィルタを設定するため、WEB-INF/web.xmlに下記を設定しました。
terasolunaの機能説明書 (AL008-02 エンコードフィルタ機能)を、参照しました。
xml
1<filter> 2 <filter-name>encodingFilter</filter-name> 3 <filter-class>jp.terasoluna.fw.ex.web.thin.SetCharacterEncodingFilter</filter-class> 4 <init-param> 5 <param-name>encoding</param-name> 6 <param-value>Windows-31J</param-value> 7 </init-param> 8 <init-param> 9 <param-name>more</param-name> 10 <param-value>false</param-value> 11 </init-param> 12 <init-param> 13 <param-name>ignore</param-name> 14 <param-value>false</param-value> 15 </init-param> 16</filter> 17<filter-mapping> 18 <filter-name>encodingFilter</filter-name> 19 <url-pattern>/*</url-pattern> 20</filter-mapping>
質問1
jp.terasoluna.fw.ex.web.thin.SetCharacterEncodingFilter
の、more
パラメタの意味を教えてください。
JavaDocを見ましたが、「フィルタの複数回処理フラグ」という情報までしか分かりませんでした。
jp.terasoluna.fw.ex.web.thin.SetCharacterEncodingFilter
も掲載します。
java
1/* 2 * Copyright (c) 2012 NTT DATA Corporation 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 17package jp.terasoluna.fw.ex.web.thin; 18 19import java.io.IOException; 20import java.nio.charset.Charset; 21import java.nio.charset.IllegalCharsetNameException; 22 23import javax.servlet.Filter; 24import javax.servlet.FilterChain; 25import javax.servlet.FilterConfig; 26import javax.servlet.ServletException; 27import javax.servlet.ServletRequest; 28import javax.servlet.ServletResponse; 29 30import org.apache.commons.logging.Log; 31import org.apache.commons.logging.LogFactory; 32 33/** 34 * <p>リクエストパラメータのエンコーディング設定を行う。</p> 35 * <p> 36 * 使用方法 37 * </p> 38 * <p> 39 * クライアントのリクエストに自動的に文字エンコード指定を行いたい場合、 40 * Webアプリケーション設定ファイル(web.xml)で 41 * 専用のサーブレットフィルタ(SetCharacterEncodingFilter)を登録する。<br> 42 * その際、初期化パラメータとして、 43 * リクエストの文字エンコード種別・フィルタの複数回処理フラグ・フィルター無効化フラグを指定する。<br> 44 * 文字エンコード種別の値が未入力、無効な値の場合はwarnレベルのログを出力し、エンコーディング設定は行われない。<br> 45 * 以下は、リクエストに自動的に文字エンコード指定を行う場合のencodingの指定例である。<br> 46 * <br> 47 * Webアプリケーション設定ファイル(web.xml)<br> 48 * <code> 49 * <filter><br> 50 * <filter-name>encodingFilter</filter-name><br> 51 * <filter-class><br> 52 * jp.terasoluna.fw.ex.web.thin.SetCharacterEncodingFilter<br> 53 * </filter-class><br> 54 * <init-param><br> 55 * <param-name>encoding</param-name><br> 56 * <param-value>Windows-31J</param-value><br> 57 * </init-param><br> 58 * <init-param><br> 59 * <param-name>more</param-name><br> 60 * <param-value>false</param-value><br> 61 * </init-param><br> 62 * <init-param><br> 63 * <param-name>ignore</param-name><br> 64 * <param-value>false</param-value><br> 65 * </init-param><br> 66 * </filter><br> 67 * <br> 68 * <filter-mapping><br> 69 * <filter-name>encodingFilter</filter-name><br> 70 * <url-pattern>/*</url-pattern><br> 71 * </filter-mapping><br> 72 * </code> 73 */ 74public class SetCharacterEncodingFilter implements Filter { 75 76 /** 77 * ログクラス。 78 */ 79 private static Log log = LogFactory 80 .getLog(SetCharacterEncodingFilter.class); 81 82 /** 83 * フィルタ処理済みフラグ。 84 */ 85 private static final String THRU_FILTER_KEY = "THRU_FILTER_ENCODING"; 86 87 /** 88 * フィルタの複数回処理フラグ。 89 */ 90 private boolean moreFlag = false; 91 92 /** 93 * エンコーディング。 94 */ 95 private String encoding = null; 96 97 /** 98 * このフィルターを無効化するフラグ。 99 */ 100 private boolean ignoreFlag = false; 101 102 /** 103 * 初期化処理。 104 * 105 * @param filterConfig 106 * フィルター設定 107 * 108 * @throws ServletException 109 * 例外 110 */ 111 public void init(FilterConfig filterConfig) throws ServletException { 112 113 // Log 114 if (log.isDebugEnabled()) { 115 log.debug("init() called."); 116 } 117 118 String encode = filterConfig.getInitParameter("encoding"); 119 120 try { 121 if (!Charset.isSupported(encode)) { 122 log.warn(encode + " is unsupported encoding."); 123 } else { 124 this.encoding = encode; 125 } 126 } catch (IllegalCharsetNameException e) { 127 log.warn("The format of encoding is not correct."); 128 } catch (IllegalArgumentException e) { 129 log.warn("encoding can not be null."); 130 } 131 132 if (log.isDebugEnabled()) { 133 log.debug("this.encoding = " + this.encoding); 134 } 135 136 String ignoreValue = filterConfig.getInitParameter("ignore"); 137 if (ignoreValue != null) { 138 this.ignoreFlag = this.createFlag(ignoreValue); 139 } 140 141 String moreValue = filterConfig.getInitParameter("more"); 142 if (moreValue != null) { 143 this.moreFlag = this.createFlag(moreValue); 144 } 145 } 146 147 /** 148 * フィルター処理。 149 * 150 * <p> 151 * リクエストパラメータを指定されたエンコーディングに設定する。 152 * </p> 153 * 154 * @param request 155 * リクエスト 156 * @param response 157 * レスポンス 158 * @param chain 159 * フィルターチェーン 160 * 161 * @throws IOException 162 * 例外 163 * @throws ServletException 164 * 例外 165 */ 166 public void doFilter(ServletRequest request, ServletResponse response, 167 FilterChain chain) throws IOException, 168 ServletException { 169 170 if (!this.moreFlag && request.getAttribute(THRU_FILTER_KEY) != null) { 171 chain.doFilter(request, response); 172 return; 173 } 174 175 if (!ignoreFlag && encoding != null) { 176 // Log 177 if (log.isDebugEnabled()) { 178 log.debug("set encoding(" + encoding + ") to request."); 179 } 180 request.setCharacterEncoding(encoding); 181 } 182 183 // 処理済フラグを格納 184 request.setAttribute(THRU_FILTER_KEY, THRU_FILTER_KEY); 185 186 chain.doFilter(request, response); 187 } 188 189 /** 190 * 終了時処理。 191 */ 192 public void destroy() { 193 this.encoding = null; 194 } 195 196 /** 197 * 文字列より、フラグを生成する。 198 * 199 * @param value 200 * 対象文字列。 201 * @return 生成されたフラグ。 202 */ 203 private boolean createFlag(String value) { 204 if ("true".equalsIgnoreCase(value)) { 205 return true; 206 } else if ("yes".equalsIgnoreCase(value)) { 207 return true; 208 } 209 return false; 210 } 211}
質問2
Tomcatにも、エンコーディングフィルタorg.apache.catalina.filters.SetCharacterEncodingFilter
があります。
これと、tomcatのエンコーディングフィルタの違いを教えてください。
公式サイト: https://tomcat.apache.org/tomcat-7.0-doc/config/filter.html#Set_Character_Encoding_Filter
あなたの回答
tips
プレビュー