Maven project structure schematics
Creating maven project structure for tomcat war creation
Create a directory structure for the the application,the name will be secondapp.
mkdir secondapp
mkdir -p src/main/webapp/WEB-INF
mkdir -p src/main/java/com/cba/web
vi 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/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.cba.web</groupId>
<artifactId>secondapp</artifactId>
<version>1</version>
<packaging>war</packaging>
<name>secondapp</name>
<url>https://www.custom-built-apps.com</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>4.0.0</version>
<scope>provided</scope>
</dependency>
</dependencies>
<build>
<sourceDirectory>src/main/java/com/cba/web/</sourceDirectory>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>3.2.3</version>
<configuration>
<warSourceDirectory>src/main/webapp</warSourceDirectory>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>
</project>
vi src/main/webapp/WEB-INF/web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
version="3.1">
<display-name>connection to PostgreSQL</display-name>
<servlet>
<servlet-name>secondapp</servlet-name>
<servlet-class>com.cba.web.secondapp</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>secondapp</servlet-name>
<url-pattern>/secondapp</url-pattern>
</servlet-mapping>
</web-app>
vi src/main/java/com/cba/web/secondapp.java
// File : secondapp.java
// Author : Boris Alexandrov
// Date : Sun May 3 22:26:44 EDT 2020
// Purpose : Servlet for Tomcat
///////////////////////////////////////
package com.cba.web;
import java.sql.*;
import java.util.*;
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.lang.*;
public class secondapp extends HttpServlet
{
public void init(ServletConfig config) throws ServletException
{
super.init(config);
}
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
{
String url = "jdbc:postgresql://192.168.2.23:5432/dataexplorerdb2";
Properties props = new Properties();
props.setProperty("user","dataexplorer2");
props.setProperty("password","SomePassWord");
PrintWriter out = response.getWriter();
out.println("<HTML>");
out.println("<HEAD>");
out.println("<TITLE>PostgresSQL testing</TITLE>");
out.println("<link rel=stylesheet type=text/css href=css/cba.css>");
out.println("</HEAD>");
out.println("<BODY>");
out.println("<H1>Product sales by the month</H1>");
// out.println("<H1>Establishing connection ...</H1>");
try
{
Connection conn = DriverManager.getConnection(url, props);
// out.println("<H1>Connected ..</H1>");
String strSQL="select * from product_sales_monthly";
Statement stmt=conn.createStatement();
ResultSet rs=stmt.executeQuery(strSQL);
out.println("<TABLE>");
out.println("<TR><TH>Product</TH><TH>Month</TH><TH>Year</TH><TH>Amount,CAD</TH></TR>");
while(rs.next())
{
out.println("<TR>");
out.println("<TD>"+rs.getString("product")+"</TD>");
out.println("<TD>"+rs.getString("month")+"</TD>");
out.println("<TD>"+rs.getString(3)+"</TD>");
out.println("<TD>"+rs.getDouble(4)+"</TD>");
out.println("</TR>");
}
out.println("</TABLE>");
conn.close();
out.println("</BODY>");
out.println("</HTML>");
}
catch(SQLException e)
{
out.println(e.getMessage());
}
}
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
{
doGet(request,response);
}
};
Build the application:
mvn package
......
[INFO] Building war: /home/dataexplorer1/projects/java/secondapp/target/secondapp-1.war
Deploying the war into tomcat
Copy the war file to /tmp and remove the version number
cp /home/dataexplorer1/projects/java/secondapp/target/secondapp-1.war /tmp/secondapp.war
In the browser open the Tomcat manager application at
Choose Deploy war file located on the server, enter the the location of the file and click Deploy
Check the Deploy message:
Testing the application run
in the browser go to application path :
http://r01edge.custom-built-apps.com:1962/secondapp/secondapp
Bitbucket
http://r01edge.custom-built-apps.com:7990/projects/HTODP/repos/java/browse/secondapp
Attachments:
image2020-5-3_22-52-58.png (image/png)
image2020-5-3_22-55-24.png (image/png)
image2020-5-3_22-56-44.png (image/png)
servletDirectoryStructure.png (image/png)
servletDirectoryStructure.vsdx (application/octet-stream)
servletDirectoryStructure.png (image/png)
servletDirectoryStructure.png (image/png)