Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Servlet doesn't pick initial parameter

 
Andrew Carney
Ranch Hand
Posts: 96
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,

I have created a simple Servlet that reads init parameter.

Here are the lines from the servlet doGet method:

String url = getServletConfig().getInitParameter("URL");
out.println("url: " + url + "<br>");

and here is the relevant web.xml:
<servlet>
<servlet-name>MyServlet</servlet-name>
<servlet-class>com.mycompany.ADPServlet</servlet-class>
<init-param>
<param-name>URL</param-name>
<param-value>test</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>

When I run the Servlet the output is:
url: null

Any idea why...?
 
Satish Kandagadla
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please add servlet mapping element in your web.xml
 
Andrew Carney
Ranch Hand
Posts: 96
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I already have servlet mapping:

<servlet-mapping>
<servlet-name>MyServlet</servlet-name>
<url-pattern>/MyServlet</url-pattern>
</servlet-mapping>

and to be honest I don't think that it is relevant to the problem.
 
Bosun Bello
Ranch Hand
Posts: 1511
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How are you accessing this servlet? It needs to be by the name you assigned to it, which is also matches the mapping according to your post above.
 
Andrew Carney
Ranch Hand
Posts: 96
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I wanted to say that I must be accessing it correctly otherwise I wouldn't have seen the output but now that I check I see that if I access the servlet by the class name I get null but when I access the servlet by the mapping name I do see the parameter value! Why is that....?
[ November 16, 2008: Message edited by: Roy Cohen ]
 
Bosun Bello
Ranch Hand
Posts: 1511
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That's just the way it works. According to the specification, Initialization parameters are only available when
servlets are accessed by means of their registered names or through
custom URL patterns associated with their registered names.

Originally posted by Roy Cohen:
I wanted to say that I must be accessing it correctly otherwise I wouldn't have seen the output but now that I check I see that if I access the servlet by the class name I get null but when I access the servlet by the mapping name I do see the parameter value! Why is that....?

[ November 16, 2008: Message edited by: Roy Cohen ]
 
Andrew Carney
Ranch Hand
Posts: 96
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks a lot for clearing this!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic