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


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


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:


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.

Programmatically adding and configuring web components

The ability to programmatically add a servlet and filter to a context is useful for framework developers.

package com.pkg.servlet30;

import java.util.HashMap;
import java.util.Map;
import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import javax.servlet.ServletRegistration;
import javax.servlet.annotation.WebListener;

public class SimpleServletContextListener implements ServletContextListener {

 public void contextInitialized(ServletContextEvent sce) {
 ServletContext context = sce.getServletContext();

 String simpleServletName = "simpleServlet";
 String simpleFilterName = "simpleFilter";

 Map<String, String> initParams = new HashMap<String, String>();
 initParams.put("param1", "value1");
 initParams.put("param2", "value2");

 String[] urlPatterns = new String[]{"/simple"};

 ServletRegistration.Dynamic addServlet = context.addServlet(simpleServletName, NewServlet.class);

 context.addFilter(simpleFilterName, NewFilter.class);


 public void contextDestroyed(ServletContextEvent sce) {




The code is straightforward. It acquires the reference to ServletContext and makes use of the methods addServlet(), addMapping(), setInitParameters() and addFilter() for dynamically adding web components.

JSP Page and JSP Document

  • The difference between JSP Page and XML-BASED JSP Document is of syntax.
  • In JSP page we can put normal text but in JSP Document have <jsp:text> tag.
  • The XML syntax gives the JSP page author less complexity and more flexibility.
  • Jsp pages are saved having file extension of .jsp , while JSP documents having .jspx extension.