File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Servlets and the fly likes Servlet doesn't pick initial parameter Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "Servlet doesn Watch "Servlet doesn New topic
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: 1510
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: 1510
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!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Servlet doesn't pick initial parameter