• No replies
16 Posts

Pinned topic getInitParameterNames() does not get sevlet init-parameters

‏2013-01-09T01:40:41Z |
I'm taking my first steps on web developing using servlets. Now I'm trying to retrieve some init-parameters of a servlet as well as context ones. They are declared on web.xml as follows:

<?xml version=
"1.0" encoding=
"UTF-8"?> <web-app xmlns:xsi=
"" xmlns=
"" xmlns:web=
"" xsi:schemaLocation=
"" version=
"3.0"> <display-name>JediWebTutorial</display-name> <context-param> <param-name>contextParam</param-name> <param-value>contextValue</param-value> </context-param> <servlet> <servlet-name>GreeterServlet</servlet-name> <servlet-class>jedi.servlets.GreeterServlet</servlet-class> <init-param> <param-name>servletParam</param-name> <param-value>servletValue</param-value> </init-param> </servlet> <servlet> <servlet-name>GetParameterValuesServlet</servlet-name> ...

I'm trying to get it within the GreeterServlet, in the doGet method, like this:


void doGet(HttpServletRequest request, HttpServletResponse response) 

throws ServletException, IOException 
{   ServletConfig config = getServletConfig(); ServletContext context = config.getServletContext(); Enumeration<String> servletParams = config.getInitParameterNames(); Enumeration<String> contextParams = context.getInitParameterNames();   

for (String param : Collections.list(servletParams)) 
{ System.out.println(param); 

for (String param : Collections.list(contextParams)) 
{ System.out.println(param); 
} ...

As expected, console shows the name "contextParam". However, it does not print the "servletParam" name, because, though the servlet parameter is there in web.xml, getInitParameterNames() return an empty enumeration.

I'm struggling with it for hours and have assured that the node is inside and I'm not overriding init() method of GreeterServlet in any way (as it was reported as the cause of the problem in some cases on many discussion lists). I haven't found similar case on web - maybe for it's a too simple matter that I'm too beginer. Anyway, thanks for anybody who bother to take a look at it!

By the way, if helpful, I'm using TomCat 7.0.34 and Eclipse Helios.