0% found this document useful (0 votes)
105 views7 pages

Middleware - Servlet - MAVEN

The document discusses how to create a servlet project using Maven. It provides steps to generate a Maven project structure, add dependencies and plugins to the pom.xml file, write a simple "Hello World" servlet class, configure the servlet in web.xml, and run the project using the Jetty plugin. The key aspects covered are using Maven standards to manage dependencies and the build lifecycle, as well as the basic servlet configuration and deployment definitions required to execute the servlet.

Uploaded by

badsmile
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
105 views7 pages

Middleware - Servlet - MAVEN

The document discusses how to create a servlet project using Maven. It provides steps to generate a Maven project structure, add dependencies and plugins to the pom.xml file, write a simple "Hello World" servlet class, configure the servlet in web.xml, and run the project using the Jetty plugin. The key aspects covered are using Maven standards to manage dependencies and the build lifecycle, as well as the basic servlet configuration and deployment definitions required to execute the servlet.

Uploaded by

badsmile
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 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 ([Link] 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=[Link] -DartifactId=Tuto1 -DarchetypeArtifactId=maven-archetype-webapp

Base revision 7951cb8, Sun Dec 23 [Link] 2012 +0100, David Bromberg.

2 4. You should get the following tree directory in your le system : Tuto1 [Link] src main java resources webapp WEB-INF target [Link], 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 [Link] 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 [Link]
Line 1 5 10 -

<project> ... <repositories> <repository> <id>[Link]</id> <name>[Link] Repository for Maven 2</name> <url>[Link] </repository> </repositories> ... </project>

It must have only one xml node <respositories> in a [Link] le

David Bromberg

Base revision 7951cb8

[Link]

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

<project> ... <repositories> ... <repository> <id>[Link]</id> <name>[Link] Repository for Maven 2</name> <url>[Link] </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 [Link]
Line 1 5 10 15 -

package [Link]; import [Link].*; import [Link].*; import [Link].*; public class SimpleServlet extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { PrintWriter out = [Link](); [Link]( "Hello World\n" ); [Link](); [Link](); } }

David Bromberg

Base revision 7951cb8

[Link]

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 [Link] le as illustrated in the following : Listing 4 [Link] download
Line 1 5 10 -

<project> ... <dependencies> <dependency> <groupId>[Link]</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 [Link] 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

[Link]

9. Edit the [Link] 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 [Link]
Line 1 5 10 15 -

<project> ... <build> <plugins> <plugin> <groupId>[Link]</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 [Link] le It must have exactly one xml node<plugins></plugins> included in the node <build> in your [Link] le.

David Bromberg

Base revision 7951cb8

[Link]

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 [Link] le as following : Listing 6 [Link]
Line 1 5 10 15 -

<?xml version="1.0" encoding="UTF-8"?> <web-app version="2.4" xmlns="[Link] xmlns:xsi="[Link] xsi:schemaLocation="[Link] [Link] <display-name>Hello world servlet</display-name> <servlet> <servlet-name>FirstServlet</servlet-name> <servlet-class>[Link]</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 [Link] 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 [Link]. [Link] 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 [Link] 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 [Link]
Line 1 5 10 15

<project> ... <build> <plugins> ... <plugin> <groupId>[Link]</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 [Link] 5. Now you can go to the URL of your project, http ://localhost :8080/Tuto1/test
http ://[Link]/technetwork/java/javaee/servlet/[Link]

David Bromberg

Base revision 7951cb8

[Link]

You might also like