jQuery in Action, 3rd edition
The moose likes Web Component Certification (SCWCD/OCPJWCD) and the fly likes Servlet Context parameters query Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Web Component Certification (SCWCD/OCPJWCD)
Bookmark "Servlet Context parameters query" Watch "Servlet Context parameters query" New topic

Servlet Context parameters query

Abhay Agarwal
Ranch Hand

Joined: Feb 29, 2008
Posts: 1341

Have question

Why multiple <param-name /> <param-value /> are allowed within a <context-param> tag?
foeg -

Also noticed that ContextParam1 is overrided by ContextParam2 means in servlets
getServletContext().getInitParameter("ContextParam2") returns "ContextValue2"
getServletContext().getInitParameter("ContextParam1") returns null.

I am not able to understand its behaviour.
Please explain

Oracle certified Java 8 Programmer I (1Z0-808), Oracle Java Web Service Developer (1z0-897), Oracle certified Java 7 Programmer, SCJA 1.0, SCJP 5.0, SCWCD 5.0, Oracle SQL Fundamentals I, CIW Certified Ecommerce specialist
Lucas Smith
Ranch Hand

Joined: Apr 20, 2009
Posts: 808

So it's not allowed :P Only the last one matters in this case.
You should do something like this:

BLOG: http://leakfromjavaheap.blogspot.com
Chinmaya Chowdary
Ranch Hand

Joined: Apr 21, 2008
Posts: 432
Hi, Abhay.

I tested with <init-param> tag and I got the same output as incase of using <context-param>

In servlet specification 2.4, SRV.13.4 Deployment Diagram says <param-name> and <param-value> is mandatory elements. They showed this by using 'servlet' element(see it is 10th element). I guess the same applies for <context-param> also. They don't showed the number of occurances. I don't know, why 'Tomcat' implemented in this way. If we want multiple param's, generally we use like what 'Lucas' said above.
Abhay Agarwal
Ranch Hand

Joined: Feb 29, 2008
Posts: 1341

Yes i understand we should have seperate context param tag for each init param name/value pair.
I was playing around with web.xml and tried placing two name/value param within a single context-param and to my surprise Tomcat did not gave any deployment error/runtime error. Only thing is that param got overrided.

I want to know does this behaviour (overriding on param name/value pair) is dependent on Vendor implementation of J2EE Servlet Specs.
So, is it possible that if i try to run same code on some different Web container (for eg Sun server /BEA Weblogic/IBM Websphere) i might get different result
I agree. Here's the link: http://aspose.com/file-tools
subject: Servlet Context parameters query
It's not a secret anymore!