Transcript Document
Creating Servlet/JSP Web
application with Maven
Creating Directory Structure
• Maven 2 supports the notion of creating a
complete Web project template with a simple
command
• To create Web project template need to use
maven-archetype-webapp archetype. Type in
a single line:
mvn archetype:create
-DgroupId=com.maven2example
-DartifactId=your_webapp
-DarchetypeArtifactId=maven-archetype-webapp
Maven Web Directory Structure
<root>/src/main/webapp/
- directory structure for a WAR
Create folder for Java classes
• Manually create a folder
\your_webapp\src\main\java
• Servlet classes and other Java classes may be
placed in this folder
Add libraries
• Add dependencies to Maven configuration file
\your_webapp\pom.xml
Servlet:
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.1.0</version>
</dependency>
JSTL:
<dependency>
<groupId>taglibs</groupId>
<artifactId>standard</artifactId>
<version>1.1.2</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
<version>1.1.2</version>
</dependency>
Create settings for Eclipse
• In a project root execute:
mvn eclipse:eclipse
• This command will create files:
• \your_webapp\.project
• \your_webapp\.classpath
Develop your project in Eclipse
• Import project and create servlets in
src/main/java
• Do not forget to register your servlets in web.xml
Developing HTML/JSP
• Put HTML files inside
\your_webapp\src\main\webapp
• Put JSP files inside
\your_webapp\src\main\webapp
OR inside e.g.
\your_webapp\src\main\webapp\WEB-INF\jsp
Packaging
• Executing the command
mvn package
creates a WAR file inside \your_webapp\target
Running with Jetty
• It’s easy to run application by using Jetty
plugin for Maven
• Jetty is an open-source, standards-based,
full-featured web server implemented
entirely in Java
Running with Jetty
• Add the Jetty plugin to the pom.xml
<build>
<finalName>maven2example_webapp</finalName>
<plugins>
<plugin>
<groupId>org.mortbay.jetty</groupId>
<artifactId>jetty-maven-plugin</artifactId>
<version>8.1.13.v20130916</version>
</plugin>
</plugins>
</build>
Running with Jetty
• Execute mvn jetty:run command
>mvn jetty:run
[INFO] Scanning for projects...
. . .
[INFO] --- jetty-maven-plugin:8.1.13.v20130916:run (default-cli)
@ servlet-jpa-app --[INFO] Configuring Jetty for project: servlet-jpa-app
[INFO] webAppSourceDirectory not set. Defaulting to
C:\tmp\servlet-jpa-app\src\main\webapp
[INFO] Reload Mechanic: automatic
[INFO] Classes = C:\tmp\servlet-jpa-app\target\classes
[INFO] Context path = /servlet-jpa-app
[INFO] Tmp directory = C:\tmp\servlet-jpa-app\target\tmp
[INFO] Web defaults = org/eclipse/jetty/webapp/webdefault.xml
[INFO] Web overrides = none
[INFO] web.xml file = file:/C:/tmp/servlet-jpa-app/src/main/webapp/WEB-INF/web.xml
[INFO] Webapp directory = C:\tmp\servlet-jpa-app\src\main\webapp
...
[INFO] Started Jetty Server
Stop by Ctrl+C
Opening the Application
Open your web browser to
http://localhost:8080/
Opening the Application
Valid URL is
http://localhost:8080/your_webapp/
Debugging
• Set environment variable MAVEN_OPTS
with value
-Xdebug -Xnoagent -Djava.compiler=NONE Xrunjdwp:transport=dt_socket,address=8787,
server=y,suspend=n
• E.g. create a BAT file for running Jetty:
set MAVEN_OPTS=-Xdebug -Xnoagent Djava.compiler=NONE Xrunjdwp:transport=dt_socket,address=8787,serve
r=y,suspend=n
mvn jetty:run