Java : ServletConfig::getInitParameter()で引数がnullの場合、APサーバーによって挙動が異なる。

例えばこういう状況。

  1. @Override
  2. protected void doPost(HttpServletRequest req, HttpServletResponse resp){
  3. String s = getServletConfig().getInitParameter(null);

普通はあんまり考えられませんが、あったとして。

環境が変われば挙動も変わるという、当たり前のことなのですが、いちおう軽く確認しました。
何かのお役に立てれば。

  • Tomcat6 : null が返却される。
  • Tomcat9 : NullPointerException がスローされる?(*未検証)
  • Wildfly10 : IllegalStateException がスローされる。

以下でざっくりと補足。

■Tomcat6

null が返却される。
挙動としてはHashMapのnullの挙動と同じ。内部ではきっとこうなってるんじゃなかでしょうか。
  1. //HashMapのnull指定時の挙動テスト。
  2. HashMap map = new HashMap();
  3. try {
  4. String s = map.get(null);
  5. System.out.println(s);
  6. } catch ( Exception e ) {
  7. System.out.println(e.getMessage());
  8. }

■Wildfly10

IllegalStateException がスローされる。
  1. /**
  2. * Returns a String containing the value of the named
  3. * initialization parameter, or null if the parameter does
  4. * not exist. See {@link ServletConfig#getInitParameter}.
  5. *
  6. * This method is supplied for convenience. It gets the
  7. * value of the named parameter from the servlet's
  8. * ServletConfig object.
  9. *
  10. * @param name a String specifying the name
  11. * of the initialization parameter
  12. *
  13. * @return String a String containing the value
  14. * of the initialization parameter
  15. *
  16. */
  17. public String getInitParameter(String name) {
  18. ServletConfig sc = getServletConfig();
  19. if (sc == null) {
  20. throw new IllegalStateException(
  21. lStrings.getString("err.servlet_config_not_initialized"));
  22. }
  23. return sc.getInitParameter(name);
  24. }

■Tomcat9

Tomcat9 は、ServletContextの同メソッドに、「NullPointerExceptionをthrowsするよ」とのこと。
  1. /**
  2. * Returns a String containing the value of the named
  3. * context-wide initialization parameter, or null if the
  4. * parameter does not exist.
  5. *
  6. * This method can make available configuration information useful to an
  7. * entire "web application". For example, it can provide a webmaster's email
  8. * address or the name of a system that holds critical data.
  9. *
  10. * @param name
  11. * a String containing the name of the parameter
  12. * whose value is requested
  13. * @return a String containing the value of the initialization
  14. * parameter
  15. * @throws NullPointerException If the provided parameter name is
  16. * null
  17. * @see ServletConfig#getInitParameter
  18. */
  19. public String getInitParameter(String name);

コメント

このブログの人気の投稿

windows10 で nvidia のグラボのcode43現象を解決した

Java : processbuilder 標準出力 タイムアウト

GTX560Ti がおかしい(code 43が出る)(2018年)→解決しました(2019)