Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sharing config parameter among servlets

Tags:

java

servlets

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.

like image 396
BreakPhreak Avatar asked Feb 28 '26 13:02

BreakPhreak


1 Answers

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).

like image 194
skaffman Avatar answered Mar 02 '26 03:03

skaffman