JSP/Jetty web applicaton with Maven

December 2016



Web application


A web application is a program using the web browser as an interface, but all data processing is handled on the server side. This allows web pages to be dynamically generated based in the information provided by the user. In this article we shall focus on creating a simple project with JavaServer (JSP), to generate pages a Jetty web server, whose installation and use will be fully managed by Maven. Note that: To test the code, it is necessary to have a minimum knowledge in Maven, it must be properly installed on your machine. Learn more Maven - Getting started

The JSP project


At first , we must create the project structure with the maven-archetype-webapp archetype.

mvn archetype:generate -DarchetypeArtifactId=maven-archetype-webapp -DgroupId=org.ccm.maven -DartifactId=firstwebapp -DinteractiveMode=false


It is initially composed of these files :

firstwebapp/
| pom.xml
| src/
| | main/
| | | resources/
| | | webapp/
| | | | index.jsp
| | | | WEB-INF/
| | | | | web.xml


We shall focus first on the index.jsp page, whose content is as such:

<html>
<body>
<h2>Hello World!</h2>
</body>
</html>
For now, it is a simple HTML page, we will see later how to add Java instructions in order to boost the content .

Now focus on the pom.xml file, the intial content is:

<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>org.ccm.maven</groupId>
<artifactId>firstwebapp</artifactId>
<packaging>war</packaging>
<version>1.0-SNAPSHOT</version>
<name>firstwebapp 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>
</dependencies>
<build>
<finalName>firstwebapp</finalName>
</build>
</project>

The Jetty server


To put our web application online, we need a Java application server. For this purpose we shall add a Jetty plugin that will boot the server from a Maven command.
We must change the pom.xml file:

<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>org.ccm.maven</groupId>
<artifactId>firstwebapp</artifactId>
<packaging>war</packaging>
<version>1.0-SNAPSHOT</version>
<name>firstwebapp 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>
</dependencies>
<build>
<finalName>firstwebapp</finalName>
<plugins>
<plugin>
<groupId>org.mortbay.jetty</groupId>
<artifactId>jetty-maven-plugin</artifactId>
<version>8.1.13.v20130916</version>
<configuration>
<webAppConfig>
<contextPath>/firstwebapp</contextPath>
</webAppConfig>
</configuration>
</plugin>
</plugins>
</build>
</project>

Note: versions will evolve over time , you can check the Maven central repository for information on the latest versions of dependencies and plugins.

Installation and first launch


First you will need to install the application, take all sources projects and assemble them into a WAR archive. The command is:
mvn clean install
You now have a target folder in your project folder, it contains the WAR file to deploy the application on any Java application server. To install the application under Jetty, start the server by running the following command:
mvn jetty:run
You can now access the web application at the following address: http://localhost:8080/firstwebapp/
By default, you are immediately directed to the index.jsp page that displays "Hello World!"

Boosting the JSP with Java


Your web project is now running properly and you can now improve it.
For example, if you want that the following page is displayed "Hello Toto", when the http://localhost:8080/firstwebapp/?name=Toto address is used (the name is used as argument).
In addition, we show the number of visitors to the page (as it is being accessed):

<%-- Gestion des accents (entre autre) --%>
<%@ page contentType="text/html; charset=UTF-8" %>
<%@ page pageEncoding="UTF-8" %>

<%-- Date de démarrage de la JSP --%>
<%@ page import="java.util.Date"%>
<%! Date startDate = new Date(); %>

<%-- Nombre de visiteurs sur la page. --%>
<%! int nbVisitors = 0; %>

<html>
<head>
<title>Hello</title>
</head>
<body>
<%-- On affiche le nom s'il est passé en paramètre --%>
<h2>Bonjour <%= request.getParameter("name")!=null ? request.getParameter("name") : "" %> !</h2>

<%-- On incrémente le nombre de visiteurs et on l'affiche. --%>
<p>Vous êtes le <%= ++nbVisitors %>è visiteur de cette page depuis son démarrage le <%= String.format("%1$te %1$tB à %1$tkh%1$tM",startDate) %>.</p>
</body>
</html>

It is not necessary to restart the server after modifying the JSP, these changes are done automatically. So just go to the site and test the URL with or without the "name" argument :
http://localhost:8080/firstwebapp/
http://localhost:8080/firstwebapp/?name=Toto
http://localhost:8080/firstwebapp/?name=Bob

Related :

This document entitled « JSP/Jetty web applicaton with Maven » from CCM (ccm.net) is made available under the Creative Commons license. You can copy, modify copies of this page, under the conditions stipulated by the license, as this note appears clearly.