Google

Mar 4, 2014

Creating a simple Java Web project with Maven tutorial -- Part 1

Three  part tutorial for Java beginners to create a simple web application (i.e. a Web ARchive file -- i.e. a war file).
  1. Part-1: Without using an IDE like eclipse. Just using Maven, Java, and text editor like Notepad++.
  2. Part-2: Analyzing the war file and Maven pom.xml file.
  3. Part-3Import the project into eclipse and use m2e plugin for Maven within eclipse.
 Step 1: Execute the Maven maven-archetype-webapp command to create the Maven web structure with the following command on a DOS command prompt. This assumes that you have set up Java and Maven.


mvn archetype:generate -DgroupId=com.mytutorial -DartifactId=simpleWeb  -DarchetypeArtifactId=maven-archetype-webapp


Enter "0.1-SNAPSHOT" when prompted for the version and "Y" to package: com.mytutorial.

Step 2: You can verify the structure created in the file system as shown below. pom.xml is the maven file to define build details and web.xml is web deployment descriptor file.




Step 3: open the pom.xml file in a text editor like Notepad++. If you don't already have it, download it. Add servlet-api as a dependency.

<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.mytutorial</groupId>
  <artifactId>simpleWeb</artifactId>
  <packaging>war</packaging>
  <version>0.1-SNAPSHOT</version>
  <name>simpleWeb Maven Webapp</name>
  <url>http://maven.apache.org</url>
  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>
 
 <dependency>
  <groupId>javax.servlet</groupId>
  <artifactId>servlet-api</artifactId>
                <version>2.5</version>
 </dependency>
 
  </dependencies>
  <build>
    <finalName>simpleWeb</finalName>
  </build>
</project>


Step 4: Define a simple servlet class in a package com.mytutorial. You need to first create a folder named "java" under src/main. Then create folders com and com/mytutorial under the folder java. You can use the notepad++

Create a Java class named  SimpleServlet.java

package com.mytutorial;

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 SimpleServlet extends HttpServlet {

 private static final long serialVersionUID = 1L;

 @Override
 public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
  PrintWriter out = response.getWriter();
  out.println("<html>");
  out.println("<body>");
  out.println("<h1>Simple</h1>");
  out.println("</body>");
  out.println("</html>");
 }

}

Step 5: Go to the DOS command prompt and change directory to simpleWeb where the pom.xml file is. type the command mvn compile. This should compile your SimpleServlet.java.

C:\Users\akumaras\projects\simpleWeb>mvn compile
[INFO] Scanning for projects...
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building simpleWeb Maven Webapp 0.1-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ simpleWeb ---
[WARNING] Using platform encoding (Cp1252 actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] Copying 0 resource
[INFO]
[INFO] --- maven-compiler-plugin:2.5.1:compile (default-compile) @ simpleWeb ---
[WARNING] File encoding has not been set, using platform encoding Cp1252, i.e. build is platform dependent!
[INFO] Compiling 1 source file to C:\Users\akumaras\projects\simpleWeb\target\classes
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 1.399s
[INFO] Finished at: Wed Feb 26 17:29:02 EST 2014
[INFO] Final Memory: 7M/243M

Note that in the file system SimpleServlet.class file gets created under target/classes/com/mytutorial.

Step 6: You need to define the web description in the web.xml descriptor file.  You need to configure your Servlet details here.

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" 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_2_5.xsd">

 <display-name>Simple web app</display-name>

 
 <servlet>
  <servlet-name>Simple Servlet</servlet-name>
  <servlet-class>com.mytutorial.SimpleServlet</servlet-class>
 </servlet>

 <servlet-mapping>
  <servlet-name>Simple Servlet</servlet-name>
  <url-pattern>/simple</url-pattern>
 </servlet-mapping>


</web-app>


Step 7: Java web applications are packaged as a war file. In order to package it, you need to run the Maven command mvn package.

C:\Users\akumaras\projects\simpleWeb>mvn package
[INFO] Scanning for projects...
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building simpleWeb Maven Webapp 0.1-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ simpleWeb ---
[WARNING] Using platform encoding (Cp1252 actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] Copying 0 resource
[INFO]
[INFO] --- maven-compiler-plugin:2.5.1:compile (default-compile) @ simpleWeb ---
[INFO] Nothing to compile - all classes are up to date
[INFO]
[INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ simpleWeb ---
[WARNING] Using platform encoding (Cp1252 actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] skip non existing resourceDirectory C:\Users\akumaras\projects\simpleWeb\src\test\resources
[INFO]
[INFO] --- maven-compiler-plugin:2.5.1:testCompile (default-testCompile) @ simpleWeb ---
[INFO] No sources to compile
[INFO]
[INFO] --- maven-surefire-plugin:2.12.4:test (default-test) @ simpleWeb ---
[INFO] No tests to run.
[INFO]
[INFO] --- maven-war-plugin:2.2:war (default-war) @ simpleWeb ---
[INFO] Packaging webapp
[INFO] Assembling webapp [simpleWeb] in [C:\Users\akumaras\projects\simpleWeb\target\simpleWeb]
[INFO] Processing war project
[INFO] Copying webapp resources [C:\Users\akumaras\projects\simpleWeb\src\main\webapp]
[INFO] Webapp assembled in [53 msecs]
[INFO] Building war: C:\Users\akumaras\projects\simpleWeb\target\simpleWeb.war
[INFO] WEB-INF\web.xml already added, skipping
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 1.575s
[INFO] Finished at: Wed Feb 26 17:45:45 EST 2014
[INFO] Final Memory: 6M/243M
[INFO] ------------------------------------------------------------------------

Inspect your file system for the war file simpleWeb.war under target. If you are curious to see what files are inside, you can unzip it with a WinZip utility.


Step: This war file can be deployed to any web container or application server. For example, Tomcat, JBoss, Weblogic, etc. I deployed this to my local Weblogic server running on port 7002.

http://localhost:7002/simpleWeb/simple


Where "simpleWeb" is the name of the war file you had created, and "simple" is the "url-pattern" you defined in your web.xml file.



A simple Java web tutorial without any IDE like eclipse. This also gives you an idea as to how Maven works.

Labels: ,

0 Comments:

Post a Comment

Subscribe to Post Comments [Atom]

<< Home