How do I get a servlet to read web.xml <param*> lines?

M

miguelk

I'm working through some examples about servlet programming and have
run into a problem. I can't get the servlet to read configuration
parameters. I'm using Tomcat 5.0.28. The web.xml file I have goes like
this:

[snip]
<servlet>
<servlet-name>ConfigDemo</servlet-name>
<servlet-class>ConfigDemoServlet</servlet-class>
</servlet>
<init-param>
<param-name>adminContact</param-name>
<param-value>Miguel</param-value>
</init-param>
<servlet-mapping>
<servlet-name>ConfigDemo</servlet-name>
<url-pattern>/servlet/ConfigDemoServlet</url-pattern>
</servlet-mapping>
[snap]

My servlet code to show the parameters goes like this:

[snip]
public class ConfigDemoServlet extends GenericServlet implements
Servlet {
public void init(ServletConfig config) throws ServletException {
Enumeration parameters = config.getInitParameterNames();
System.out.println("has elements? " + parameters.hasMoreElements());
while (parameters.hasMoreElements()) {
String parameter = (String) parameters.nextElement();
System.out.print("parameter " + parameter + " = ");
System.out.println(config.getInitParameter(parameter));
}
}
........
[snap]

But when run, all I get is:

has elements? false

My directory structure looks like this:

\tomcat 5.0\webapps\budi\WEB-INF\classes where web.xml is under
WEB-INF.

What am I doing wrong?

Thanks.

Miguel
 
O

Oscar kind

miguelk said:
I'm working through some examples about servlet programming and have
run into a problem. I can't get the servlet to read configuration
parameters. I'm using Tomcat 5.0.28. The web.xml file I have goes like
this:

[snip]
<servlet>
<servlet-name>ConfigDemo</servlet-name>
<servlet-class>ConfigDemoServlet</servlet-class>
</servlet>
<init-param>
<param-name>adminContact</param-name>
<param-value>Miguel</param-value>
</init-param>
<servlet-mapping>
<servlet-name>ConfigDemo</servlet-name>
<url-pattern>/servlet/ConfigDemoServlet</url-pattern>
</servlet-mapping>
[snap]

My servlet code to show the parameters goes like this:

[snip]
public class ConfigDemoServlet extends GenericServlet implements
Servlet {
public void init(ServletConfig config) throws ServletException {
Enumeration parameters = config.getInitParameterNames();
System.out.println("has elements? " + parameters.hasMoreElements());
while (parameters.hasMoreElements()) {
String parameter = (String) parameters.nextElement();
System.out.print("parameter " + parameter + " = ");
System.out.println(config.getInitParameter(parameter));
}
}
.......
[snap]

But when run, all I get is:

has elements? false [...]
What am I doing wrong?

The servlet prameters are not in the servlet tag, and thus do not belong
to the sertvlet.

More the init-param tags within the servlet tag, and see what happens.
 
I

Igor Kolomiyets

Miguel,

web.xml should be in
\tomcat 5.0\webapps\budi\WEB-INF not in \tomcat
5.0\webapps\budi\WEB-INF\classes

Best regards,
Igor.
 
M

miguelk

Yes, thanks, that was it. I had actually tried this but I probably
made another mistake that time that kept it from working.

Miguel
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,066
Latest member
VytoKetoReviews

Latest Threads

Top