I'm experimenting with using annotations.  I created a Servlet with
annotations and then attempt to get the init parameters in the doGet()
method, but I keep getting a null value when I use
this.getServletConfig().  If I save the ServletConfig in an instance
variable from the init() method it works as expected.  Shouldn't the
this.getServletConfig() return the configuration object instead of a null?
What am I missing?

Here is a listing. The code is also attached. I've run it both with and
without a web.xml file (just the root element when present).
                @WebInitParam(name="name", value="Richard"),
                @WebInitParam(name="greeting", value="Hola")
public class TheServlet extends HttpServlet {

      ServletConfig myConfig;

      public void init(ServletConfig config) throws ServletException{
           myConfig = config;

    protected void doGet(HttpServletRequest request, HttpServletResponse
response) throws ServletException, IOException {

        // Set content type

       // Get initialization parameters

       //ServletConfig config = this.getServletConfig();
       //^^^^^^^^^^^^^^  The above returns null ^^^^^^^^

       ServletConfig config = myConfig;
       //^^^^^^^The above works ^^^^^^^^

       if(config != null){
              String name = config.getInitParameter("name");
              String greeting = config.getInitParameter("greeting");
              response.getWriter().println(greeting + " " +name);
              response.getWriter().println("there is no config");

Thanks in advance!

Richard Monson-Haefel

Reply via email to