First off, lets be clear on the version for installation.
| | VERSION
|
Tomcat
| 5.0.28
|
MySQL
| 5.0.15
|
Tomcat
The most confusing issue for any of these installations is configuration. At first glance the server.xml is painful to consume all at once. So, lets NUKE it!! This server.xml has been stripped and we'll explain each section below:
Remember, this server.xml is the MINIMAL REQUIRED infromation the Tomcat Server requires for execution.
<Server port="8005" shutdown="SHUTDOWN" debug="0">
The above tag defines the port for Tomcat to use when a shut down request such as
Unix /base/tc/bin/catalina stop
Windows c:\base\tc\bin\catalina stop
<Listener className="org.apache.catalina.mbeans.ServerLifecycleListener" debug="0"/>
<Listener className="org.apache.catalina.mbeans.GlobalResourcesLifecycleListener" debug="0"/>
The above tags are beyond the scope of this document but are here for completeness. If there is a desire to understand these please comment and I will seperately address them.
<GlobalNamingResources>
<Resource name="UserDatabase" auth="Container"
type="org.apache.catalina.UserDatabase"
description="User database that can be updated and saved">
</Resource>
<ResourceParams name="UserDatabase">
<parameter>
<name>factory</name>
<value>org.apache.catalina.users.MemoryUserDatabaseFactory</value>
</parameter>
<parameter>
<name>pathname</name>
<value>conf/tomcat-users.xml</value>
</parameter>
</ResourceParams>
</GlobalNamingResources>
This is the user database configurartion and is here for completeness.
<Service name="Catalina">
The service tag starts the Catalina server container definition.
<Connector port="8080"
maxThreads="150" minSpareThreads="25" maxSpareThreads="75"
enableLookups="false" redirectPort="8443" acceptCount="100"
debug="0" connectionTimeout="20000"
disableUploadTimeout="true" />
The server listens on this por for requests. The URL, in our case is localhost so our URL for the Tomcat server is localhost:8080//. The other parameters are self explanatory and are set to the Tomcat defaults.
<Connector port="8009"
enableLookups="false" redirectPort="8443" debug="0"
protocol="AJP/1.3" />
The port 8009 is how the Apache Webserver communicate to Tomcat. Apache can deliver jps to this port or servlets for the servlet container to act upon. This wil be discussed in another blog.
<Engine name="Catalina" defaultHost="localhost" debug="0">
The engine name has to be given a deault host in this case its localhost; this to is the standard default.
<Logger className="org.apache.catalina.logger.FileLogger"
prefix="catalina_new_log." suffix=".txt"
timestamp="true"/>
The logger file is created here the oroginal is catalina_log however, adding _new in the middle allows an easy search to access the file. This file will be located in the respective directories below:
Unix /base/tc/logs
Windows c:\base\tc\logs
<Realm className="org.apache.catalina.realm.UserDatabaseRealm"
debug="0" resourceName="UserDatabase"/>
The realm tag is used for an authentication mechanism in Tomcat. There are three realms, the first is a simple file, next a user database table and one that uses LDAP. The realm above is the user databse realm and for initial installation, our purposes here, this will be ignored. It is important, however, to study the realms to increase the compentance of one's Tomcat skills.
<Host name="localhost" debug="0" appBase="/base/deploy/apps"
unpackWARs="true" autoDeploy="true">
The applications have to be based somewhere, in the above appBase param the choice of base/deploy/apss was selected. This can be any directory based on your standards and configuration you see fit.. The above path is only a suggestion for demonstration purposes.
<Logger className="org.apache.catalina.logger.FileLogger
directory="logs" prefix="localhost_new_log." suffix=".txt"
timestamp="true"/>
The log file defaults ./logs in the $CATALINA home dir.
</Host>
</Engine>
</Service>
Closing tags follow
Now that we have the server configured its time to run Tomcat.
In UNIX : type /base/Tomcat/bin/catalina.sh start
In Windows : type /base/Tomcat/bin/catalina start
Deploying our first JSP!Deploy the following index.jsp in to the tcapp directory
Unix /base/deploy/apps/tcapp
Windows c:\base\deploy\apps\tcapp
<%@ page contentType="text/html; charset=iso-8859-1" language="java" import="java.sql.*" errorPage="" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<style title="text/css">
<!-- a { text-decoratopm: none }
body { font-family: vernada, helvetica; sans serif; font-size: 10pt; } -->
</style>
<% String os = System.getProperty("os.name");%>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<title>TCAPP Check Tomcat's Installation - Show OS</title>
</head>
<body>
<center>Welcome to the <U>Tomcat Server</U> </center>
<P>My Operating System is <%=os%></P>
</body>
</html>
Now, in the /base/deply/apps/tcapps directory create a WEB-INF directory and save the following web.xml file:
<?xml version="1.0" encoding="iso-8859-1"?><!DOCTYPE web-app PUBLIC "- //SUN Microsystems, Inc. //DTD Web Application 2.3//EN" "http://java.sun.com/dtd/web-app_2_3.dtd"> <web-app> <display-name>Tomcat Dev</display-name> <description> Servlet Behavior </description> </web-app>
The next file needed to test you web application is the
tcapp.xml. Place this file in the $CATALINA_HOME/conf/Catalina/localhost directory. This file simply identifies to Tomcat a file for application logging seperate from all other Tomcat logs and establishes a jdbc connection resource as seen below:
<Context path="/tcapp" docBase="tcapp" debug="0">
<Logger className="org.apache.catalina.logger.FileLogger"
prefix="localhost_tcapp_" suffix=".log"
timestamp="TRUE"/>
<Resource name="jdbc/tcapp" auth="Container"
type="javax.sql.DataSource"/>
<ResourceParams name="jdbc/tcapp">
<parameter><name>username</name>
<value>root</value></parameter>
<parameter><name>password</name><value>1234</value></parameter>
<parameter><name>driverClassName</name>
<value>org.gjt.mm.mysql.Driver</value></parameter>
<parameter><name>url</name>
<value>jdbc:mysql://localhost/Lessons</value></parameter>
</ResourceParams>
</Context>
Call our JSP File
In the broswer type http://localhost:8080/tcapp
The index.jsp is pulled up as it is identified as a deafult home page. The result of the index.jsp is shown below:
Welcome to the
Tomcat Server My Operating System is Mac OS X
Note: This setup is a little premature and requires MySQL to contiune. However, index.jsp allows us a very quick test to see if tomcat is installed and configured correctly. Simply placing the tcapp.xml into $CATALINA_HOME/conf/Catalina/localhost directory will allow Tomcat to red this new entry and deploy the web application. In order to see this happen in real-time you can use tail on the catalina.out log as follows:
In Mac OS X
Orion:/base/tc/logs sparcs$ tail -f catalina.out
In Windows
c:\base\tc\logs sparcs$ tail -f catalina.out
The output will look something like this;
INFO: Starting Coyote HTTP/1.1 on http-8080
Nov 21, 2005 8:41:08 AM org.apache.jk.common.ChannelSocket init
INFO: JK2: ajp13 listening on /0.0.0.0:8009
Nov 21, 2005 8:41:08 AM org.apache.jk.server.JkMain start
INFO: Jk running ID=0 time=20/136 config=/base/tc/conf/jk2.properties
Nov 21, 2005 8:41:08 AM org.apache.catalina.startup.Catalina start
INFO: Server startup in 49546 ms
Windows does not have tail.exe natively! However, there is a open Source solution which can be found on SourceForge. The link to Windows Tail is tail.
The next step is to get MySQL a table we can work with and updating the index.jsp to have a link to access the lessons database.
MySQL
The installation to MySQL can be located at this blog entry.
The installation directories used through this tutorial are listed below:
Unix /base/mysql/
Windows c:\base\mysql
I have been using MySQL Admin to create database and tables, and well, general administartion. I believe this to be a nice little package and would suggest using it... Its an easy install and provides a GUI for MySQL Administration.
(NOTE: My experience with MySQL Admin is Windows and Mac OS X only! However, I am sure the Linux performs well!)
Now lets create a database called "lessons". We will use the mysql.exe cmd line interface, its simple, works and demonstrates whats really going on!!
Execute /base/mysql/bin/mysql in Unix or c:\base\mysql\bin\msqyl.exe
mysql> create database lessons;
mysql> use lessons;
Database changed
mysql> CREATE TABLE `lessons`.`faqs` (
-> `qid` INT NOT NULL,
-> `question` varchar(255),
-> `answer` text
-> )
-> ENGINE = MYISAM
-> CHARACTER SET utf8;
Query OK, 0 rows affected (0.00 sec)
mysql> insert into faqs values (0, "Where can I download tomcat?", "http://tomcat.apache.org/");
Query OK, 1 row affected (0.00 sec)
mysql> insert into faqs values (1, "Where can I download MySQL?", "http://dev.mysql.com/downloads/" );
Query OK, 1 row affected (0.00 sec)
mysql> select * from faqs;
+-----+------------------------------+---------------------------------+
| qid | question | answer |
+-----+------------------------------+---------------------------------+
| 1 | Where can I download MySQL? | http://dev.mysql.com/downloads/ |
| 0 | Where can I download tomcat? | http://tomcat.apache.org/ |
+-----+------------------------------+---------------------------------+
2 rows in set (0.00 sec)
The actual SQL script is below:
create database lessons;use lessons;CREATE TABLE `lessons`.`faqs` (`qid` INT NOT NULL AUTO_INCREMENT,`question` varchar(255),`answer` text)ENGINE = MYISAMCHARACTER SET utf8;insert into faqs values (0, "Where can I download tomcat?", "http://tomcat.apache.org/");insert into faqs values (1, "Where can I download MySQL?", "http://dev.mysql.com/downloads/" );select * from faqs;
The next step is to access the faws table in the database Lessons. As you recall from above the preliminary work was completed in
tcapp.xml. We now need to add a a link to the code in the index.jsp in order to invoke the database read. Which is seen below:
<%@ page contentType="text/html; charset=iso-8859-1" language="java" import="java.sql.*" errorPage="" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<style title="text/css">
<!-- a { text-decoratopm: none }
body { font-family: vernada, helvetica; sans serif; font-size: 10pt; } -->
</style>
<% String os = System.getProperty("os.name");%>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<title>TCAPP Check Tomcat's Installation - Show OS</title>
</head>
<body>
<center>Welcome to the <U>Tomcat Server</U> </center>
<P>My Operating System is <%=os%></P>
<P>Retrieve [<a href="getData.jsp">data</a>] </p>
<form name="form2" method="post" action="InsertData.jsp">
<p>Enter Title
<input type="text" name="question">
</p>
<p>Enter URL
<input type="text" name="answer">
</p>
<input type="submit" name="Submit" value="Submit">
</form>
</body>
</html>
The getData.jsp actually does a select on the faqs table and retrieves the results.
The InsertData.jsp simply inserts values to the faqs database.
That should complete this entry!! Next we'll look at a full CRUD (Creaete, Read, Update, Delete) implementation and refactor the JSP code to a POJO (Plain Old Java Objects) implementation.
Understanding MySQL users can be found [here] this is important and will remove confusion from your daily grind. Suggestion level is set to HIGH; Read this document!
tags: open source sos sensible open source open office java j2ee tomcat servlets jsp
links: digg this del.icio.us technorati reddit