In: Computer Science
Using Maven and Web Programming Basics
1. Build a program using maven 75 pts
A. Create file structure
Create a file structure for your program like the following:
web_app
→ src
→ main
→ java
→ resources
→ webapp
→ target
make sure you have in src/main/ the directories java, resources, and webapp.
In most Java IDEs (Eclipse, IntelliJ, etc.) you can make a maven project, which will have this structure by default (without the webapp directory), you can use that IDE tools then add the webapp directory.
B. Make pom.xml
Apache maven is a build automation tool that can compile and build your program as well as download any .jar dependencies you need. I’ll provide you with a pom.xml file with the parameters set to download the .jar files you need to make a web server in java. Put this file at the top level of your program file structure (same level as the the src and target directories). To verify you examined the file, change the value in the <name> tag from YOUR_WEB_APP to the name of your program. You can see all the jar files that are needed in <dependencies> and where the program is named in
<build>
<plugin>
<programs>
<program>
the file that sets up the tomcat program is in webDriver.Main (which I will describe later) and the program name is web (if you are doing the extra credit don’t change this)
C. Add tomcat driver
For this program we’ll be using the Apache Tomcat web framework (another popular one for java is Glassfish). In IDE you can use the Web Application projects to form your web program, but for this class I want you to build the web app driver so you can more easily publish your software and so you can see what is need to make a java web server.
To start make a package in src/main/java called webDriver (same as the name above), then add the provided Main.java file into that package (web_app/src/main/java/webDriver). If you see errors in your IDE your package names do not match (package webDriver) or you set up the pom.xml file incorrectly. Otherwise it should not have any errors. This class when run talks to the operating system to make your computer into a web server.
D. Create web.xml file
To help out your web server with some piping of traffic you need to setup a web.xml file. In your src/main/webapp directory create a folder called WEB-INF. In there put the provided web.xml file. This file helps direct traffic for the server, but it’s really simple for this homework, but we’ll build on it, in later assignments. To verify you have looked at the file, change the <display-name> value in the file, to your program name. The <welcome-file-list> gives the search order for the server’s default landing page.
E. Generate index.html
Finally you should have a working server to check if it works put an index.html page in src/main/webapp. I provided one, but you can put your own one in there.
To test if everything work, you’ll need to build the maven
package. For windows users, open powershell or cmd, osX users open
console, and terminal for Linux users, navigate to your project and
type
mvn package
you should get a bunch of messages where the last five lines are similar to:
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 1.862 s
[INFO] Finished at: 2020-01-12T13:01:55-07:00
[INFO] ------------------------------------------------------------------------
Here is my solution:
I have build it in eclipse with maven plugin installed.
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>com.proj</groupId>
<artifactId>gontu</artifactId>
<packaging>war</packaging>
<version>0.0.1-SNAPSHOT</version>
<name>gontu 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>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-core -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>5.2.9.RELEASE</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-webmvc -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.2.9.RELEASE</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-webmvc -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.2.9.RELEASE</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-context -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.2.9.RELEASE</version>
</dependency>
<!-- https://mvnrepository.com/artifact/commons-logging/commons-logging -->
<dependency>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
<version>1.2</version>
</dependency>
</dependencies>
<build>
<finalName>gontu</finalName>
</build>
</project>
index.jsp
<html>
<body>
<h2>Hello World!</h2>
</body>
</html>
build success screenshot:
You can build your project on this setup.