• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Servlet doesn't pick initial parameter

 
Ranch Hand
Posts: 96
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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...?
 
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 1514
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 1514
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks a lot for clearing this!
 
This tiny ad is suggesting that maybe she should go play in traffic.
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic