Maven by Example
   - 5.6. Adding J2EE Dependencies

5.6. Adding J2EE Dependencies

To write a servlet, we’ll need to add the Servlet API as a dependency to the project’s POM.

Add the Servlet 2.4 Specification as a Dependency. 

<project>
    [...]
    <dependencies>
        [...]
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>servlet-api</artifactId>
            <version>2.4</version>
            <scope>provided</scope>
        </dependency>
    </dependencies>
    [...]
</project>

It is also worth pointing out that we have used the provided scope for this dependency. This tells Maven that the JAR is "provided" by the container and thus should not be included in the WAR. If you were interested in writing a custom JSP tag for this simple web application, you would need to add a dependency on the JSP 2.0 API. Use the configuration shown in this example:

Adding the JSP 2.0 Specification as a Dependency. 

<project>
    [...]
    <dependencies>
        [...]
        <dependency>
            <groupId>javax.servlet.jsp</groupId>
            <artifactId>jsp-api</artifactId>
            <version>2.0</version>
            <scope>provided</scope>
        </dependency>
    </dependencies>
    [...]
</project>

Once you’ve added the Servlet specification as a dependency, run mvn clean install followed by mvn jetty:run.

Note

mvn jetty:run will continue to run the Jetty servlet container until you stop the process with CTRL-C. If you started Jetty in Section 5.4, “Configuring the Jetty Plugin”, you will need to stop that process before starting Jetty a second time.

[tobrien@t1 simple-webapp]$ mvn clean install
...
[tobrien@t1 simple-webapp]$ mvn jetty:run
[INFO] [jetty:run]
...
2007-12-14 16:18:31.305::INFO:  jetty-6.1.6rc1
2007-12-14 16:18:31.453::INFO:  No Transaction manager found
2007-12-14 16:18:32.745::INFO:  Started SelectChannelConnector@0.0.0.0:8080
[INFO] Started Jetty Server

At this point, you should be able to retrieve the output of the SimpleServlet. From the command line, you can use curl to print the output of this servlet to standard output:

~/examples/ch-simple-web $ curl http://localhost:8080/simple-webapp/simple
SimpleServlet Executed











Become a Member

Are you a current user of:

Top