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="admin@example.com"))
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-paramcan be overriden by configuring aservletwith a matching name inweb.xml: