//2012-10-01 johnpfeiffer
(PREREQUISITES: maven and eclipse installed)
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
IN ECLIPSE GUI
New Project -> Maven Project (simple skip archetype suggestion)
Group Id, Artifact Id, Version, Packaging (war)
Right click src/main/java -> Create a package (matches GroupId)
Right click the package -> Create a new Class (ServletExample.java)
package net.kittyandbear;
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 ServletExample extends HttpServlet
{
private static final long serialVersionUID = 1L;
protected void doGet( HttpServletRequest request , HttpServletResponse response ) throws ServletException , IOException
{
response.setContentType( "text/html" ); // MIME type
PrintWriter servletresponse = null;
servletresponse = response.getWriter();
servletresponse.println( "hi" );
}
protected void doPost( HttpServletRequest request , HttpServletResponse response ) throws ServletException , IOException
{
doGet( request, response);
}
} //end class
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
ALTERNATE MAVEN COMMAND LINE (must be imported into Eclipse)
mvn archetype:create
-DgroupId=net.kittyandbear
-DartifactId=webapp
-DarchetypeArtifactId=maven-archetype-webapp
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
src -> main -> webapp and Right click New Folder "WEB-INF"
src -> main -> webapp -> WEB-INF and Right click -> Create a new file (web.xml)
ServletExample/src/main/webapp/WEB-INF/web.xml
<?xml version="1.0" encoding="ISO-8859-1"?>
<web-app
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"
version="2.5">
<servlet>
<servlet-name>ServletExample</servlet-name>
<servlet-class>net.kittyandbear.ServletExample</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>ServletExample</servlet-name>
<url-pattern>/ServletExample/*</url-pattern>
</servlet-mapping>
</web-app>
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Modify the pom.xml
<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>kittyandbear.net</groupId>
<artifactId>ServletExampleMaven</artifactId>
<version>0.1</version>
<packaging>war</packaging>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>2.5</version>
<scope>provided</scope>
</dependency>
</dependencies>
<build>
<finalName>ServletExample</finalName>
<plugins>
<plugin>
<groupId>org.apache.tomcat.maven</groupId>
<artifactId>tomcat7-maven-plugin</artifactId>
<version>2.0</version>
<configuration>
<port>8080</port>
<path>/</path>
</configuration>
</plugin>
</plugins>
</build>
</project>
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
mvn clean install tomcat7:run
(DOWNLOADS TOMCAT7 - A BUNCH OF STUFF, TO YOUR LOCAL MAVEN REPOSITORY AND THEN RUNS YOUR SERVLET)
Downloading: http://repo.maven.apache.org/maven2/org/apache/tomcat/maven/tomcat7-maven-plugin/2.0/tomcat7-maven-plugin-2.0.pom
...
Downloaded: http://repo.maven.apache.org/maven2/org/eclipse/jdt/core/compiler/ecj/3.7.1/ecj-3.7.1.jar (1708 KB at 290.0 KB/sec)
[INFO] Running war on http://localhost:8080/
[INFO] Creating Tomcat server configuration at C:\Users\John\Desktop\opt\workspace\ServletExampleMaven\target\tomcat
[INFO] create webapp with contextPath:
Oct 1, 2012 10:11:32 AM org.apache.coyote.AbstractProtocol init
INFO: Initializing ProtocolHandler ["http-bio-8080"]
Oct 1, 2012 10:11:32 AM org.apache.catalina.core.StandardService startInternal
INFO: Starting service Tomcat
Oct 1, 2012 10:11:32 AM org.apache.catalina.core.StandardEngine startInternal
INFO: Starting Servlet Engine: Apache Tomcat/7.0.30
Oct 1, 2012 10:11:35 AM org.apache.coyote.AbstractProtocol start
INFO: Starting ProtocolHandler ["http-bio-8080"]
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
BROWSE TO HTTP://localhost:8080/ServletExample/ServletExample to see "hi"
OR take advantage of the servlet mapping: http://localhost:8080/ServletExample
in the Console use Control + C to stop the tomcat server
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
TROUBLESHOOTING
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-war-plugin:2.1.1:war (default-war) on project ServletExampleMaven: Error assembling WAR: webxml attribute is required (or pre-existing WEB-INF/web.xml if executing in update mode) -> [Help 1]
Make sure you have created the correct WEB-INF subfolder (must be under "webapps") and web.xml