Servlet 3.0 Annotations


Following are the annotations that MUST be supported by a Servlet 3.0 compliant web container.

1. @WebServlet

    • This annotation is used to define a Servlet component in a web application.
    • This annotation is specified on a class and contains metadata about the Servlet being declared. The urlPatterns or the value attribute on the annotation MUST be present.
    • It is recommended to use value when the only attribute on the annotation is the url pattern and to use the urlPatterns attribute when the other attributes are also used. It is illegal to have both value and urlPatterns attribute used together on the same annotation.
    • The default name of the Servlet if not specified is the fully qualified class name.
    • The annotated servlet MUST specify at least one url pattern to be deployed.
    • If the same servlet class is declared in the deployment descriptor under a different  name, a new instance of the servlet MUST be instantiated.
    • If the same servlet class is added to the ServletContext via the programmatic API “Programmatically adding and configuring Servlets”  the values declared via the @WebServlet annotation MUST be ignored and a new instance of the servlet with the name specified MUST be created.
    • Classes annotated with @WebServlet class MUST extend the javax.servlet.http.HttpServlet class.
    • Example

 

@WebServlet(”/foo”)
public class CalculatorServlet extends HttpServlet{
...
}
@WebServlet(name=”MyServlet”, urlPatterns={"/foo", "/bar"})
public class SampleUsingAnnotationAttributes extends HttpServlet{
public void doGet(HttpServletRequest req, HttpServletResponse res) {
...}
}

2. @WebFilter

    • This annotation is used to define a Filter in a web application.
    • The default name of the Filter if not specified is the fully qualified class name.
    • The urlPatterns attribute, servletNames attribute or the value attribute of the annotation MUST be specified.
    • Classes annotated with @WebFilter MUST implement javax.servlet.Filter.
    • Example

 

@WebFilter(“/foo”)
public class MyFilter implements Filter 
{
public void doFilter(HttpServletRequest req, HttpServletResponse res){
...
}
}

3. @WebInitParam

    • This annotation is used to specify any init parameters that must be passed to the Servlet or the Filter. It is an attribute of the WebServlet and WebFilter annotation.
    • Example

 

@WebServlet(value="/hello",initParams = {@WebInitParam(name="foo", value="Hello "),
                                         @WebInitParam(name="bar", value=" World!")
                     })
public class HelloServlet extends GenericServlet {
.....
}

4. @WebListener

    • The WebListener annotation is used to annotate a listener to get events for various operations on the particular web application context. Classes annotated with @WebListener MUST implement one of the following interfaces:
      javax.servlet.ServletContextListener
      javax.servlet.ServletContextAttributeListener
      javax.servlet.ServletRequestListener
      javax.servlet.ServletRequestAttributeListener
      javax.servlet.http.HttpSessionListener
      javax.servlet.http.HttpSessionAttributeListener

 

5. @MultipartConfig

    • This annotation, when specified on a Servlet, indicates that the request it expects is of type mime/multipart. The HttpServletRequest object of the corresponding servlet MUST make available the mime attachments via the getParts and getPart methods to iterate over the various mime attachments.
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s