Adding Web Annotation Tags

Annotation-based programming tags are used within your Web projects as providers of metadata that is then used to generate other application artifacts as required. You add these tags to your code in the same way that you add Javadoc comments.

About this task

Adding class-level annotations can be done using the Generate an annotated <artifact> class, where <artifact> might be a servlet or JSP that you are creating within your Web project.

Procedure

You can automatically generate an annotated class in your Web project. Select Generate an annotated <artifact> class, and it creates a default annotated class for you.

For example, the code for the annotated servlet class is as follows:

Annotated servlet class code

Example

This default annotated servlet class illustrates the three basic parts of annotation-based programming:
  • The first element is the phrase
    * Servlet implementation class for Servlet: MyServlet

    This phrase represents the source code comment explaining the function / purpose of this class.

  • The second element includes the asterisk and @ symbol:
    * @

    The @ sign indicates that what follows is an annotation that needs to be processed.

  • The third element is the body of the annotation tag:
     * @web.servlet
     *   name="myServlet"
     *   display-name="myServlet" 
     *
     * @web.servlet-mapping
     *   url-pattern="/myServlet"
     *  

    This code contains the annotations that is used to generate the artifacts. In this case, the annotation specifies that it is of the web.servlet type, which defines data relating to servlets. The tag also specifies values for the parameters name, display-name, and url-pattern.

What to do next

The artifacts specified by the annotation tags are generated at run time.

Related Reference


Feedback