Last time we learned about how to install & integrate Tomcat with Apache. This time we will focus our full attention on the Tomcat and try to learn how to configure custom web application in it.
Why?
Why do we need to configure the custom web application? When, Tomcat comes along with preconfigured web application. Well here are the some of the answers for why
• We must not have web application inside Tomcat installation. This makes software upgrade easy
• We don’t accidently delete the application along with Tomcat
• We can set appropriate permissions for application without messing with Tomcat
• Most importantly, we make application maintainable
HOW?
Following are generic steps that one needs to follow to configure Tomcat Web Application.
I. Create the Web Application Directory Structure
II. Set appropriate permission on the Web Application Directory Structure
III. Modifying the configuration files
IV. Installing Javax Servlet Classes
V. Preparing Test Environment
VI. Preparing Test files
VII. Testing the Web Application
VIII. Accessing Application via Apache
I. Creating Web Application Directory Structure
I’m going to put my web application at $tchome which refers to the location /var/www/tctest throughout this document. We need following structure in order to deploy our web application
1. $tchome/webapp This is works as the root for all the Tomcat web application. May be we can call it container
2. $tchome/webapp/$domain Replace $domain with the name of your domain for e.g. tctest.com
3. $tchome/webapp/$domain/logs This is where we can keep the logs. Note that this is not mandatory directory but logs are always
good to have especially in rainy day.
4. $tchome/webapp/$domain/ROOT ROOT is the root of your application for $domain
5. $tchome/webapp/$domain/ROOT/WEB-INF WEB-INF is the directory where we put the application specific configuration files
6. $tchome/webapp/$domain/ROOT/WEB-INF/classes This is where you can put the application related java classes
I’m quite sure that you can create the directory structure on your own, just to make it quick I have give you one command which build the structure in single stroke.
[root@linbox~]#mkdir –pv $tchome/webapp/$domain/{logs,ROOT/WEB-INF/classes}
II. Set appropriate permission on the Web Application Directory Structure
We have to make sure that web application is accessible
[root@linbox~]#chmod 775 $tchome/webapp/{$domain,$domain/ROOT}
III. Modifing the configuration files
1. Take back up of original server.xml in your tomcat installation directory (i.e. /usr/local/tomcat/conf in our case) and, create new server.xml with following contents
<Server port=”8005″ shutdown=”SHUTDOWN”>
<Service name=”Catalina”>
<Connector port=”8080″ protocol=”HTTP/1.1″ connectionTimeout=”20000″ redirectPort=”8443″ />
<Connector port=”8009″ protocol=”AJP/1.3″ redirectPort=”8443″ />
<Engine name=”Catalina” defaultHost=”localhost”>
<!– Define the default virtual hos –>
<Host name=”tctest.com”
appBase=”/var/www/tctest /webapps/tctest.com”
unpackWARs=”true” autoDeploy=”true”
xmlValidation=”false”
xmlNamespaceAware=”false”>
</Host>
</Engine>
</Service>
</Server>
2. Take backup of original context.xml in your tomcat installation directory and, create new context.xml with following contents
<Context reloadable=”true” privileged=”true”/>
3. Now, final configuration file web.xml, there will be one default web.xml file inside your tomcat installation directory which you can modify for the global effect. However at this stage better not to play with default file. We will simply create another web.xml inside WEB-INF of our application (i.e. /var/www/tctest/webapps/tctest.com/ROOT/WEB-INF ) with following contents.
<?xml version=”1.0″ encoding=”ISO-8859-1″?>
<web-app xmlns=”http://java.sun.com/xml/ns/javaee”
xmlns:xsi=”http://www.w3.org/2001/XMLSchema-instance”
xsi:schemaLocation=”http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd” version=”2.5″>
<servlet>
<servlet-name>invoker</servlet-name>
<servlet-class>
org.apache.catalina.servlets.InvokerServlet
</servlet-class>
<init-param>
<param-name>debug</param-name>
<param-value>0</param-value>
</init-param>
<load-on-startup>2</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>invoker</servlet-name>
<url-pattern>/servlet/*</url-pattern>
</servlet-mapping>
</web-app>
I have not explained any DTDs & TAGS used in these xml files. If you are curious,you can check out Tomcat documentation at http://tomcat.apache.org/tomcat-6.0-doc/index.html
IV. Installing Javax Servlet Classes
Javax Servelt Classes are used very often in any web application. It contains several built in classes which can be used by servlet developers for building web application. Correct me if am wrong, as in my view, one can think of Javax Servlet classes as API or RAD tool
You can download javax servelt classes from here
Following are the steps to install these javax servlet classes
1. Unzip the classfile
[root@linbox~]#unzip servlet-2_3-fcs-classfiles.zip
2. Copy/Move it inside Tomcat library
[root@linbox~]cp -rv javax/ /usr/local/tomcat/lib
V. Preparing Test Environment
1. First thing we need to do is create one development directory where we will keep the source files for JSP and servlets. It could be anywhere but, outside tomcat installation directory.
For e.g. I have created directory src, at location /var/www/tctest/webapps/tctest.com/
although, practically one should have absolutely separate development directory
2. As, second step we have to set the environment variable CLASSPATH, since servlets and JSP are not part of the J2SE. Here Tomcat server already knows about the servlets but, compiler (i.e. javac ) have no idea about those servlet classes hence, it will throw an compilation error when we make use of those servlet classes.
Use these commands to set your CLASSPATH
[root@linbox~]#CLASSPATH=”.:/var/www/tctest/webapps/tctest.com/src:/usr/local/tomcat/lib”
[root@linbox~]#export CLASSPATH
As usual remember to put these command in /etc/profile to make your life easy
That’s it! We are done with configuration and everything is set to test out application only missing part is test files
VI. Preparing Test Files
Let’s create some HTML, JSP & Servlets, test files inside our development directory we created in previous step (i.e. src ) to verify our configuration Below are the some example codes that you can use for testing.
HTML Page <hello.html>
<html>
<title>Hello! Html from Tomcat</title>
<body>
Hello! Html From TomCat
</body>
</html>
JSP Page <hello.jsp>
<html>
<title>Hello! JSP from Tomcat</title>
<body>
<% out.println( “Hello! JSP From Tomcat” ); %>
</body>
</html>
Servlet <helloservlet.java>
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
/** Simple servlet used to test server.
* <P>
* Taken from Core Servlets and JavaServer Pages 2nd Edition
* from Prentice Hall and Sun Microsystems Press,
* http://www.coreservlets.com/.
* © 2003 Marty Hall; may be freely used or adapted.
*/
public class helloservlet extends HttpServlet {
public void doGet(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException {
response.setContentType(“text/html”);
PrintWriter out = response.getWriter();
String docType =
“<!DOCTYPE HTML PUBLIC \”-//W3C//DTD HTML 4.0 ” +
“Transitional//EN\”>\n”;
out.println(docType +
“<HTML>\n” +
“<HEAD><TITLE>Hello Servelts</TITLE></HEAD>\n” +
“<BODY BGCOLOR=\”#FDF5E6\”>\n” +
“<H1>Hello Servlets! </H1>\n” +
“</BODY></HTML>”);
}
}
Before we can make use of this servlet we need to compile it and get the class file. Use the following command to do generate class file for our first servlet
[root@src]#javac helloservlet.java
To use this command your J2SE installations bin directory must be in your path. If you have followed the installation steps as per first tutorial then , javac will can be found in /usr/local/java/bin directory
Please don’t ask me to trouble shoot the compilation errors as am not servlet developer myself, if you can see, I have taken this example from http://www.coreservlets.com/ You can either visit that site or Google for some other example
We will be ready to see the result, once we put/copy/move these test files in appropriate directories as shown
move hello.html to /var/www/tctest/webapps/tctest.com/ROOT/
move hello.jsp to /var/www/tctest/webapps/tctest.com/ROOT/
move helloservlet.class to /var/www/tctest/webapps/tctest.com/ROOT/WEB-INF/classes
VII. Testing Web Application
To test it, we will be accessing application directly with Tomcat port 8080 as follows
http://tctest.com:8080/hello.html This should display Hello HTML Page
http://tctest.com:8080/hello.jsp This should display Hello JSP Page
http://tctest.com:8080/servlet/helloservlet This should display Hello Servlet Page
You may need to make entry of tctest.com in your /etc/hosts file to make it work
VIII. Accessing Application via Apache
Finally, let’s add the following lines in apache configuration file to access our tctest.com via apache.
<VirtualHost *:80>
ServerAdmin admin@tctest.com
DocumentRoot “/$tchome /webapps/tctest.com/ROOT”
<Directory “/$tchome /webapps/tctest.com/ROOT/”>
Options Indexes
AllowOverride None
Order allow,deny
Allow from all
</Directory>
#which pages to seek for by default in website
DirectoryIndex index.jsp hello.jsp index.html hello.html
JkMount / worker1
JkMount /* worker1
# Where to put jk logs
JkLogFile /$tchome /webapps/tctest.com/logs/tctest.com-mod_jk_log
# Set the jk log level [debug/error/info]
JkLogLevel debug
# Select the log format
JkLogStampFormat “[%a %b %d %H:%M:%S %Y] “
# JkRequestLogFormat set the request format
JkRequestLogFormat “%w %V %T”
ServerName www.tctest.com
ServerAlias tctest.com
ErrorLog /$tchome /webapps/tctest.com/logs/tctest.com-error_log
CustomLog /$tchome/webapps/tctest.com/logs/tctest.com-access_log common
</VirtualHost>
To use the above configuration make sure you have uncommented NameVirtualHost *:80 directive in httpd.conf
Stop/start both Apache & Tomcat, and then try to access
http://tctest.com/hello.html to test HTML file
http://tctest.com/hello.jsp to test JSP file
http://tctest.com/servlet/helloservlet to test Servelt
So, this time we coved how to configure & test custom web application. We have also covered Tomcat virtual hosting (Surprised?). Remember yet, security area is untouched hence, it’s pretty much unsafe way to deploy web application. For those, who are wondering where Tomcat Virtual hosting came in this tutorial. I will cover clear example with two custom web applications in next tutorial. There should be no problem, if you follow through these steps properly. However feel free t o contact me with your suggestions, doubts & difficulties