JSP/Jetty web applicaton with Maven

by Robert Mullins

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

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

mvn archetype:generate -DarchetypeArtifactId=maven-archetype-webapp -DgroupId=orgcmaven -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:

Hello World!

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:

xsi:schemaLocation="http://maven.apacherg/POM/4.0.0 http://maven.apacherg/xsd/maven-4.0.0.xsd">

4.0.0

orgcmaven

firstwebapp

war

1.0-SNAPSHOT

firstwebapp Maven Webapp

http://maven.apacherg

junit

junit

3.8.1

test

firstwebapp

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:

xsi:schemaLocation="http://maven.apacherg/POM/4.0.0 http://maven.apacherg/xsd/maven-4.0.0.xsd">

4.0.0

orgcmaven

firstwebapp

war

1.0-SNAPSHOT

firstwebapp Maven Webapp

http://maven.apacherg

junit

junit

3.8.1

test

firstwebapp

orgortbay.jetty

jetty-maven-plugin

8.1.13.v20130916

/firstwebapp

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

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!"

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

Hello

Bonjour !

Vous êtes le è visiteur de cette page depuis son démarrage le .

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 :

Leave a Comment