{"id":36,"date":"2017-02-09T22:37:00","date_gmt":"2017-02-09T22:37:00","guid":{"rendered":""},"modified":"2019-03-12T20:10:16","modified_gmt":"2019-03-12T20:10:16","slug":"putting-tomcat-into-a-container","status":"publish","type":"post","link":"https:\/\/ntlx.org\/de\/2017\/02\/putting-tomcat-into-a-container.html","title":{"rendered":"Putting tomcat into a container"},"content":{"rendered":"<p>As soon as you get in touch with software there is no chance to get around one topic these days: Microservices. And wherever you hear this term there is always a second one around the corner: Docker. I myself have only little experience in productively using docker, and this is mostly restricted to the convenience of packaging stuff and installing it somewhere else via a central repository. But already this is quite cool, so why not try to go further and create a small Java service in a docker container if setting up things is so easy with docker. The goal of this post is to document how I achieved a first response of the service &#8211; setting up a Java microservice with docker should be almost no work, right? As I haven&#8217;t had much contact with tomcat and maven, I learned all this (not much in the end but it took some time) the hard way &#8211; fighting all the way through old forum posts, maven repositories, eclipse UIs, tomcat books and documentations. Please let me know if you see any potential improvements.<\/p>\n<h2>Setting things up<\/h2>\n<p>Here we go. First of all I created a docker-compose file that shows how my microservice-powered project will look like:<\/p>\n<p><!-- HTML generated using hilite.me --><\/p>\n<div style=\"background: #ffffff; border-width: 0.1em 0.1em 0.1em 0.8em; border: solid gray; overflow: auto; padding: 0.2em 0.6em; width: auto;\">\n<table>\n<tbody>\n<tr>\n<td>\n<pre style=\"line-height: 125%; margin: 0;\"> 1<br \/> 2<br \/> 3<br \/> 4<br \/> 5<br \/> 6<br \/> 7<br \/> 8<br \/> 9<br \/>10<br \/>11<br \/>12<br \/>13<br \/>14<br \/>15<br \/>16<br \/>17<br \/>18<br \/>19<br \/>20<br \/>21<br \/>22<br \/>23<br \/>24<br \/>25<br \/>26<\/pre>\n<\/td>\n<td>\n<pre style=\"line-height: 125%; margin: 0;\">proxy:<br \/>    image: nginx<br \/>    links:<br \/>        - frontend<br \/>        - backend<br \/>    volumes:<br \/>        - .\/proxy\/nginx.conf:\/etc\/nginx\/conf.d\/default.conf<br \/>    ports:<br \/>        - <span style=\"background-color: #fff0f0;\">\"80:80\"<\/span><br \/>frontend:<br \/>    image: nginx<br \/>    volumes:<br \/>        - .\/frontend:\/usr\/share\/nginx\/html<br \/>backend:<br \/>    image: tomcat<br \/>    links: <br \/>        - database<br \/>    volumes:<br \/>        - .\/backend\/tomcat-users.xml:\/usr\/local\/tomcat\/conf\/tomcat-users.xml<br \/>database:<br \/>    image: mysql<br \/>    environment:<br \/>        - MYSQL_ROOT_PASSWORD=topsecret<br \/>        - MYSQL_DATABASE=mydb<br \/>        - MYSQL_USER=mydbuser<br \/>        - MYSQL_PASSWORD=<br \/><\/pre>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/div>\n<p>I know container-links are not the most modern way to go in the docker universe, but for the ease of use and to show how things are tangled together, I still use them for this small project. As you can see, we have four different containers. One for the frontend, containing the great UI of our application. A second one containing the database with all the data the application will need to run. Don&#8217;t worry, I won&#8217;t bother you with boring database setup in this blog post. It&#8217;s only about the third thing: the tomcat container. For this container we add a link to the database, of course, and we mount a local file to override the user configuration of the tomcat.<br \/>This is needed to be able to show the manager application and to deploy remotely to the tomcat server. Here is the config file as I created it. (I copied it out of the container and adjusted it, maybe you should do the same to make sure it&#8217;s compatible with your version of tomcat. And, for productive usage, of course you should use a password for the admin user. But configuring maven is more complicated then, so for now it&#8217;s ok without for me.)<\/p>\n<p><!-- HTML generated using hilite.me --><\/p>\n<div style=\"background: #ffffff; border-width: 0.1em 0.1em 0.1em 0.8em; border: solid gray; overflow: auto; padding: 0.2em 0.6em; width: auto;\">\n<table>\n<tbody>\n<tr>\n<td>\n<pre style=\"line-height: 125%; margin: 0;\"> 1<br \/> 2<br \/> 3<br \/> 4<br \/> 5<br \/> 6<br \/> 7<br \/> 8<br \/> 9<br \/>10<br \/>11<br \/>12<br \/>13<br \/>14<br \/>15<br \/>16<br \/>17<br \/>18<br \/>19<br \/>20<br \/>21<br \/>22<br \/>23<br \/>24<br \/>25<br \/>26<br \/>27<br \/>28<br \/>29<br \/>30<br \/>31<br \/>32<br \/>33<br \/>34<br \/>35<br \/>36<br \/>37<br \/>38<br \/>39<br \/>40<br \/>41<br \/>42<br \/>43<br \/>44<br \/>45<br \/>46<br \/>47<br \/>48<\/pre>\n<\/td>\n<td>\n<pre style=\"line-height: 125%; margin: 0;\"><span style=\"color: #557799;\">&lt;?xml version='1.0' encoding='utf-8'?&gt;<\/span><br \/><span style=\"color: #888888;\">&lt;!--<\/span><br \/><span style=\"color: #888888;\">  Licensed to the Apache Software Foundation (ASF) under one or more<\/span><br \/><span style=\"color: #888888;\">  contributor license agreements.  See the NOTICE file distributed with<\/span><br \/><span style=\"color: #888888;\">  this work for additional information regarding copyright ownership.<\/span><br \/><span style=\"color: #888888;\">  The ASF licenses this file to You under the Apache License, Version 2.0<\/span><br \/><span style=\"color: #888888;\">  (the \"License\"); you may not use this file except in compliance with<\/span><br \/><span style=\"color: #888888;\">  the License.  You may obtain a copy of the License at<\/span><br \/><br \/><span style=\"color: #888888;\">      http:\/\/www.apache.org\/licenses\/LICENSE-2.0<\/span><br \/><br \/><span style=\"color: #888888;\">  Unless required by applicable law or agreed to in writing, software<\/span><br \/><span style=\"color: #888888;\">  distributed under the License is distributed on an \"AS IS\" BASIS,<\/span><br \/><span style=\"color: #888888;\">  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.<\/span><br \/><span style=\"color: #888888;\">  See the License for the specific language governing permissions and<\/span><br \/><span style=\"color: #888888;\">  limitations under the License.<\/span><br \/><span style=\"color: #888888;\">--&gt;<\/span><br \/><span style=\"color: #007700;\">&lt;tomcat-users<\/span> <span style=\"color: #0000cc;\">xmlns=<\/span><span style=\"background-color: #fff0f0;\">\"http:\/\/tomcat.apache.org\/xml\"<\/span><br \/>              <span style=\"color: #0000cc;\">xmlns:xsi=<\/span><span style=\"background-color: #fff0f0;\">\"http:\/\/www.w3.org\/2001\/XMLSchema-instance\"<\/span><br \/>              <span style=\"color: #0000cc;\">xsi:schemaLocation=<\/span><span style=\"background-color: #fff0f0;\">\"http:\/\/tomcat.apache.org\/xml tomcat-users.xsd\"<\/span><br \/>              <span style=\"color: #0000cc;\">version=<\/span><span style=\"background-color: #fff0f0;\">\"1.0\"<\/span><span style=\"color: #007700;\">&gt;<\/span><br \/><span style=\"color: #888888;\">&lt;!--<\/span><br \/><span style=\"color: #888888;\">  NOTE:  By default, no user is included in the \"manager-gui\" role required<\/span><br \/><span style=\"color: #888888;\">  to operate the \"\/manager\/html\" web application.  If you wish to use this app,<\/span><br \/><span style=\"color: #888888;\">  you must define such a user - the username and password are arbitrary. It is<\/span><br \/><span style=\"color: #888888;\">  strongly recommended that you do NOT use one of the users in the commented out<\/span><br \/><span style=\"color: #888888;\">  section below since they are intended for use with the examples web<\/span><br \/><span style=\"color: #888888;\">  application.<\/span><br \/><span style=\"color: #888888;\">--&gt;<\/span><br \/><span style=\"color: #888888;\">&lt;!--<\/span><br \/><span style=\"color: #888888;\">  NOTE:  The sample user and role entries below are intended for use with the<\/span><br \/><span style=\"color: #888888;\">  examples web application. They are wrapped in a comment and thus are ignored<\/span><br \/><span style=\"color: #888888;\">  when reading this file. If you wish to configure these users for use with the<\/span><br \/><span style=\"color: #888888;\">  examples web application, do not forget to remove the &lt;!.. ..&gt; that surrounds<\/span><br \/><span style=\"color: #888888;\">  them. You will also need to set the passwords to something appropriate.<\/span><br \/><span style=\"color: #888888;\">--&gt;<\/span><br \/><span style=\"color: #888888;\">&lt;!--<\/span><br \/><span style=\"color: #888888;\">  &lt;role rolename=\"tomcat\"\/&gt;<\/span><br \/><span style=\"color: #888888;\">  &lt;role rolename=\"role1\"\/&gt;<\/span><br \/><span style=\"color: #888888;\">  &lt;user username=\"tomcat\" password=\"&lt;must-be-changed&gt;\" roles=\"tomcat\"\/&gt;<\/span><br \/><span style=\"color: #888888;\">  &lt;user username=\"both\" password=\"&lt;must-be-changed&gt;\" roles=\"tomcat,role1\"\/&gt;<\/span><br \/><span style=\"color: #888888;\">  &lt;user username=\"role1\" password=\"&lt;must-be-changed&gt;\" roles=\"role1\"\/&gt;<\/span><br \/><span style=\"color: #888888;\">--&gt;<\/span><br \/><span style=\"color: #007700;\">&lt;role<\/span> <span style=\"color: #0000cc;\">rolename=<\/span><span style=\"background-color: #fff0f0;\">\"manager-script\"<\/span><span style=\"color: #007700;\">\/&gt;<\/span><br \/><span style=\"color: #007700;\">&lt;user<\/span> <span style=\"color: #0000cc;\">username=<\/span><span style=\"background-color: #fff0f0;\">\"admin\"<\/span> <span style=\"color: #0000cc;\">password=<\/span><span style=\"background-color: #fff0f0;\">\"\"<\/span> <span style=\"color: #0000cc;\">roles=<\/span><span style=\"background-color: #fff0f0;\">\"manager-script\"<\/span><span style=\"color: #007700;\">\/&gt;<\/span><br \/><span style=\"color: #007700;\">&lt;role<\/span> <span style=\"color: #0000cc;\">rolename=<\/span><span style=\"background-color: #fff0f0;\">\"manager-gui\"<\/span><span style=\"color: #007700;\">\/&gt;<\/span><br \/><span style=\"color: #007700;\">&lt;user<\/span> <span style=\"color: #0000cc;\">username=<\/span><span style=\"background-color: #fff0f0;\">\"tomcat\"<\/span> <span style=\"color: #0000cc;\">password=<\/span><span style=\"background-color: #fff0f0;\">\"topsecret\"<\/span> <span style=\"color: #0000cc;\">roles=<\/span><span style=\"background-color: #fff0f0;\">\"manager-gui\"<\/span><span style=\"color: #007700;\">\/&gt;<\/span><br \/><span style=\"color: #007700;\">&lt;\/tomcat-users&gt;<\/span><br \/><\/pre>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/div>\n<p>The last container is used to reverse-proxy the requests of the application either to the frontend or to the backend, depending on the URL. In this example, I configured nginx to lead all the requests starting with \/backend (http:\/\/localhost\/backend) or \/manager to the backend container (make sure to set it to the correct port of tomcat, 8080) and all others to the frontend container. So we need to make sure the servlet we are going to create is available under \/backend, so they can be routed through the reverse proxy correctly.<br \/>We run docker-compose up -d as usual and some seconds later the containers are up and running. If you now go to http:\/\/localhost\/backend you should already see tomcat greeting you with a 404 error page. Installing tomcat has never been so easy.<\/p>\n<h2>Deploying to tomcat<\/h2>\n<div>Next, we want to be able to deploy a servlet to the tomcat running in the docker container. To do this, I decided to use maven to build and deploy the project. To get the initial setup, I ran maven with the following command line:<\/div>\n<div><span style=\"font-family: &quot;courier new&quot; , &quot;courier&quot; , monospace;\">mvn archetype:generate -DgroupId=com.ntlx -DartifactId=backend -DarchetypeArtifactId=maven-archetype-webapp <\/span><br \/>After the execution of this command you will find your self in a folder structure, containing all the files and folders you always wondered where they come from, and who knows where to put them:<\/p>\n<div><span>.\/pom.xml<\/span><\/div>\n<div><span>.\/src<\/span><\/div>\n<div><span>.\/src\/main<\/span><\/div>\n<div><span>.\/src\/main\/resources<\/span><\/div>\n<div><span>.\/src\/main\/webapp<\/span><\/div>\n<div><span>.\/src\/main\/webapp\/index.jsp<\/span><\/div>\n<div><span>.\/src\/main\/webapp\/WEB-INF<\/span><\/div>\n<div><span>.\/src\/main\/webapp\/WEB-INF\/web.xml<\/span><br \/><span><br \/><\/span><span>The pom.xml now needs some further adjustments because we want to deploy to a remote tomcat server. Here is the complete file, as it looks for me:<\/span><br \/><span><br \/><\/span><!-- HTML generated using hilite.me --><\/p>\n<div style=\"background: #ffffff; border-width: 0.1em 0.1em 0.1em 0.8em; border: solid gray; overflow: auto; padding: 0.2em 0.6em; width: auto;\">\n<table>\n<tbody>\n<tr>\n<td>\n<pre style=\"line-height: 125%; margin: 0;\"> 1<br \/> 2<br \/> 3<br \/> 4<br \/> 5<br \/> 6<br \/> 7<br \/> 8<br \/> 9<br \/>10<br \/>11<br \/>12<br \/>13<br \/>14<br \/>15<br \/>16<br \/>17<br \/>18<br \/>19<br \/>20<br \/>21<br \/>22<br \/>23<br \/>24<br \/>25<br \/>26<br \/>27<br \/>28<br \/>29<br \/>30<br \/>31<br \/>32<br \/>33<br \/>34<br \/>35<br \/>36<br \/>37<br \/>38<br \/>39<br \/>40<br \/>41<br \/>42<br \/>43<br \/>44<br \/>45<br \/>46<br \/>47<br \/>48<br \/>49<br \/>50<br \/>51<br \/>52<br \/>53<br \/>54<br \/>55<br \/>56<br \/>57<br \/>58<br \/>59<br \/>60<br \/>61<br \/>62<br \/>63<br \/>64<br \/>65<br \/>66<br \/>67<br \/>68<br \/>69<br \/>70<br \/>71<br \/>72<br \/>73<br \/>74<br \/>75<br \/>76<br \/>77<br \/>78<br \/>79<br \/>80<br \/>81<br \/>82<\/pre>\n<\/td>\n<td>\n<pre style=\"line-height: 125%; margin: 0;\"><span style=\"color: #007700;\">&lt;project<\/span> <span style=\"color: #0000cc;\">xmlns=<\/span><span style=\"background-color: #fff0f0;\">\"http:\/\/maven.apache.org\/POM\/4.0.0\"<\/span> <span style=\"color: #0000cc;\">xmlns:xsi=<\/span><span style=\"background-color: #fff0f0;\">\"http:\/\/www.w3.org\/2001\/XMLSchema-instance\"<\/span><br \/>    <span style=\"color: #0000cc;\">xsi:schemaLocation=<\/span><span style=\"background-color: #fff0f0;\">\"http:\/\/maven.apache.org\/POM\/4.0.0 http:\/\/maven.apache.org\/maven-v4_0_0.xsd\"<\/span><span style=\"color: #007700;\">&gt;<\/span><br \/>    <span style=\"color: #007700;\">&lt;modelVersion&gt;<\/span>4.0.0<span style=\"color: #007700;\">&lt;\/modelVersion&gt;<\/span><br \/>    <span style=\"color: #007700;\">&lt;groupId&gt;<\/span>com.ntlx<span style=\"color: #007700;\">&lt;\/groupId&gt;<\/span><br \/>    <span style=\"color: #007700;\">&lt;artifactId&gt;<\/span>backend<span style=\"color: #007700;\">&lt;\/artifactId&gt;<\/span><br \/>    <span style=\"color: #007700;\">&lt;packaging&gt;<\/span>war<span style=\"color: #007700;\">&lt;\/packaging&gt;<\/span><br \/>    <span style=\"color: #007700;\">&lt;version&gt;<\/span>1.0-SNAPSHOT<span style=\"color: #007700;\">&lt;\/version&gt;<\/span><br \/>    <span style=\"color: #007700;\">&lt;name&gt;<\/span>backend Maven Webapp<span style=\"color: #007700;\">&lt;\/name&gt;<\/span><br \/>    <span style=\"color: #007700;\">&lt;url&gt;<\/span>http:\/\/maven.apache.org<span style=\"color: #007700;\">&lt;\/url&gt;<\/span><br \/>    <span style=\"color: #007700;\">&lt;dependencies&gt;<\/span><br \/>        <span style=\"color: #007700;\">&lt;dependency&gt;<\/span><br \/>            <span style=\"color: #007700;\">&lt;groupId&gt;<\/span>junit<span style=\"color: #007700;\">&lt;\/groupId&gt;<\/span><br \/>            <span style=\"color: #007700;\">&lt;artifactId&gt;<\/span>junit<span style=\"color: #007700;\">&lt;\/artifactId&gt;<\/span><br \/>            <span style=\"color: #007700;\">&lt;version&gt;<\/span>3.8.1<span style=\"color: #007700;\">&lt;\/version&gt;<\/span><br \/>            <span style=\"color: #007700;\">&lt;scope&gt;<\/span>test<span style=\"color: #007700;\">&lt;\/scope&gt;<\/span><br \/>        <span style=\"color: #007700;\">&lt;\/dependency&gt;<\/span><br \/>        <span style=\"color: #007700;\">&lt;dependency&gt;<\/span><br \/>            <span style=\"color: #007700;\">&lt;groupId&gt;<\/span>javax.servlet<span style=\"color: #007700;\">&lt;\/groupId&gt;<\/span><br \/>            <span style=\"color: #007700;\">&lt;artifactId&gt;<\/span>javax.servlet-api<span style=\"color: #007700;\">&lt;\/artifactId&gt;<\/span><br \/>            <span style=\"color: #007700;\">&lt;version&gt;<\/span>3.1.0<span style=\"color: #007700;\">&lt;\/version&gt;<\/span><br \/>   <span style=\"color: #007700;\">&lt;scope&gt;<\/span>provided<span style=\"color: #007700;\">&lt;\/scope&gt;<\/span><br \/>        <span style=\"color: #007700;\">&lt;\/dependency&gt;<\/span><br \/><br \/><br \/>    <span style=\"color: #007700;\">&lt;\/dependencies&gt;<\/span><br \/>    <span style=\"color: #007700;\">&lt;build&gt;<\/span><br \/>        <span style=\"color: #007700;\">&lt;finalName&gt;<\/span>backend<span style=\"color: #007700;\">&lt;\/finalName&gt;<\/span><br \/>        <span style=\"color: #007700;\">&lt;plugins&gt;<\/span><br \/>            <span style=\"color: #007700;\">&lt;plugin&gt;<\/span><br \/>                <span style=\"color: #007700;\">&lt;groupId&gt;<\/span>org.apache.maven.plugins<span style=\"color: #007700;\">&lt;\/groupId&gt;<\/span><br \/>                <span style=\"color: #007700;\">&lt;artifactId&gt;<\/span>maven-compiler-plugin<span style=\"color: #007700;\">&lt;\/artifactId&gt;<\/span><br \/>                <span style=\"color: #007700;\">&lt;version&gt;<\/span>2.3.2<span style=\"color: #007700;\">&lt;\/version&gt;<\/span><br \/>                <span style=\"color: #007700;\">&lt;configuration&gt;<\/span><br \/>                    <span style=\"color: #007700;\">&lt;source&gt;<\/span>1.6<span style=\"color: #007700;\">&lt;\/source&gt;<\/span><br \/>                    <span style=\"color: #007700;\">&lt;target&gt;<\/span>1.6<span style=\"color: #007700;\">&lt;\/target&gt;<\/span><br \/>                <span style=\"color: #007700;\">&lt;\/configuration&gt;<\/span><br \/>            <span style=\"color: #007700;\">&lt;\/plugin&gt;<\/span><br \/>            <span style=\"color: #007700;\">&lt;plugin&gt;<\/span><br \/>                <span style=\"color: #007700;\">&lt;groupId&gt;<\/span>org.apache.maven.plugins<span style=\"color: #007700;\">&lt;\/groupId&gt;<\/span><br \/>                <span style=\"color: #007700;\">&lt;artifactId&gt;<\/span>maven-war-plugin<span style=\"color: #007700;\">&lt;\/artifactId&gt;<\/span><br \/>                <span style=\"color: #007700;\">&lt;version&gt;<\/span>2.3<span style=\"color: #007700;\">&lt;\/version&gt;<\/span><br \/>                <span style=\"color: #007700;\">&lt;configuration&gt;<\/span><br \/>                    <span style=\"color: #007700;\">&lt;failOnMissingWebXml&gt;<\/span>true<span style=\"color: #007700;\">&lt;\/failOnMissingWebXml&gt;<\/span><br \/>                <span style=\"color: #007700;\">&lt;\/configuration&gt;<\/span><br \/>            <span style=\"color: #007700;\">&lt;\/plugin&gt;<\/span><br \/>            <span style=\"color: #007700;\">&lt;plugin&gt;<\/span><br \/>                <span style=\"color: #007700;\">&lt;groupId&gt;<\/span>org.apache.tomcat.maven<span style=\"color: #007700;\">&lt;\/groupId&gt;<\/span><br \/>                <span style=\"color: #007700;\">&lt;artifactId&gt;<\/span>tomcat7-maven-plugin<span style=\"color: #007700;\">&lt;\/artifactId&gt;<\/span><br \/>                <span style=\"color: #007700;\">&lt;version&gt;<\/span>2.2<span style=\"color: #007700;\">&lt;\/version&gt;<\/span><br \/>                <span style=\"color: #007700;\">&lt;configuration&gt;<\/span><br \/>                    <span style=\"color: #007700;\">&lt;url&gt;<\/span>http:\/\/localhost\/manager\/text<span style=\"color: #007700;\">&lt;\/url&gt;<\/span><br \/>                    <span style=\"color: #007700;\">&lt;server&gt;<\/span>TomcatServer<span style=\"color: #007700;\">&lt;\/server&gt;<\/span><br \/>                    <span style=\"color: #007700;\">&lt;path&gt;<\/span>\/backend<span style=\"color: #007700;\">&lt;\/path&gt;<\/span><br \/>                <span style=\"color: #007700;\">&lt;\/configuration&gt;<\/span><br \/>            <span style=\"color: #007700;\">&lt;\/plugin&gt;<\/span><br \/>            <span style=\"color: #007700;\">&lt;plugin&gt;<\/span><br \/>                <span style=\"color: #007700;\">&lt;groupId&gt;<\/span>org.apache.maven.plugins<span style=\"color: #007700;\">&lt;\/groupId&gt;<\/span><br \/>                <span style=\"color: #007700;\">&lt;artifactId&gt;<\/span>maven-dependency-plugin<span style=\"color: #007700;\">&lt;\/artifactId&gt;<\/span><br \/>                <span style=\"color: #007700;\">&lt;version&gt;<\/span>2.1<span style=\"color: #007700;\">&lt;\/version&gt;<\/span><br \/>                <span style=\"color: #007700;\">&lt;executions&gt;<\/span><br \/>                    <span style=\"color: #007700;\">&lt;execution&gt;<\/span><br \/>                        <span style=\"color: #007700;\">&lt;phase&gt;<\/span>validate<span style=\"color: #007700;\">&lt;\/phase&gt;<\/span><br \/>                        <span style=\"color: #007700;\">&lt;goals&gt;<\/span><br \/>                            <span style=\"color: #007700;\">&lt;goal&gt;<\/span>copy<span style=\"color: #007700;\">&lt;\/goal&gt;<\/span><br \/>                        <span style=\"color: #007700;\">&lt;\/goals&gt;<\/span><br \/>                        <span style=\"color: #007700;\">&lt;configuration&gt;<\/span><br \/>                            <span style=\"color: #007700;\">&lt;silent&gt;<\/span>true<span style=\"color: #007700;\">&lt;\/silent&gt;<\/span><br \/>                            <span style=\"color: #007700;\">&lt;artifactItems&gt;<\/span><br \/>                                <span style=\"color: #007700;\">&lt;artifactItem&gt;<\/span><br \/>                                    <span style=\"color: #007700;\">&lt;groupId&gt;<\/span>javax<span style=\"color: #007700;\">&lt;\/groupId&gt;<\/span><br \/>                                    <span style=\"color: #007700;\">&lt;artifactId&gt;<\/span>javaee-endorsed-api<span style=\"color: #007700;\">&lt;\/artifactId&gt;<\/span><br \/>                                    <span style=\"color: #007700;\">&lt;version&gt;<\/span>6.0<span style=\"color: #007700;\">&lt;\/version&gt;<\/span><br \/>                                    <span style=\"color: #007700;\">&lt;type&gt;<\/span>jar<span style=\"color: #007700;\">&lt;\/type&gt;<\/span><br \/>                                <span style=\"color: #007700;\">&lt;\/artifactItem&gt;<\/span><br \/>                            <span style=\"color: #007700;\">&lt;\/artifactItems&gt;<\/span><br \/>                        <span style=\"color: #007700;\">&lt;\/configuration&gt;<\/span><br \/>                    <span style=\"color: #007700;\">&lt;\/execution&gt;<\/span><br \/>                <span style=\"color: #007700;\">&lt;\/executions&gt;<\/span><br \/>            <span style=\"color: #007700;\">&lt;\/plugin&gt;<\/span><br \/>        <span style=\"color: #007700;\">&lt;\/plugins&gt;<\/span><br \/>    <span style=\"color: #007700;\">&lt;\/build&gt;<\/span><br \/><span style=\"color: #007700;\">&lt;\/project&gt;<\/span><br \/><\/pre>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/div>\n<p>It already contains the plugins needed to deploy a servlet and it contains the information to deploy onto a remote tomcat. In our case the remote tomcat is localhost, port 80 (remember: reverse-proxied to port 8080 in the tomcat container). \/manager\/text is needed to deploy our service. That&#8217;s why we also created a reverse-proxy for this URL.<\/div>\n<p>That&#8217;s nearly it. If we now run<br \/><span style=\"font-family: &quot;courier new&quot; , &quot;courier&quot; , monospace;\">mvn tomcat7:deploy<\/span><br \/><span style=\"font-family: inherit;\">it should be able to deploy our web application to the tomcat server. Go to http:\/\/localhost\/backend again to see the hello world page, maven created earlier.<\/span><\/p>\n<h2><span style=\"font-family: inherit;\">Adding a servlet<\/span><\/h2>\n<\/div>\n<div><span style=\"font-family: inherit;\">To provide our RESTful service in the end, we want to use a servlet. Therefore, we need to create a .java file for our servlet class at the right place: src\/main\/java. Create this folder, add some subfolders for your package like com\/ntlx\/ and add a Java Servlet file there. I myself copied the example from the eclipse wizard for servlet creation:<\/span><\/p>\n<div><\/div>\n<\/div>\n<p><!-- HTML generated using hilite.me --><\/p>\n<div style=\"background: #ffffff; border-width: 0.1em 0.1em 0.1em 0.8em; border: solid gray; overflow: auto; padding: 0.2em 0.6em; width: auto;\">\n<table>\n<tbody>\n<tr>\n<td>\n<pre style=\"line-height: 125%; margin: 0;\"> 1<br \/> 2<br \/> 3<br \/> 4<br \/> 5<br \/> 6<br \/> 7<br \/> 8<br \/> 9<br \/>10<br \/>11<br \/>12<br \/>13<br \/>14<br \/>15<br \/>16<br \/>17<br \/>18<br \/>19<br \/>20<br \/>21<br \/>22<br \/>23<br \/>24<br \/>25<br \/>26<br \/>27<br \/>28<br \/>29<br \/>30<br \/>31<br \/>32<br \/>33<br \/>34<br \/>35<br \/>36<br \/>37<br \/>38<br \/>39<br \/>40<\/pre>\n<\/td>\n<td>\n<pre style=\"line-height: 125%; margin: 0;\"><span style=\"color: #008800; font-weight: bold;\">import<\/span> <span style=\"color: #0e84b5; font-weight: bold;\">java.io.IOException<\/span><span style=\"color: #333333;\">;<\/span><br \/><br \/><span style=\"color: #008800; font-weight: bold;\">import<\/span> <span style=\"color: #0e84b5; font-weight: bold;\">javax.servlet.ServletException<\/span><span style=\"color: #333333;\">;<\/span><br \/><span style=\"color: #008800; font-weight: bold;\">import<\/span> <span style=\"color: #0e84b5; font-weight: bold;\">javax.servlet.annotation.WebServlet<\/span><span style=\"color: #333333;\">;<\/span><br \/><span style=\"color: #008800; font-weight: bold;\">import<\/span> <span style=\"color: #0e84b5; font-weight: bold;\">javax.servlet.http.HttpServlet<\/span><span style=\"color: #333333;\">;<\/span><br \/><span style=\"color: #008800; font-weight: bold;\">import<\/span> <span style=\"color: #0e84b5; font-weight: bold;\">javax.servlet.http.HttpServletRequest<\/span><span style=\"color: #333333;\">;<\/span><br \/><span style=\"color: #008800; font-weight: bold;\">import<\/span> <span style=\"color: #0e84b5; font-weight: bold;\">javax.servlet.http.HttpServletResponse<\/span><span style=\"color: #333333;\">;<\/span><br \/><span style=\"color: #008800; font-weight: bold;\">package<\/span> com<span style=\"color: #333333;\">.<\/span><span style=\"color: #0000cc;\">ntlx<\/span><span style=\"color: #333333;\">;<\/span><br \/><br \/><span style=\"color: #888888;\">\/**<\/span><br \/><span style=\"color: #888888;\"> * Servlet implementation class FirstTest<\/span><br \/><span style=\"color: #888888;\"> *\/<\/span><br \/><span style=\"color: #555555; font-weight: bold;\">@WebServlet<\/span><span style=\"color: #333333;\">(<\/span><span style=\"background-color: #fff0f0;\">\"\/FirstTest\"<\/span><span style=\"color: #333333;\">)<\/span><br \/><span style=\"color: #008800; font-weight: bold;\">public<\/span> <span style=\"color: #008800; font-weight: bold;\">class<\/span> <span style=\"color: #bb0066; font-weight: bold;\">FirstTest<\/span> <span style=\"color: #008800; font-weight: bold;\">extends<\/span> HttpServlet <span style=\"color: #333333;\">{<\/span><br \/> <span style=\"color: #008800; font-weight: bold;\">private<\/span> <span style=\"color: #008800; font-weight: bold;\">static<\/span> <span style=\"color: #008800; font-weight: bold;\">final<\/span> <span style=\"color: #333399; font-weight: bold;\">long<\/span> serialVersionUID <span style=\"color: #333333;\">=<\/span> <span style=\"color: #0000dd; font-weight: bold;\">1L<\/span><span style=\"color: #333333;\">;<\/span><br \/><br \/>    <span style=\"color: #888888;\">\/**<\/span><br \/><span style=\"color: #888888;\">     * Default constructor. <\/span><br \/><span style=\"color: #888888;\">     *\/<\/span><br \/>    <span style=\"color: #008800; font-weight: bold;\">public<\/span> <span style=\"color: #0066bb; font-weight: bold;\">FirstTest<\/span><span style=\"color: #333333;\">()<\/span> <span style=\"color: #333333;\">{<\/span><br \/>        <span style=\"color: #888888;\">\/\/ TODO Auto-generated constructor stub<\/span><br \/>    <span style=\"color: #333333;\">}<\/span><br \/><br \/> <span style=\"color: #888888;\">\/**<\/span><br \/><span style=\"color: #888888;\">  * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)<\/span><br \/><span style=\"color: #888888;\">  *\/<\/span><br \/> <span style=\"color: #008800; font-weight: bold;\">protected<\/span> <span style=\"color: #333399; font-weight: bold;\">void<\/span> <span style=\"color: #0066bb; font-weight: bold;\">doGet<\/span><span style=\"color: #333333;\">(<\/span>HttpServletRequest request<span style=\"color: #333333;\">,<\/span> HttpServletResponse response<span style=\"color: #333333;\">)<\/span> <span style=\"color: #008800; font-weight: bold;\">throws<\/span> ServletException<span style=\"color: #333333;\">,<\/span> IOException <span style=\"color: #333333;\">{<\/span><br \/>  <span style=\"color: #888888;\">\/\/ TODO Auto-generated method stub<\/span><br \/>  response<span style=\"color: #333333;\">.<\/span><span style=\"color: #0000cc;\">getWriter<\/span><span style=\"color: #333333;\">().<\/span><span style=\"color: #0000cc;\">append<\/span><span style=\"color: #333333;\">(<\/span><span style=\"background-color: #fff0f0;\">\"Served at: \"<\/span><span style=\"color: #333333;\">).<\/span><span style=\"color: #0000cc;\">append<\/span><span style=\"color: #333333;\">(<\/span>request<span style=\"color: #333333;\">.<\/span><span style=\"color: #0000cc;\">getContextPath<\/span><span style=\"color: #333333;\">());<\/span><br \/> <span style=\"color: #333333;\">}<\/span><br \/><br \/> <span style=\"color: #888888;\">\/**<\/span><br \/><span style=\"color: #888888;\">  * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)<\/span><br \/><span style=\"color: #888888;\">  *\/<\/span><br \/> <span style=\"color: #008800; font-weight: bold;\">protected<\/span> <span style=\"color: #333399; font-weight: bold;\">void<\/span> <span style=\"color: #0066bb; font-weight: bold;\">doPost<\/span><span style=\"color: #333333;\">(<\/span>HttpServletRequest request<span style=\"color: #333333;\">,<\/span> HttpServletResponse response<span style=\"color: #333333;\">)<\/span> <span style=\"color: #008800; font-weight: bold;\">throws<\/span> ServletException<span style=\"color: #333333;\">,<\/span> IOException <span style=\"color: #333333;\">{<\/span><br \/>  <span style=\"color: #888888;\">\/\/ TODO Auto-generated method stub<\/span><br \/>  doGet<span style=\"color: #333333;\">(<\/span>request<span style=\"color: #333333;\">,<\/span> response<span style=\"color: #333333;\">);<\/span><br \/> <span style=\"color: #333333;\">}<\/span><br \/><br \/><span style=\"color: #333333;\">}<\/span><br \/><\/pre>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/div>\n<p><span style=\"font-family: inherit;\">As you can see, we set the URL to \/FirstTest. This is the relative address where the servlet will be found. So, after a run of&nbsp;<\/span><br \/><span style=\"font-family: &quot;courier new&quot; , &quot;courier&quot; , monospace;\">mvn tomcat7:redeploy<\/span><\/p>\n<div><span style=\"font-family: inherit;\">you should see the response of the servlet at http:\/\/localhost\/backend\/FirstTest<\/span><\/div>\n<div><span style=\"font-family: inherit;\"><br \/><\/span><\/div>\n<div><span style=\"font-family: inherit;\">That&#8217;s it, now we can start developing the service! I think this is quite handy as you can deploy the same tomcat on a different machine, like the productive one instead of your developer notebook, and it will work as before. Of course we have to think of a process to package the servlet with the docker container, but this shouldn&#8217;t be too much effort.<\/span><\/div>","protected":false},"excerpt":{"rendered":"As soon as you get in touch with software there is no chance to get around one topic these days: Microservices. And wherever you hear this term there is always a second one around the corner: Docker. I myself have only little experience in productively using docker, and this is mostly restricted to the convenience&#8230; <a class=\"view-article\" href=\"https:\/\/ntlx.org\/de\/2017\/02\/putting-tomcat-into-a-container.html\">Artikel ansehen<\/a>","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[],"tags":[27,24,5],"_links":{"self":[{"href":"https:\/\/ntlx.org\/de\/wp-json\/wp\/v2\/posts\/36"}],"collection":[{"href":"https:\/\/ntlx.org\/de\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/ntlx.org\/de\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/ntlx.org\/de\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/ntlx.org\/de\/wp-json\/wp\/v2\/comments?post=36"}],"version-history":[{"count":1,"href":"https:\/\/ntlx.org\/de\/wp-json\/wp\/v2\/posts\/36\/revisions"}],"predecessor-version":[{"id":72,"href":"https:\/\/ntlx.org\/de\/wp-json\/wp\/v2\/posts\/36\/revisions\/72"}],"wp:attachment":[{"href":"https:\/\/ntlx.org\/de\/wp-json\/wp\/v2\/media?parent=36"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ntlx.org\/de\/wp-json\/wp\/v2\/categories?post=36"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ntlx.org\/de\/wp-json\/wp\/v2\/tags?post=36"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}