Download from
http://www.nyangau.org/et/et.zip
.
Often people develop web applications and deploy the resulting
.war
file to Tomcat, or other servlet engine, or application
server.
These servlet engines can come with their own baggage, which includes complex installation, configuration and administration. It also can include larger deployment footprints and runtime requirements. As servlet engines can sometimes be provided by other groups or departments, operational/procedural dependencies can be created between AD groups and web teams or system administrators.
Often what you want is that some application you have written should also listen on a port and serve web content too. Or perhaps the packaging and deployment framework within which you work doesn't play nicely with the operational constraints described above.
So for this, we can use Embedded Tomcat.
This makes Tomcat a part of your application.
Your application includes a few Tomcat related .jar
files.
Upon startup, the application creates a new Tomcat()
,
customises it a little and starts it listening for web requests.
This package is really a demonstration of how this can work.
This package delivers three Maven projects.
The mywar
project is a simple web application.
It includes some static content and a servlet.
The servlet appears in three places (url-patterns), two requiring authenticated access, one also requiring confidential access (ie: SSL).
The pom.xml
produces a .war
as its result.
In principle, this .war
could be deployed to real Tomcat,
another servlet engine or application server, or even to the cloud.
The wcet
project depends on the appropriate Tomcat
.jar
s.
This includes a WCET
class, which is what instantiates
Embedded Tomcat and customises it :-
static
method (call this first)
to stop logging going to the console,
and instead log it to N files of M MB each,
and it also reformats the logging to something nicer than the
java.util.logging.*
default
.war
file
at a certain context location
The code explicitly avoids log rotation based on date, as runaway logging can fill a disk and take a system down before rotation kicks in. Instead it promises to keep within a disk size limit, and if runaway logging does occur, the worst that happens is that the logs don't go back as far in time as they would normally. Log numbers and sizes have defaults which can be overridden using properties.
The mywebproc
project instantiates a
WCET
, registers My WAR into it, and starts listening.
The main .java
file is therefore very short and unexciting.
It is likely that in real use, userids and passwords might be obtained from
a credential store and configured into the WCET
.
Its pom.xml
depends on the mywar
project and
the wcet
project.
Perhaps the most interesting thing is the use of an
assembly.xml
to construct the file deployable asset.
It creates a .zip
file containing something like this :-
$ unzip -v mywebproc-0.1-SNAPSHOT-bin.zip Archive: mywebproc-0.1-SNAPSHOT-bin.zip Length Method Size Cmpr Date Time CRC-32 Name -------- ------ ------- ---- ---------- ----- -------- ---- 0 Stored 0 0% 02-10-2022 20:28 00000000 base/ 0 Stored 0 0% 02-10-2022 20:28 00000000 base/webapps/ 4148 Defl:N 2845 31% 02-10-2022 19:51 9376bd62 base/webapps/mywar.war 0 Stored 0 0% 12-29-2015 16:03 00000000 keystores/ 3169 Defl:N 1763 44% 12-29-2015 16:03 b6bb4483 keystores/webserver.jks 0 Stored 0 0% 02-10-2022 20:28 00000000 lib/ 2870966 Defl:N 2733644 5% 02-10-2022 19:50 6cda2b41 lib/ecj-3.18.0.jar 2642 Defl:N 1736 34% 02-10-2022 20:16 bf6f50e3 lib/mywebproc-0.1-SNAPSHOT.jar 13197 Defl:N 10519 20% 02-10-2022 19:50 87585847 lib/tomcat-annotations-api-9.0.58.jar 11515 Defl:N 9524 17% 02-10-2022 19:50 f91a4cd2 lib/tomcat-api-9.0.58.jar 89206 Defl:N 82613 7% 02-10-2022 19:50 9ff1b8aa lib/tomcat-el-api-9.0.58.jar 3430643 Defl:N 3200352 7% 02-10-2022 19:50 3682af03 lib/tomcat-embed-core-9.0.58.jar 257028 Defl:N 237730 8% 02-10-2022 19:50 c3e6af37 lib/tomcat-embed-el-9.0.58.jar 651819 Defl:N 607585 7% 02-10-2022 19:50 549b4319 lib/tomcat-embed-jasper-9.0.58.jar 570508 Defl:N 534843 6% 02-10-2022 19:50 fb791dcf lib/tomcat-jasper-9.0.58.jar 172138 Defl:N 157961 8% 02-10-2022 19:50 08f708bc lib/tomcat-jasper-el-9.0.58.jar 150070 Defl:N 140821 6% 02-10-2022 19:50 469669fa lib/tomcat-jdbc-9.0.58.jar 63640 Defl:N 54090 15% 02-10-2022 19:50 53c27ba5 lib/tomcat-jsp-api-9.0.58.jar 47110 Defl:N 43328 8% 02-10-2022 19:50 b2d3ce6e lib/tomcat-juli-9.0.58.jar 284411 Defl:N 266700 6% 02-10-2022 19:50 bfac375b lib/tomcat-servlet-api-9.0.58.jar 213406 Defl:N 194836 9% 02-10-2022 19:50 a930db61 lib/tomcat-util-9.0.58.jar 224107 Defl:N 203994 9% 02-10-2022 19:50 e0fc920d lib/tomcat-util-scan-9.0.58.jar 10930 Defl:N 9812 10% 02-10-2022 20:28 580e9ea9 lib/wcet-0.1-SNAPSHOT.jar 84 Defl:N 84 0% 12-30-2015 12:26 1a2d7e3f mywebproc 76 Defl:N 76 0% 12-30-2015 12:26 5b612a39 mywebproc.bat -------- ------- --- ------- 9070813 8494856 6% 25 files
Its important to spot that although
mywebproc-0.1-SNAPSHOT.jar
was put in the lib
subdirectory, the assembly process has carefully arranged to put
mywar.war
(named without a version suffix) in the
base/webapps/
subdirectory.
This is so the Java code that registers a .war
into
Tomcat has a fixed filename to refer to it by.
Note the inclusion of a couple of scripts
(mywebproc
and mywebproc.bat
) to run the program.
You can imagine more sophisticated versions that could be used as
init-scripts.
The intent of this structure is that you simply unzip
the file, and run the script.
In reality, the installation process or the start script is likely
to need to add a context.xml
into the META-INF/
subdirectory of the .war
file.
This file contains things such as database connection definitions,
and is therefore likely to vary depending on the environment into
which the application is deployed.
Now uses Java 11 (or later).
Usage :-
$ unzip mywebproc-0.1-SNAPSHOT-bin.zip $ ./mywebproc
Point your browser at http://localhost:8080/mywar/
or
https://localhost:8443/mywar/
.
Feel free to copy, its public domain. Caveat Emptor.