Is there a way two (or more) servlets can share config parameter, declared once in web.xml?
Looked here, but it doesn't seem to be the answer.
The use-case is fairly simple: I have two servlets: one uploads files to a directory, other downloads them. I would be happy to enlist the directory/path only once in web.xml to avoid ambiguity/confusions.
Yes, add a <context-param> to your web.xml, e.g.
<context-param>
<param-name>myParam</param-name>
<param-value>Some value</param-value>
</context-param>
This is scoped to the webapp as a whole, rather than individual servlets.
You can then obtain this in your servlet(s) from the getInitParameter(...) method of the ServletContext object (which in turn can be obtained using getServletContext() in your servlet).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With