Use cases for @WebInitParam

前端 未结 3 948
既然无缘
既然无缘 2021-02-02 14:41

Since the Servlet 3.0 specification there is the possibility of declaring servlet mapping metadata as annotation on the servlet class:

@WebServlet(name=\"appInfo         


        
相关标签:
3条回答
  • 2021-02-02 14:55

    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>noreply@example.com</param-value>
        </init-param>
      </servlet>
    
    0 讨论(0)
  • 2021-02-02 14:56

    I think the use case is like other use cases for other annotations in various frameworks where we used separate XML prior to annotations.

    You can say the same about JAXB annotations. Really, you can implement one class and use multiple strategies of its mapping to XML. But once you move to annotations you create kind of tight coupling between class and metadata. The same is relevant for Spring annotations. Etc.

    In practice we rarely deploy the same servlet twice using different configuration or use the same EJB twice or map class to different XML schemas. But in this case it is very convenient to store metadata together with code. This problem is solved in java with annotations.

    Bottom line: use this definition in concrete application where each servlet has certain functionality and role and by definition is not reusable and tightly coupled with its URL mapping and configuration. Do not use this if you a creating environment like Struts or Spring controller. In this case the application programmer should be able to configure the servlet.

    0 讨论(0)
  • 2021-02-02 15:05

    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.

    0 讨论(0)
提交回复
热议问题