Java : ServletConfig::getInitParameter()で引数がnullの場合、APサーバーによって挙動が異なる。
例えばこういう状況。
普通はあんまり考えられませんが、あったとして。
環境が変われば挙動も変わるという、当たり前のことなのですが、いちおう軽く確認しました。
何かのお役に立てれば。
以下でざっくりと補足。
挙動としてはHashMapのnullの挙動と同じ。内部ではきっとこうなってるんじゃなかでしょうか。
- @Override
- protected void doPost(HttpServletRequest req, HttpServletResponse resp){
- String s = getServletConfig().getInitParameter(null);
普通はあんまり考えられませんが、あったとして。
環境が変われば挙動も変わるという、当たり前のことなのですが、いちおう軽く確認しました。
何かのお役に立てれば。
- Tomcat6 : null が返却される。
- Tomcat9 : NullPointerException がスローされる?(*未検証)
- Wildfly10 : IllegalStateException がスローされる。
以下でざっくりと補足。
■Tomcat6
null が返却される。挙動としてはHashMapのnullの挙動と同じ。内部ではきっとこうなってるんじゃなかでしょうか。
- //HashMapのnull指定時の挙動テスト。
- HashMap
map = new HashMap (); try { String s = map.get(null); System.out.println(s); } catch ( Exception e ) { System.out.println(e.getMessage()); }
■Wildfly10
IllegalStateException がスローされる。
- /**
- * Returns a
String
containing the value of the named- * initialization parameter, or
null
if the parameter does- * not exist. See {@link ServletConfig#getInitParameter}.
- *
- * This method is supplied for convenience. It gets the
- * value of the named parameter from the servlet's
- *
ServletConfig
object.- *
- * @param name a
String
specifying the name- * of the initialization parameter
- *
- * @return String a
String
containing the value- * of the initialization parameter
- *
- */
- public String getInitParameter(String name) {
- ServletConfig sc = getServletConfig();
- if (sc == null) {
- throw new IllegalStateException(
- lStrings.getString("err.servlet_config_not_initialized"));
- }
- return sc.getInitParameter(name);
- }
■Tomcat9
Tomcat9 は、ServletContextの同メソッドに、「NullPointerExceptionをthrowsするよ」とのこと。
- /**
- * Returns a
String
containing the value of the named- * context-wide initialization parameter, or
null
if the- * parameter does not exist.
- *
- * This method can make available configuration information useful to an
- * entire "web application". For example, it can provide a webmaster's email
- * address or the name of a system that holds critical data.
- *
- * @param name
- * a
String
containing the name of the parameter- * whose value is requested
- * @return a
String
containing the value of the initialization- * parameter
- * @throws NullPointerException If the provided parameter name is
- *
null
- * @see ServletConfig#getInitParameter
- */
- public String getInitParameter(String name);
コメント
コメントを投稿