Tuesday, March 12, 2013

First Hello World Web Application using Maven

To create a simple Servlet web application using Maven in Eclipse.
In this example, a simple web application is created using Maven in Eclipse. A servlet is created to output a "Hello World~~~" sentance when called.
Maven/Jetty plugin is used for running the application.
Implementation
  1. Create a Maven Project in Eclipse.
    By following the procedures below, an Eclipse project with the followings:
    1. A Java source folder, /src/main/java;
    2. A Resource source folder, /src/main/resources;
    3. A Java source folder for testing, /src/test/java;
    4. A Resource source folder for testing, /src/test/resources;
    5. A Web Application source folder, /src/main/webapp;
    6. A WEB-INF folder, /src/main/webapp/WEB-INF; and
    7. pom.xml.

    Figure 1: Created Maven Project
    Procedures
    1. Select "File"->"Project..." from the menu. In the "New Project" dialog, select "Maven Project". Click the "Next" button.
      Figure 2: New Maven Project
    2. In the "New Project - Select project name and location" dialog, check the option "Create a simple project (skip archetype selection)". Click the "Next" button.
      Figure 3: New Maven Project - Select project name and location.
    3. In the "New Project - Configure Project" dialog, input the Group Id, Artifact Id and Name.  Select "war" as the "Packaging". Click the "Next" button. The project will then be created.
      Figure 4: New Project - Configure Project
  2. Update the pom.xml.
    • If context is not defined, <artifactId> will be used as the context of the application.
    <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.blogspot.adaprognotebook</groupId> <artifactId>helloweb</artifactId> <packaging>war</packaging> <version>1.0-SNAPSHOT</version> <name>Hello World Webapp</name> <repositories> <repository> <id>java.net2</id> <name>Repository hosting the Java EE artifacts</name> <url>http://download.java.net/maven/2</url> </repository> <repository> <id>sonatype-nexus-snapshots</id> <name>Sonatype Nexus Snapshots for the jetty-maven plugin</name> <url>https://oss.sonatype.org/content/repositories/snapshots</url> <releases> <enabled>false</enabled> </releases> <snapshots> <enabled>true</enabled> </snapshots> </repository> </repositories> <dependencies> <dependency> <groupId>javax</groupId> <artifactId>javaee-web-api</artifactId> <version>${javaee.version}</version> <scope>provided</scope> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.mortbay.jetty</groupId> <artifactId>jetty-maven-plugin</artifactId> <version>${jetty.version}</version> <configuration> <scanIntervalSeconds>30</scanIntervalSeconds> <stopPort>7788</stopPort> <stopKey>foo</stopKey> <webApp> <descriptor>${basedir}/src/main/webapp/WEB-INF/web.xml</descriptor> </webApp> </configuration> </plugin> </plugins> </build> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <javaee.version>6.0</javaee.version> <jetty.version>8.1.9.v20130131</jetty.version> </properties> </project>
  3. Create the Java Servlet in /src/main/java.
    package com.blogspot.adaprognotebook; import java.io.IOException; import java.io.PrintWriter; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; public class HelloWorld extends HttpServlet { @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { PrintWriter out = resp.getWriter(); out.println("<html>"); out.println("<head>"); out.println("<title>Hello World Servlet</title>"); out.println("</head>"); out.println("<body>"); out.println("<h2>Hello World~~~</h2>"); out.println("</body>"); out.println("</html>"); out.close(); } }
  4. Modify web.xml in /src/main/webapp/WEB-INF.
    <?xml version="1.0" encoding="UTF-8"?> <web-app id="HelloWebApp" version="3.0" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" metadata-complete="true"> <servlet> <display-name>First Hello World Servlet</display-name> <servlet-name>Hello</servlet-name> <servlet-class>com.blogspot.adaprognotebook.HelloWorld</servlet-class> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>Hello</servlet-name> <url-pattern>/hello.view</url-pattern> </servlet-mapping> </web-app>
  5. Start Jetty by running a new configuration with goal "jetty:run", and view the web page in browser using URL http://localhost:8080/helloweb/hello.view.
    Figure 5: Hello World Servlet

No comments:

Post a Comment