Java Servlet Annotation – Servlet API 3.0 and Above

Java Servlet Annotation

1.) The javax.servlet.annotation package contains a number of annotations that allow users to use annotations to declare servlets, filters, listeners and specify the metadata for the declared component.

2.)Servlet API 3.0 introduces a new package javax. servlet.annotation.

3.) Servlet API 3.0 provides annotation types used to annotating a servlet class.

4.) Annotation in Servlet API 3.0 replace xml configuration – web deployment descriptor file (web.xml) for servlet declaration and Servlet mapping.

5.) Servlet API 3.0 Annotation Types:

i.) @HandlesTypes

Description : Annotation type is used to declare the class types that a ServletContainerInitializer can handle. It has one attribute, value, that is used to declare the class types. For example, the following ServletContainerInitializer is annotated with @HandleTypes that declares that the initializer can handle UsefulServlet.

 

ii.) @HttpConstraint

Description :  This annotation is used within the ServletSecurity annotation to represent the security constraints to be applied to all HTTP protocol methods for which a corresponding HttpMethodConstraint element does NOT occur within the ServletSecurity annotation.

iii.) @HttpMethodConstraint

Description : This annotation is used within the ServletSecurity annotation to represent security constraints on specific HTTP protocol messages.

iv.) @MultipartConfig

Description : Annotation that may be specified on a Servlet class, indicating that instances of the Servlet expect requests that conform to the multipart/form-data MIME type.Servlets annotated with MultipartConfig may retrieve the Part components of a given multipart/form-data request by calling getPart or getParts.

v.) @ServletSecurity

Description : This annotation is used on a Servlet implementation class to specify security constraints to be enforced by a Servlet container on HTTP protocol messages. The Servlet container will enforce these constraints on the url-patterns mapped to the servlets mapped to the annotated class.

vi.)@WebFilter

Description : This annotation is processed by the container at deployment time, and the corresponding filter applied to the specified URL patterns, servlets, and dispatcher types.

vii.) @WebInitParam

Description : This annotation is used on a Servlet or Filter implementation class to specify an initialization parameter.

viii.) @WebListener

Description This annotation is used to declare a WebListener. Any class annotated with WebListener must implement one or more of the ServletContextListener, ServletContextAttributeListener,ServletRequestListener, ServletRequestAttributeListener, HttpSessionListener, or HttpSessionAttributeListener interfaces.

x.) @WebServlet

Description : This annotation is processed by the container at deployment time, and the corresponding servlet made available at the specified URL patterns.