Why is it that context parameters are null outside the doGet block of a
servlet?

For example, suppose I have the following in web.xml:

<context-param>
        <param-name>basepath</param-name>
        <param-value>bob</param-value>
</context-param>

Why does Servlet 1 below print out "bob" while Sevlet 2 gives me a
NullPointerException error? Why isn't the context parameter available
outside the doGet block?

Servlet 1
==========
public class Hello extends HttpServlet{

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

                PrintWriter out = response.getWriter();
                out.println(getServletContext()getInitParameter("basepath"));
                out.close();
        }
}

Servlet 2
==========
public class Hello extends HttpServlet{

        private String basepath = getServletContext()getInitParameter("basepath");

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

                PrintWriter out = response.getWriter();
                out.println(basepath);
                out.close();
        }
}

Thank you,
Mike


--
To unsubscribe, e-mail:   <mailto:[EMAIL PROTECTED]>
For additional commands, e-mail: <mailto:[EMAIL PROTECTED]>

Reply via email to