http://ms.ntcb.edu.tw/~steven/l-penguin.s/article/httpd-tomcat-mysql-jk.htm在前一篇
Apache 及 Tomcat 的结合 (New window) 的文件中,说明了使用 mod_jk2 把 Apache 与 Tocmat 相结合的实例。最近小弟遇到了主机的扩充案,重新调整了 Apche 的编译,并且使用新版本的 Tomcat Connectors JK 1.2.26 版本来做两者之间的沟通。
本篇文章同时也介绍了 Tomcat 6.0 的 MySQL Connection Pool 用法(同时适用於 Tomcat 5.5),实做中也提及如何测试资料库的连线。
此文章与前一篇
Apache 及 Tomcat 的结合 (New window)并不相容,Tomcat 3 和 Tomcat 5.5 的 Connection Pool 用法已不同;而 mod_jk2
也不再维护,Tomcat 组织以 mod_jk
为主要的发展对像,所以在这些改变之下,决定重新写此篇文件以供大家参考,若有不明或有误的地方请大家提出。
架构Apache 服务Tomcat 服务MySQL 服务架构
使用者在浏览网站时,是对应到 Apache Server,而 JSP 程式则是放在 Tomcat 主机上,当然了资料库也是独立的。使用这种架构可分散网页档案与程式之间的损失风险。
架构设计之初,应避免把 应用伺服器 与 资料库伺服器 暴露在网路之中,可以选择使用内部的虚拟 IP 或防火墙来防止使用者可任意寻找露洞的机会。当然了,安全性的研究不在本次的议题里,大家可以参阅 查询相关资料。
以下 IP 为本次的实例分配:
Apache:192.168.1.205(Apache 2.2.9,Tomcat Connector 1.2.26)
Tomcat:192.168.1.207(Tomcat 6.0.16)
MySQL:192.168.1.204(MySQL 5.0.45)
Apache 服务
要完成整个 Apache 伺服器的设定,需要下例套件配合:
Sun Java JDK(Java version 1.6.0_06)
Apache(httpd 2.2.9)
Tomcat(tomcat 6.0.16)
Tomcat Connector(Tomcat Connector 1.2.26)
以下会逐一说明。
下载安装 Apache
下载 Apache 并解压缩
最新版本的 Apache 可以到 Apache 官方网站下载:
http://httpd.apache.org/编译 httpd
若是采用此方式,在您的环境中必需要有 gcc 编译器
0001
00020003 0004 0005 0006 0007
| root # cd httpd-2.2.9
root # ./configure --prefix=/usr/local/httpd --with-mpm=worker
> --enable-cache --enable-file-cache --enable-disk-cache --enable-mem-cache
> --enable-mime-magic --enable-headers --enable-ssl --enable-http --enable-cgi
> --enable-rewrite --enable-so --with-suexec-gidmin --with-suexec-logfile
root # make
root # make install |
以上编译到安装的过程可能需要一段时间,结束之后 httpd 会安装到 /usr/local/httpd 目录,使用下例指可以启动 http 服务,即可使用浏览器联结。
0001 | root # /usr/local/httpd/bin/apachectl start |
查看 httpd 是否有启动并设定连接埠。
0001
00020003 0004 0005 0006 0007 0008 0009 0010
| root # ps aux | grep httpd; netstat -ntulp | grep :80
root 3021 0.0 0.7 7908 2808 ? Ss 18:55 0:00 /usr/local/httpd/bin/httpd -k start
daemon 3022 0.0 0.4 7512 1648 ? S 18:55 0:00 /usr/local/httpd/bin/httpd -k start
daemon 3023 0.0 0.7 284680 2904 ? Sl 18:55 0:00 /usr/local/httpd/bin/httpd -k start
daemon 3024 0.0 0.7 284680 2856 ? Sl 18:55 0:00 /usr/local/httpd/bin/httpd -k start
daemon 3035 0.0 0.7 284680 2904 ? Sl 18:55 0:00 /usr/local/httpd/bin/httpd -k start
daemon 3200 0.0 0.7 284680 2836 ? Sl 18:57 0:00 /usr/local/httpd/bin/httpd -k start
root 3824 0.0 0.1 1820 484 pts/0 S+ 22:16 0:00 grep httpd
tcp 0 0 :::80 :::* LISTEN 3021/httpd |
下载安装 Tomcat、Sun Java JDK 与 Tomcat Connector
Tomcat 可到
http://tomcat.apache.org/ 下载、Tomcat Connector 可到
http://tomcat.apache.org/download-connectors.cgi 下载,本例中 Java JDK 是采用 Sun 所发展的 Sun Java JDK,可至
http://java.sun.com/javase/downloads/index.jsp 下载 JDK 即可。
下载 Tomcat Connector
下载 Sun Java JDK
0001 | root # lynx 'http://cds.sun.com/is-bin/INTERSHOP.enfinity/WFS/CDS-CDS_Developer-Site/en_US/-/USD/VerifyItem-Start/jdk-6u6-linux-i586.bin?BundledLineItemUUID=fR9IBe.pEDAAAAEaAgAocbiQ&OrderID=YtFIBe.pnk8AAAEa9_8ocbiP&ProductID=VXZIBe.ootIAAAEZTrBAkQve&FileName=/jdk-6u6-linux-i586.bin' |
下载 Tomcat
搬移档案至 /opt 目录
0001 | root # mv apache-tomcat-6.0.16.tar.gz tomcat-connectors-1.2.26-src.tar.gz jdk-6u6-linux-i586.bin /opt/ |
解压缩档案
0001 | root # tar -zxvf apache-tomcat-6.0.16.tar.gz ; tar -zxvf tomcat-connectors-1.2.26-src.tar.gz ; chmod +x jdk-6u6-linux-i586.bin |
设定与安装 Tomcat、Tomcat Connector 与 Sun Java JDK
安装 Sun Java JDK
0001
00020003 0004 0005 0006 0007 0008 0009 0010 0011 0012 0013 0014 0015 0016 0017 0018 0019 0020 0021 0022 0023 0024 0025 0026 0027 0028 0029 0030 0031 0032 0033 0034 0035 0036 0037 0038 0039 0040 0041 0042 0043 0044 0045 0046 0047 0048 0049 0050 0051 0052 0053 0054 0055 0056 0057 0058 0059 0060 0061 0062 0063 0064 0065 0066 0067 0068 0069 0070 0071 0072 0073 0074
| root # ./jdk-6u6-linux-i586.bin
Sun Microsystems, Inc. Binary Code License Agreement
for the JAVA SE DEVELOPMENT KIT (JDK), VERSION 6
SUN MICROSYSTEMS, INC. ("SUN") IS WILLING TO LICENSE THE
SOFTWARE IDENTIFIED BELOW TO YOU ONLY UPON THE CONDITION
THAT YOU ACCEPT ALL OF THE TERMS CONTAINED IN THIS BINARY
CODE LICENSE AGREEMENT AND SUPPLEMENTAL LICENSE TERMS
(COLLECTIVELY "AGREEMENT"). PLEASE READ THE AGREEMENT
CAREFULLY. BY DOWNLOADING OR INSTALLING THIS SOFTWARE, YOU
ACCEPT THE TERMS OF THE AGREEMENT. INDICATE ACCEPTANCE BY
SELECTING THE "ACCEPT" BUTTON AT THE BOTTOM OF THE
AGREEMENT. IF YOU ARE NOT WILLING TO BE BOUND BY ALL THE
TERMS, SELECT THE "DECLINE" BUTTON AT THE BOTTOM OF THE
AGREEMENT AND THE DOWNLOAD OR INSTALL PROCESS WILL NOT
CONTINUE.
1. DEFINITIONS. "Software" means the identified above in
binary form, any other machine readable materials
(including, but not limited to, libraries, source files,
header files, and data files), any updates or error
corrections provided by Sun, and any user manuals,
programming guides and other documentation provided to you
--More--
~~~~~~~~~~~~~~~~~ignore~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
THIRDPARTYLICENSEREADME.txt file, the disclaimer of
warranty and limitation of liability provisions in
paragraphs 5 and 6 of the Binary Code License Agreement
shall apply to all Software in this distribution.
H. Termination for Infringement. Either party may terminate
this Agreement immediately should any Software become, or
in either party's opinion be likely to become, the subject
of a claim of infringement of any intellectual property
right.
I. Installation and Auto-Update. The Software's
installation and auto-update processes transmit a limited
amount of data to Sun (or its service provider) about those
specific processes to help Sun understand and optimize
them. Sun does not associate the data with personally
identifiable information. You can find more information
about the data Sun collects at http://java.com/data/.
For inquiries please contact: Sun Microsystems, Inc., 4150
Network Circle, Santa Clara, California 95054, U.S.A.
Do you agree to the above license terms? [yes or no]
yes
~~~~~~~~~~~~~~~~~~~ignore~~~~~~~~~~~~~~~~~~~~~``
Java(TM) SE Development Kit 6 successfully installed.
Product Registration is FREE and includes many benefits:
* Notification of new versions, patches, and updates
* Special offers on Sun products, services and training
* Access to early releases and documentation
Product and system data will be collected. If your configuration
supports a browser, the Sun Product Registration form for
the JDK will be presented. If you do not register, none of
this information will be saved. You may also register your
JDK later by opening the register.html file (located in
the JDK installation directory) in a browser.
For more information on what data Registration collects and
how it is managed and used, see: http://java.sun.com/javase/registration/JDKRegistrationPrivacy.html
Press Enter to continue.....
Done. |
安装 Tomcat
0001 | root # mv apache-tomcat-6.0.16 tomcat; mv jdk1.6.0_06/ java |
设定环境变数
在安装 mod_jk 之前,需先设定 tomcat 与 java 的目录,可在 /etc/profile 设定。
0001
00020003 0004 0005 0006 0007
| JAVA_HOME=/opt/java
CATALINA_HOME=/opt/tomcat
CATALINA_BASE=/opt/tomcat
CLASSPATH=./:${JAVA_HOME}/lib:${JAVA_HOME}/jre/lib/ext:${CATALINA_HOME}/common/lib
export JAVA_HOME CATALINA_HOME CATALINA_BASE CLASSPATH
export PATH=${JAVA_HOME}/bin:${CATALINA_HOME}/bin:${PATH} |
设定完成后可使用 source 指令或 . 指令让修改的项目生效。
0001 | root # . /etc/profile |
编译安装 mod_jk
完成以上设定后,即可开始编译 mod_jk。
编译 mod_jk
0001
00020003
| root # cd tomcat-connectors-1.2.26-src/native/
root # ./configure --with-java-home=/opt/java --with-apxs=/usr/local/httpd/bin/apxs
root # make |
安装 mod_jk.so 至 httpd
0001 | root # cp apache-2.0/mod_jk.so /usr/local/httpd/lib/ |
以上完成编译安装之后,就可以开始设定 apache,让 mod_jk 生效。apache 的设定档在 /usr/local/httpd/conf/ 目录 下。
设定 Apche 与 mod_jk
编辑 httpd.conf
在 httpd.conf 中加入载入 mod_jk 模组与 mod_jk 设定档项目。
0001
00020003
| LoadModule jk_module lib/mod_jk.so
# mod_jk settings
Include conf/mod_jk.conf |
在 /usr/local/httpd/conf/ 目录加入 mod_jk.conf
0001 | root # touch mod_jk.conf |
在 mod_jk.conf 加入以下内容
0001
00020003 0004 0005 0006 0007 0008 0009 0010 0011 0012 0013 0014 0015
| #setup the workers.properties file path, default prefix path is httpd's home (/usr/local/httpd)
JkWorkersFile conf/workers.properties
JkLogFile /var/log/jk.log
JkShmFile /var/log/jk-runtime-status
JkLogLevel error
JkLogStampFormat "[%a %b %d %H:%M:%S %Y] "
JkOptions +ForwardKeySize +ForwardURICompat -ForwardDirectories
JkRequestLogFormat "%w %V %T"
# Sample JkMounts. Replace these with the paths you would
# like to mount from your JSP server.
# syntax: JkMount ${URL_DIR}/*.jsp worker_name
JkMount /project1/*.jsp jsp-ap207
JkMount /project2/*.jsp jsp-ap207
JkMount /project3/*.jsp jsp-ap207 |
增加 conf/workers.properties 档案
0001 | root # touch /usr/local/httpd/conf/workers.properties |
设定 workers.properties
0001
00020003 0004 0005 0006 0007 0008 0009 0010 0011
| # BEGIN workers.properties
# setting tomcat_home and java_home
workers.tomcat_home=/opt/tomcat
workers.java_home=/opt/java
# worker.list defined worker_name, used by mod_jk.conf
worker.list=jsp-ap207
worker.jsp-ap207.port=8009
worker.jsp-ap207.host=192.168.1.207
worker.jsp-ap207.type=ajp13
worker.jsp-ap207.lbfactor=1 |
重新启动 Apache
经过了以上步骤后,在 Apache 的部份可说是已完成设定(当然细项目份还需要调整)。现在可重新启动 Apache。
001
0002 | root # /usr/local/httpd/bin/apachectl stop
root # /usr/local/httpd/bin/apachectl start |
Tomcat 服务
要完成整个 Tomcat 的设定,需要以下套件配合
Tomcat(
http://tomcat.apache.org/)
Java JDK(
http://java.sun.com/javase/downloads/index.jsp)
jaf(
http://java.sun.com/javase/technologies/desktop/javabeans/jaf/downloads/index.html)
javamail(
http://java.sun.com/products/javamail/downloads/index.html)
下载与安装 Tomcat、Java JDK、jaf 与 javamail
下载 Tomcat
下载 Sun Java JDK
0001 | root # lynx 'http://cds.sun.com/is-bin/INTERSHOP.enfinity/WFS/CDS-CDS_Developer-Site/en_US/-/USD/VerifyItem-Start/jdk-6u6-linux-i586.bin?BundledLineItemUUID=fR9IBe.pEDAAAAEaAgAocbiQ&OrderID=YtFIBe.pnk8AAAEa9_8ocbiP&ProductID=VXZIBe.ootIAAAEZTrBAkQve&FileName=/jdk-6u6-linux-i586.bin' |
下载 jaf
0001 | root # lynx 'http://cds.sun.com/is-bin/INTERSHOP.enfinity/WFS/CDS-CDS_Developer-Site/en_US/-/USD/VerifyItem-Start/jaf-1_0_2-upd2.zip?BundledLineItemUUID=WoRIBe.m5QUAAAEaKzlGV8Li&OrderID=VYdIBe.mwygAAAEaHzlGV8Li&ProductID=J_vACUFBIFEAAAEYeEo5AXiq&FileName=/jaf-1_0_2-upd2.zip' |
下载 javamail
0001 | root # lynx 'http://cds.sun.com/is-bin/INTERSHOP.enfinity/WFS/CDS-CDS_Developer-Site/en_US/-/USD/VerifyItem-Start/javamail-1_4_1.zip?BundledLineItemUUID=n7RIBe.mEw8AAAEavaVGV8Lk&OrderID=iOdIBe.mpdYAAAEarqVGV8Lk&ProductID=3v_ACUFBMTsAAAEYxBQ5AXuF&FileName=/javamail-1_4_1.zip' |
解压缩档案
0001
00020003 0004 0005 0006 0007 0008 0009 0010 0011 0012 0013 0014 0015 0016 0017 0018 0019 0020 0021 0022 0023 0024 0025 0026 0027 0028 0029 0030 0031 0032 0033 0034 0035 0036 0037 0038 0039 0040 0041 0042 0043 0044 0045 0046 0047 0048 0049 0050 0051 0052 0053 0054 0055 0056 0057 0058 0059 0060 0061 0062 0063 0064 0065 0066 0067 0068 0069 0070 0071 0072 0073 0074 0075
| root # tar -zxvf apache-tomcat-6.0.16.tar.gz ;unzip jaf-1_0_2-upd2.zip ; unzip javamail-1_4_1.zip; chmod +x jdk-6u6-linux-i586.bin root # ./jdk-6u6-linux-i586.bin
Sun Microsystems, Inc. Binary Code License Agreement
for the JAVA SE DEVELOPMENT KIT (JDK), VERSION 6
SUN MICROSYSTEMS, INC. ("SUN") IS WILLING TO LICENSE THE
SOFTWARE IDENTIFIED BELOW TO YOU ONLY UPON THE CONDITION
THAT YOU ACCEPT ALL OF THE TERMS CONTAINED IN THIS BINARY
CODE LICENSE AGREEMENT AND SUPPLEMENTAL LICENSE TERMS
(COLLECTIVELY "AGREEMENT"). PLEASE READ THE AGREEMENT
CAREFULLY. BY DOWNLOADING OR INSTALLING THIS SOFTWARE, YOU
ACCEPT THE TERMS OF THE AGREEMENT. INDICATE ACCEPTANCE BY
SELECTING THE "ACCEPT" BUTTON AT THE BOTTOM OF THE
AGREEMENT. IF YOU ARE NOT WILLING TO BE BOUND BY ALL THE
TERMS, SELECT THE "DECLINE" BUTTON AT THE BOTTOM OF THE
AGREEMENT AND THE DOWNLOAD OR INSTALL PROCESS WILL NOT
CONTINUE.
1. DEFINITIONS. "Software" means the identified above in
binary form, any other machine readable materials
(including, but not limited to, libraries, source files,
header files, and data files), any updates or error
corrections provided by Sun, and any user manuals,
programming guides and other documentation provided to you
--More--
~~~~~~~~~~~~~~~~~ignore~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
THIRDPARTYLICENSEREADME.txt file, the disclaimer of
warranty and limitation of liability provisions in
paragraphs 5 and 6 of the Binary Code License Agreement
shall apply to all Software in this distribution.
H. Termination for Infringement. Either party may terminate
this Agreement immediately should any Software become, or
in either party's opinion be likely to become, the subject
of a claim of infringement of any intellectual property
right.
I. Installation and Auto-Update. The Software's
installation and auto-update processes transmit a limited
amount of data to Sun (or its service provider) about those
specific processes to help Sun understand and optimize
them. Sun does not associate the data with personally
identifiable information. You can find more information
about the data Sun collects at http://java.com/data/.
For inquiries please contact: Sun Microsystems, Inc., 4150
Network Circle, Santa Clara, California 95054, U.S.A.
Do you agree to the above license terms? [yes or no]
yes
~~~~~~~~~~~~~~~~~~~ignore~~~~~~~~~~~~~~~~~~~~~``
Java(TM) SE Development Kit 6 successfully installed.
Product Registration is FREE and includes many benefits:
* Notification of new versions, patches, and updates
* Special offers on Sun products, services and training
* Access to early releases and documentation
Product and system data will be collected. If your configuration
supports a browser, the Sun Product Registration form for
the JDK will be presented. If you do not register, none of
this information will be saved. You may also register your
JDK later by opening the register.html file (located in
the JDK installation directory) in a browser.
For more information on what data Registration collects and
how it is managed and used, see: http://java.sun.com/javase/registration/JDKRegistrationPrivacy.html
Press Enter to continue.....
Done. |
将档案移动到 /opt 目录
0001 | root # mv apache-tomcat-6.0.16 /opt/tomcat; mv jaf-1.0.2/ /opt/jaf; mv javamail-1.4.1/ /opt/javamail; mv jdk1.6.0_06/ /opt/java |
以上过程会把 Tomcat、jaf、javamail 与 Sun Java JDK 安装完成,现在必需设定系统变数才可使 tomcat 顺利启动。
编辑 /etc/profile 档案,并加入以下设定。
0001
00020003 0004 0005 0006 0007
| JAVA_HOME=/opt/java
CATALINA_HOME=/opt/tomcat
CATALINA_BASE=/opt/tomcat
CLASSPATH=./:${JAVA_HOME}/lib:${JAVA_HOME}/jre/lib/ext:${CATALINA_HOME}/common/lib:/opt/javamail/mail.jar:/opt/jaf/activation.jar
export JAVA_HOME CATALINA_HOME CATALINA_BASE CLASSPATH
export PATH=${JAVA_HOME}/bin:${CATALINA_HOME}/bin:${PATH} |
完整的设定 /etc/profile 之后执行下列指令可立即生效。
0001 | root # . /etc/profile |
完成以上项目后,即可开始设定 tomcat,让 tomcat 接受处理 Apache 的请求。
设定 tomcat
以下说明的 tomcat 设定档均指 /opt/tomcat/conf/server.xml 档案,若您的 tomcat 安装在不同路径请自行更改。
处理 Apache 所指示的档案,需要 tomcat 的配合,预设 tomcat 开启 8009 连接埠的请求,因此只需要设定虚拟目录即可。
设定
http://192.168.1.205/project1 目录,设定时先先定议一个虚拟网站,再设定虚拟目录,虚拟网站由
<Host></Host> 的 xml 标签所包含,而目录是以
<Context></Context> 所包含,其格式如下:
0001
00020003 0004 0005 0006
| <Engine>
<Host attrs ...>
<Context attrs ...>
</Context>
</Host>
</Engine> |
server.xml 修改过后的样式如下:
0001
00020003 0004 0005
| <Host name="192.168.1.205" debug="0" appBase="/default" unpackWARs="true" autoDeploy="true">
<Context path="/project1" docBase="/projects/project1"
debug="5" reloadable="true" crossContext="true">
</Context>
</Host> |
以上 <Context> 标签里的 path 属性是指 URL 的路径,实际对应的目录为 /projects/project1/。设定完成后,请先行建立 /projects/project1/ 目录。
0001 | root # mkdir -p "/projects/project1"; cd /projects/project1 |
启动 tomcat 服务。
0001
00020003 0004 0005
| root # /opt/tomcat/bin/startup.sh
Using CATALINA_BASE: /opt/tomcat
Using CATALINA_HOME: /opt/tomcat
Using CATALINA_TMPDIR: /opt/tomcat/temp
Using JRE_HOME: /opt/java |
测试
完成了以上 Apache 与 Tomcat 的设定,现在可进行测试,请先在 Tomcat 主机上的 /projects/project1/ 建立一个 hello.jsp 程式,内容如下。
0001
00020003 0004 0005 0006
| <%@ page contentType="text/html;charset=gb2312" %>
<%
String str1="Hello World!";
out.println(str1);
%> |
hello.jsp 程式可点选
此处 下载。
现在,开启您的浏览器,输入
http://192.168.1.205/project1/hello.jsp 网址。
gle_ad_client = "pub-6969296564641751";
/* 728x90, 已建立 2008/6/27 */
google_ad_slot = "0944321075";
google_ad_width = 728;
google_ad_height = 90;
//-->
window.google_render_ad();
MySQL 设定
MySQL 设定的方式很简单,基乎不需要任何的设定,只要确定可以连线到资料库即可。
在 Tomcat 伺服器上,测试 MySQL 连线方如下:
01
02
03
04
05
06
07
08
09 | steven $ mysql -u steven -p -h 192.168.1.204
Enter password:
Welcome to the MySQL monitor. Commands end with ; or g.
Your MySQL connection id is 443 to server version: 5.0.22-log
Type 'help;' or 'h' for help. Type 'c' to clear the buffer.
mysql> |
以上若可顺利连线到 MySQL 主机则表示成功。
若是连线失败,请检查防火墙设定与 MySQL 是否有启动。
安装 MySQL Connection/J
要使 tomcat 与 MySQL 可以做资源联结,需要有 MySQL Connector/J 的配合才行,可到 MySQL 网站(
http://dev.mysql.com/downloads/connector/j/)中下载。
本文编写时,最新版本的 Connector/J 为 5.1.6 版。
01
02 | root # lynx 'http://dev.mysql.com/get/Downloads/Connector-J/mysql-connector-java-5.1.6.tar.gz/from/http://mysql.ntu.edu.tw/'
root # tar -zxvf mysql-connector-java-5.1.6.tar.gz |
下载完后,把 mysql-connector-java-5.1.6-bin.jar 复制到 /opt/tomcat/common/lib/ 里即可
01 | root # cp mysql-connector-java-5.1.6/mysql-connector-java-5.1.6-bin.jar /opt/tomcat/common/lib/ |
编辑 Tomcat 设定档
设定 Tomcat Database Connection Pool 的方式很简单,只要在目录里加上 <Resource></Resource> 标签即可,使用的方式如下:
01
0203 04 05 06 07
| <Engine>
<Host attrs ...>
<Context attrs ...>
<Resource attrs ... />
</Context>
</Host>
</Engine> |
把 <Resource> 标签放在 <Context> 里,则此目录的程式都可以引用在此设定的资料库连线资源,以下为实际修改方式:
01
02
03
04
05
06
07
08
0910
| <Host name="192.168.1.205" debug="0" appBase="/default" unpackWARs="true" autoDeploy="true">
<Context path="/project1" docBase="/projects/project1"
debug="5" reloadable="true" crossContext="true">
<Resource name="jdbc/project1" auth="Container" type="javax.sql.DataSource"
maxActive="100" maxIdle="30" maxWait="10000"
username="steven" password="pass" driverClassName="com.mysql.jdbc.Driver"
url="jdbc:mysql://192.168.1.204:3306/project1?autoReconnect=true"/>
</Context>
</Host> |
在 <Resource> 标签里的 name 是指资料库资源名称,username 与 password
为连线的帐号与密码,而 url 里设定了所要连结的资料库主机与资料库名称。当然您必需把 project1 这个资料库先建立起来并设定
steven 帐号可以存取才行。
注意:您可以同时设定多个 Resource 资源,只要 name 不重复即可。
设定完成后,需重新启动 tomcat 伺服器。
闭关 tomcat
0001
00020003 0004 0005
| root # /opt/tomcat/bin/shutdown.sh
Using CATALINA_BASE: /opt/tomcat
Using CATALINA_HOME: /opt/tomcat
Using CATALINA_TMPDIR: /opt/tomcat/temp
Using JRE_HOME: /opt/java |
启动 tomcat
0001
00020003 0004 0005
| root # /opt/tomcat/bin/startup.sh
Using CATALINA_BASE: /opt/tomcat
Using CATALINA_HOME: /opt/tomcat
Using CATALINA_TMPDIR: /opt/tomcat/temp
Using JRE_HOME: /opt/java |
测试 JSP 连线
请在 /projects/project1/ 里建立 db_test.jsp,而 db_test.jsp 的内容如下:
01
02
03
04
05
06
07
08
0910 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
| <%@ page import="java.sql.*"%>
<%@ page import="javax.sql.*"%>
<%@ page import="javax.naming.*"%>
<%@ page session="false" %>
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=gb2312">
<title>Test of mysql connection pool</title>
</head>
<body>
<%
out.print("start<br/>");
try{
Context initctx = new InitialContext();
Context ctx = (Context) initctx.lookup("java:comp/env");
Object obj = (Object) ctx.lookup("jdbc/project1");
javax.sql.DataSource ds = (javax.sql.DataSource)obj;
Connection conn = ds.getConnection();
out.print("mysql connection pool runs perfectly!");
conn.close();
}
catch(Exception ex){
out.print(ex.getMessage());
ex.printStackTrace();
}
%>
</body>
</html> |
请注意,以上 jsp 里 ctx.lookup 里所指定的 jdbc 为 tomcat 设定档里的 <Resource> 名称,与 name 属性相对应。
以上 db_test.jsp 可以点选
此处 下载。
开启浏览器,输入测试程式的网址,若是连线成功则会出现 mysql connection pool runs perfectly! 字样。
若看到此讯息的话,那么恭禧您已经完成了本次的整合,接来您可以对伺服器与应用程式开始测试或调效。若您在过程中遇到了问题,欢迎提出来研究,或是在各套件的网站上都有详细的介绍与方法。
06/27/2008