The Apache web server – Part 3
Tomcat
Tomcat is needed if we want to execute Java Servlets and Java Server Pages on our web server. It adds this feature and also has some usable management tools.
Installation
First we need to install the following packages:
tomcat7
tomcat7-admin
tomcat7-docs
After a successful installation Tomcat can be started with the following command: ‚/etc/init.d/tomcat7 start‘, stopped with ‚/etc/init.d/tomcat7 stop‘ and restarted with ‚/etc/init.d/tomcat7 restart‘. For this you need to be root.
You can test if the installation was successful if you type the following into your browser:
http://localhost:8080
If everything is right, this message should be displayed: „It works!“.
Configuration
You can configure Apache Tomcat with xml files in the ‚conf‘ directory of the Tomcat directory (normally ‚/var/lib/tomcat7/conf‘). The ‚tomcat-users.xml‘ file is interesting. You can define user and user roles for the Tomcat manager tool. To use this tool, we define a new user and a new role:
<role rolename="manager-gui" /> <user username="manager" password="meinpasswort" roles="manager-gui"/>
When the file is saved, we restart Tomcat. Now we can log in with our newly created user to the management gui of Tomcat:
http://localhost:8080/manager
This tool displays all running applications. You can stop, restart or delete them. At the end of the site there are input fields to deplay new applications to this Tomcat web server.
You can test this with an example Java web application. You can find it here: Link auf JavaTest.war. Such an application can be implemented with IDEs like Netbeans or Eclipse. *.war files are compiled projects. When you deploy this project it is located at the following URL:
http://localhost/JavaTest
You should see a success message. The site displays informations about your browser and you operating system. It also shows your IP address. If you are interested into the code, here is the whole source of this application. You can also find it in the JSP file in the war archiv.
<%@page contentType="text/html" pageEncoding="UTF-8"%> <!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>JSP Testseite</title> </head> <body> <h1>JSP Testseite!</h1> Your browser is: <%= request.getHeader("User-Agent") %><br> Your IP address is: <%= request.getRemoteAddr() %><br> </body> </html>
If you are now interested into web development you will easily find more on this topic on this blog or on the net.