パラメーターをサーブレットに渡す方法– ServletConfig
以下は、web.xmlのServletConfig“ init-param”を使用して、パラメーターをサーブレットに渡す方法を示すサーブレットコードの例です。
デプロイメント記述子(web.xml)
パラメーター値を「init-param」に入れ、「servlet」要素内にあることを確認します
ServletName com.example.ServletDemo [email protected] ServletName /Demo
サーブレットコード
public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException{ PrintWriter pw = response.getWriter(); pw.println(getServletConfig().getInitParameter("email")); }
「getServletConfig()。getInitParameter(“ email”)」メソッドは、web.xmlでServletConfigパラメーター値を取得するために使用されます。 ところで、このパラメーターは、このサーブレットでのみ使用可能です。 Webアプリケーション全体でグローバルにアクセスできるパラメーターが必要な場合は、サーブレットコンテキスト要素にパラメーターを配置する必要があります。