Java EE 7 SDK

Samples Main Page

# The Annotations Servlet Sample Application

This is a simple Servlet application that uses annotations for servlets, filters, and content listeners.

## Description

Servlet

The servlet reads the initial parameter, a request attribute and a context attribute and provides the result.

    @WebServlet(name="mytest",
                urlPatterns={"/"},
                initParams={ @WebInitParam(name="message",value="my servlet") } )
    public class TestServlet extends HttpServlet {
        public void init(ServletConfig config) throws ServletException {
            // read the context attribute
            ...
        }
        public void service(HttpServletRequest req, HttpServletResponse res) throws IOException, ServletException {
        }
    }

`@javax.servlet.annotation.WebServlet` is an annotation that specifies metadata for a servlet. In this case, it specifies the URL pattern and an initialization parameter. You need to extend `javax.servlet.http.HttpServlet`.

Filter

The filter reads the initial parameter and sets a request attribute.

    @WebFilter(urlPatterns={"/"},
               initParams={ @WebInitParam(name="mesg", value="my filter") })
    public class TestFilter implements Filter {
        public void init(FilterConfig filterConfig) throws ServletException {
            ...
        }
        public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain)
    	                     throws IOException, ServletException {
            ...
        }
        public void destroy() {
            ...
        }
    }

`@javax.servlet.annotation.WebFilter` is an annotation that specifies meta-data for a filter. In this case, it specifies the URL pattern and an initialization parameter. You need to extend `javax.servlet.Filter`.

ContextListener

The context listener sets a context attribute in the `contextInitialized()` method.

    @javax.servlet.annotation.WebListener
    public class TestServletContextListener implements ServletContextListener {
        public void contextInitialized(ServletContextEvent sce) {
            ...
        }
        public void contextDestroyed(ServletContextEvent sce) {
            ...
        }
    }

`@javax.servlet.annotaton.WebListener` indicates that a class is a context listener.

Deployment Descriptor

By using annotations, the deployment descriptor is no longer required.

GlassFish Server Specific Deployment Configuration

There is no need to define any GlassFish Server deployment descriptor (`sun-web.xml`) for this sample.

## Key Features

This sample demonstrates the following key features:

- Specifying metadata for a servlet using annotations.
- Specifying the URL pattern and initialization parameters using annotations instead of declaring them in the deployment descriptor.

## Building, Deploying, and Running the Application

Follow these instructions for building, deploying, and running this sample application:

1. Set up your build environment and configure the application server with which the build system has to work by following the common build instructions.
2. `app_dir` is the sample application base directory: `samples_install_dir/servlet/annotation-war`.
3. ````Change directory to `app_dir.
   `
4. Build and deploy the sample application using the `mvn` target:

   Use the command below to run this sample using the Cargo framework:

   `app_dir> mvn clean verify cargo:run`

   You can point Cargo to an already installed and running Glassfish server:

   `app_dir> mvn clean verify cargo:run -Dglassfish.home=$<glassfish_dir>` (e.g. ../glassfish4)

   You can also build, deploy the sample application without Cargo:

   `app_dir> mvn install`

   `app_dir> asadmin deploy ./target/<app_name>.war`

5. Run the sample as follows:
   - Use the browser to access
     `http://<javaee.server.name>:<javaee.server.port>/annotation-war`
     The expected message in the browser is the following:
     `Hello, my servlet, my filter, my listener.`
6. Use the glassfish command line to undeploy the application.

   `app_dir> asadmin undeploy <app_name>`

7. Use the target `clean` to remove the temporary directories like /target.

   `app_dir> mvn clean`

## Building, Deploying, and Running the Application in NetBeans IDE

Perform the following steps to build, deploy, and run the application using NetBeans IDE:

1. Refer to the common build instructions for setting up NetBeans IDE and Java EE 7 SDK.
2. In the NetBeans IDE, choose File → Open Project (Ctrl-Shift-O), navigate to the `samples_install_dir/servlet/` directory, select `annotation-war`, and click Open Project.
3. In the Projects tab, right click `annotation-war` and select Run to build, deploy, and run the project.

## Troubleshooting

If you have problems when running the application, refer the troubleshooting document.

Copyright © 1997-2013 Oracle and/or its affiliates. All rights reserved.