Win a copy of Terraform in Action this week in the Cloud forum!
  • 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:
  • Tim Cooke
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Rob Spoor
  • Bear Bibeault
Saloon Keepers:
  • Jesse Silverman
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • Al Hobbs
  • salvin francis

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: 1512
  • 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: 1512
  • 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!
 
You showed up just in time for the waffles! And this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic