Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Use cases for @WebInitParam

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?

like image 940
Robert Munteanu Avatar asked Nov 09 '11 11:11

Robert Munteanu


Video Answer


2 Answers

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>
like image 113
Ramesh PVK Avatar answered Sep 19 '22 12:09

Ramesh PVK


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.

like image 28
Piotr Nowicki Avatar answered Sep 18 '22 12:09

Piotr Nowicki