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

/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:

javax.servlet javax.servlet-api 3.1.0

JSTL:

taglibs standard 1.1.2 javax.servlet jstl 1.1.2

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

maven2example_webapp org.mortbay.jetty jetty-maven-plugin 8.1.13.v20130916

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