Middleware - Servlet - MAVEN

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 7

Middleware Servlet MAVEN

23 d ecembre 2012

1
Building a servlet
aven is a project management tool which encompasses a project object M model, a set of standards, a project lifecycle, a dependency management system, and logic for executing plugin goals at dened phases in a lifecycle. The specicity of maven is to leverage on a set of standards to compile code, create le, deploy WAR , etc. One of our main objective is to study how to create/manage a maven project. 1. Download maven 3.0.4 (http://maven.apache.org/download.html) 2. Unzip maven in your home directory, update your PATH variable of your existing shell. # export M2_HOME=r epertoire_maven # export PATH=$PATH:$M2_HOME/bin 3. Enter the following command line to create a new maven project called Tuto1 # mvn archetype:generate -DgroupId=net.tuto1.ws.service -DartifactId=Tuto1 -DarchetypeArtifactId=maven-archetype-webapp

Base revision 7951cb8, Sun Dec 23 21:37:53 2012 +0100, David Bromberg.

2 4. You should get the following tree directory in your le system : Tuto1 pom.xml src main java resources webapp WEB-INF target pom.xml, this le describes in details your current project src, contains all the source le of your project main/java, contains particularly all your Java source les that will be included in the generated JAR, WAR packages, etc. . . main/webapp, contains .jsp pages main/WEB-INF, contains servlet conguration les target, in this directory you will nd code that has been generated from source les, etc. . . Some of your directory may not be created as for example src/main/java. In this case, you should create them by hand. 5. Edit the pom.xml which is at the root of your project directory (under the Tuto1 directory). Add the following directories to enable maven to download the required libraries to compile your project. Listing 1 pom.xml
Line 1 5 10 -

<project> ... <repositories> <repository> <id>maven2-repository.dev.java.net</id> <name>Java.net Repository for Maven 2</name> <url>http://download.java.net/maven/2/</url> </repository> </repositories> ... </project>

It must have only one xml node <respositories> in a pom.xml le

David Bromberg

Base revision 7951cb8

www.david.bromberg.fr

6. If you have any issues with the remote repositories, you can add the additional repository. Listing 2 pom.xml
Line 1 5 10 -

<project> ... <repositories> ... <repository> <id>maven2-bromberg.dev.java.net</id> <name>Java.net Repository for Maven 2</name> <url>http://download.java.net/maven/2/</url> </repository> ... </repositories> ... </project>

Designing the servlet


7. You should create the following Java servlet in the src/main/java/net/tuto1/ws directory. Listing 3 SimpleServlet.java
Line 1 5 10 15 -

package net.tuto1.ws; import java.io.*; import javax.servlet.*; import javax.servlet.http.*; public class SimpleServlet extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { PrintWriter out = response.getWriter(); out.println( "Hello World\n" ); out.flush(); out.close(); } }

David Bromberg

Base revision 7951cb8

www.david.bromberg.fr

8. Enter either mvn clean install or mvn compile command in your shell at the root of your project. The compilation phase may fail as it lacks some dependencies. To overcome this issue, you should add the servlet API dependency in your pom.xml le as illustrated in the following : Listing 4 pom.xml download
Line 1 5 10 -

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

It must have exactly one xml node <dependencies></dependencies> included in your pom.xml le. <scope>provided</scope> indicate to maven that the corresponding dependency should not be included in the generated war package.

David Bromberg

Base revision 7951cb8

www.david.bromberg.fr

9. Edit the pom.xml le located in the root of your Tuto1 project. You must specify which compiler version you wish to use to compile your project. In our specic case, we want to produce 1.5 compliant Java bytecode as we are going to use annotations in Java source les. Listing 5 pom.xml
Line 1 5 10 15 -

<project> ... <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>2.0.2</version> <configuration> <source>1.5</source> <target>1.5</target> </configuration> </plugin> </plugins> </build> ... </project>

It must have exactly one xml node <build> in your pom.xml le It must have exactly one xml node<plugins></plugins> included in the node <build> in your pom.xml le.

David Bromberg

Base revision 7951cb8

www.david.bromberg.fr

2
Servlet deployment
Conguration
1. before deploying your servlet, you must congure it to make it works into a servlet container. To this end, go in the /src/main/webapp/WEB-INF directory and edit the web.xml le as following : Listing 6 web.xml
Line 1 5 10 15 -

<?xml version="1.0" encoding="UTF-8"?> <web-app version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"> <display-name>Hello world servlet</display-name> <servlet> <servlet-name>FirstServlet</servlet-name> <servlet-class>net.tuto1.ws.SimpleServlet</servlet-class> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>FirstServlet</servlet-name> <url-pattern>/test</url-pattern> </servlet-mapping> </web-app>

2. The web.xml le has been standardized through the JSR-315 . You absolutely must remember the meaning of these main xml tags, which are fundamental to make your Web Service running without issues. The <servlet>...</servlet> tags indicate which servlet will be loaded by the servlet container when HTTP requests arrived. More particularly, in our context, we are using the Sun/Oracle web service stack. Thus we use the following class com.sun.xml.ws.transport. http.servlet.WSServlet to receive all HTTP requests. The tags <servlet-mapping>...</servlet-mapping>, enable to specify which servlet should be executed according to the URL asked by the HTTP client. The tags <url-pattern>...</url-pattern>, dene a pattern that will trigger the HTTP redirection to the adequate servlet. For instance, <urlpattern>/services/*</url-pattern> maps all HTTP requests having in their URL the pattern /services to the servlet named WebServicePort. 3. Edit the pom.xml le located at the root of your Tuto1 project to specify which servlet container you wish to use. We are going to use the Jetty container, and its corresponding plugin to start and stop it on the y from the command line. Listing 7 pom.xml
Line 1 5 10 15

<project> ... <build> <plugins> ... <plugin> <groupId>org.mortbay.jetty</groupId> <artifactId>jetty-maven-plugin</artifactId> <version>8.1.5.v20120716</version> </plugin> ... </plugins> </build> ... </project>

4. Once the Jetty plugin congured, you can run your web application thanks to the following command line : mvn jetty:run dans votre shell. To get more information on how the plugin works, have a look to the following URL http://wiki.eclipse.org/Jetty/Feature/Jetty_Maven_Plugin. 5. Now you can go to the URL of your project, http ://localhost :8080/Tuto1/test
http ://www.oracle.com/technetwork/java/javaee/servlet/index.html

David Bromberg

Base revision 7951cb8

www.david.bromberg.fr

You might also like