| Author |
Servlet doesn't pick initial parameter
|
Andrew Carney
Ranch Hand
Joined: Oct 17, 2006
Posts: 96
|
|
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
Joined: Jul 03, 2006
Posts: 27
|
|
|
Please add servlet mapping element in your web.xml
|
 |
Andrew Carney
Ranch Hand
Joined: Oct 17, 2006
Posts: 96
|
|
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
Joined: Nov 06, 2000
Posts: 1506
|
|
|
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.
|
Bosun (SCJP, SCWCD)
So much trouble in the world -- Bob Marley
|
 |
Andrew Carney
Ranch Hand
Joined: Oct 17, 2006
Posts: 96
|
|
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
Joined: Nov 06, 2000
Posts: 1506
|
|
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
Joined: Oct 17, 2006
Posts: 96
|
|
|
Thanks a lot for clearing this!
|
 |
 |
|
|
subject: Servlet doesn't pick initial parameter
|
|
|