Tomcat is a web container. It supports APIs in the presentation layer described earlier. In addition, it supports JDBC and JPA. It is easy to configure and could be a good option if you do not want to use EJBs.
Download the latest version of Tomcat from http://tomcat.apache.org/. Unzip the downloaded file in a folder. Set the JAVA_HOME environment variable to point to the folder where JDK is installed (the folder path should be the JDK folder, which has bin as one of the subfolders). To start the server, run startup.bat in Command Prompt on Windows and startup.sh in a Terminal window on Mac and Linux. If there are no errors, then you should see the message Server startup in --ms or Tomcat started.
The default Tomcat installation is configured to use port 8080. If you want to change the port, open server.xml under the conf folder and look for a connector declaration such as the following:
<Connector port="8080" protocol="HTTP/1.1"
connectionTimeout="20000"
redirectPort="8443" />
Change the port value to any port number you want, though in this book we will be using the default port 8080. Before we open the default page of Tomcat, we will add a user for administration of the Tomcat server. Open tomcat-users.xml under the conf folder using any text editor. At the end of the file, you will see commented example of how to add users. Add the following configuration before the closure of </tomcat-users> tag:
<role rolename="manager-gui"/>
<user username="admin" password="admin" roles="manager-gui"/>
Here, we are adding a user admin, with password also as admin, to a role called manager-gui. This role has access to web pages for managing an application in Tomcat. This and other security roles are defined in web.xml of the manager application. You can find it at webapps/manager/WEB-INF/web.xml.
After making the preceding changes, open a web browser and browse to http://localhost:8080 (modify the port number if you have changed the default port). You will see the following default Tomcat page:
Figure 1.6: The default Tomcat web application
Click on the Manager App button on the right. You will be asked for the username and password. Enter the username and password you configured in tomcat-users.xml for manager-gui, as described earlier. After you are successfully logged in, you will see the Tomcat Web Application Manager page, as shown in Figure 1.7. You can see all the applications deployed in Tomcat in this page. You can also deploy your applications from this page:
Figure 1.7: Tomcat Web Application Manager
To stop the Tomcat server, press Ctrl/cmd + C or run the shutdown script in the bin folder.