Since the Servlet 3.0 specification there is the possibility of declaring servlet mapping metadata as annotation on the servlet class:
@WebServlet(name="appInfoServlet", urlPatterns ="/appInfo", initParams = @WebInitParam(name="ocwd.deployer.email", value="[email protected]"))
public class AppInfoServlet extends HttpServlet {
}
What I do not understand though is the use case for keeping init parameters in the same class as the servlet. As far as I understand these parameters are to be kept separate from the class and placed into the deployment descriptor.
What use cases are there for specifying init parameters within the @WebServlet
annotation?
The annotations are used to give the default values.
In JavaEE the deployment properties can also be provided using annotations. Given the values for annotations, the deployment descriptor i.e, web.xml can still be used to override the default values provided by the annotations.
In the example above, the init-param
can be overriden by configuring a servlet
with a matching name in web.xml
:
<servlet>
<servlet-name>appInfoServlet</servlet-name>
<init-param>
<param-name>ocwd.deployer.email</param-name>
<param-value>[email protected]</param-value>
</init-param>
</servlet>
I can think of one, from the top of my head: provide the default value (i.e. by the class designer).
If the user of this class is fine with the default value, he don't need to add anything and just uses it. If he's not - he can modify it using the DD.
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