tomcat6-jdk1.6实战篇

+++++++++++++++++++++++++++++++++++++++++++++++++++++++++
秀脱linux笔记之tomcat6实战篇

转载请说明出处:
http://hi.baidu.com/xiutuo/blog/item/4222e119ad9a914743a9adbc.html

环境:
--------------------------------------------
操作系统:centos 4.5_x86
数据库oracle 10.2.01_x86_32
jre:1.6:jdk-6u1-linux-i586-rpm.bin
tomcat6.0:编译好的二进制软件包apache-tomcat-6.0.13.tar.gz
tomcat6.0 安装路径:/usr/local/tomcat6
oracle jdbc:class12.jar
软件包存放的路径:/home/xiutuo/software/
--------------------------------------------
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
一:安装jre
1.安装jre软件包,
# cd /home/xiutuo/software
进入软件包存放目录
# chmod a+x jdk-6u1-linux-i586-rpm.bin
使它有执行权限
# ./jdk-6u1-linux-i586-rpm.bin
多敲几个空格,然后看到[yes]的时候输入yes,回车
然后在当前目录下就生成了jdk-6u1-linux-i586-rpm
# rpm -ivh jdk-6u1-linux-i586-rpm
安装jdk
如果之前有其他老版本的jdk,卸载掉,如:
rpm -e j2sdk-1.4.1-fcs
2.:设置环境变量     
      仅设置某个用户而不是所有用户可以修改~/.cshrc,~/.bash_profile文件,
追加下面一段,如果为所有用户以及以后添加的用户都加上环境变量的话,
追加/etc/profie文件.     
***主意不要在profile和~/.cshrc或~/.bash_profile中重复追加**
这里:
# vi /etc/profile     
//java     
//如果你安装的j2sdk的路径不一样,请自行修改
     
JAVA_HOME=/usr/java/jdk1.6.0_01   
export JAVA_HOME   
CLASSPATH=/usr/java/jdk1.6.0_01/lib:/usr/java/jdk1.6.0_01/jre/lib   
export CLASSPATH   
PATH=$PATH:$JAVA_HOME/bin:$JAVA_HOME/jre/bin   
export PATH   
JRE=/usr/java/jdk1.6.0_01/jre   
export JRE
3:立即生效     
//如果你修改的是/etc/profile     
# source /etc/profile     
//如果你修改的是~/.cshrc     
# source ~/.cshrc     
//如果你修改的是~/.bash_profile     
# source ~/.bash_profile     
   
4:测试成功否     
# java -version
出现
java version "1.4.2" //这里比较怪,本来是jre是1.6这里居然……
gcj (GCC) 3.4.6 20060404 (Red Hat 3.4.6-8)
Copyright (C) 2006 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.    There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
**********************************************************
解决这个问题的方法,
# cd /usr/java/
# ls -l
看看default和latest这俩个文件夹是不是jdk1.6.0_01的软连接
如果是就不用修改了,否则删除default和latest,
# rm -rf default
# rm -ff latest
然后
# ln -s jdk1.6.0_01 default
# ln -s jdk1.6.0_01 latest
删除/usr/bin/目录中的
java,javac,javaws
# cd /usr/bin
# rm -f java
# rm -f javac
# rm -f javaws
然后:
# ln -s /usr/java/default/bin/java /usr/bin/java
# ln -s /usr/java/default/bin/javac /usr/bin/javac
# ln -s /usr/java/default/bin/javaws /usr/bin/javaws
最好再
//如果你修改的是/etc/profile     
# source /etc/profile     
//如果你修改的是~/.cshrc     
# source ~/.cshrc     
//如果你修改的是~/.bash_profile     
# source ~/.bash_profile     
**********************************************************
再执行
# java -version    //这回正常了
java version "1.6.0_01"
Java(TM) SE Runtime Environment (build 1.6.0_01-b06)
Java HotSpot(TM) Client VM (build 1.6.0_01-b06, mixed mode, sharing)

二:安装tomcat6
1.安装tomcat
# cd /home/xiutuo/software                     //进入软件包存放目录
# tar -zvxf apache-tomcat-6.0.13.tar.gz        //解压
# mv apache-tomcat-6.0.13 /usr/local/tomcat6 //安装
# cd /usr/local/tomcat6/bin/                  //进入安装目录
# tar xvfz jsvc.tar.gz                        //解压
# cd jsvc-src                                 //进入jsvc-src目录
# autoconf
# ./configure
# make                                        //编译生成jsvc文件
# cp jsvc ..                                  //拷贝jsvc到上级目录
# cd ..                                       //回到上级目录

# cd /usr/local/tomcat6/
//运行下面的命令可以使tomcat以daemon方式运行
# ./bin/jsvc -cp ./bin/bootstrap.jar \      
     -outfile ./logs/catalina.out \
     -errfile ./logs/catalina.err \
     org.apache.catalina.startup.Bootstrap
2.配置oralce的jdbc
//拷贝oracle的jdbc到/usr/local/tomcat6/lib目录下
# cp /home/software/class12.jar ./lib/
3.配置tomcat用户
# cd /usr/local/tomcat6/conf
# vi tomcat-users.xml
内容如下:
<?xml version='1.0' encoding='utf-8'?>
<tomcat-users>
    <role rolename="tomcat"/>
    <role rolename="role1"/>
    <role rolename="manager"/>
    <role rolename="admin"/>
    <user username="tomcat" password="tomcat" roles="tomcat"/>
    <user username="role1" password="tomcat" roles="role1"/>
    <user username="both" password="tomcat" roles="tomcat,role1"/>
    <user username="admin" password="admin" roles="admin,manager"/>
</tomcat-users>
请修改tomcat里所有定义用户的密码:即修改password为自己想要的值。

4.启动停止tomcat服务
# cd    /usr/local/tomcat6/bin
# ./start.sh      //启动tomcat
# ./shutdown.sh //停止tomcat
查看tomcat的进程:
# ps -e | grep java
出现了java这个进程的话,说明你的tomcat差不多了。
5.让tomcat开机运行:
# cd    /usr/local/tomcat6/bin/jsvc-src/native/
# vi tomcat.sh
Tomcat can then be run as a daemon using the following commands.
cd $CATALINA_HOME
    ./bin/jsvc -cp ./bin/bootstrap.jar \
    -outfile ./logs/catalina.out -errfile ./logs/catalina.err \
          org.apache.catalina.startup.Bootstrap

The file $CATALINA_HOME/bin/jsvc/native/tomcat.sh can be used
as a template for starting Tomcat automatically at boot time
from /etc/init.d. The file is currently setup for running
Tomcat 4.1.x, so it is necessary to edit it and change
the classname from BootstrapService to Bootstrap.
Note that the Commons-Daemon JAR file must be on your runtime
classpath to run Tomcat in this manner. The Commons-Daemon JAR file
is in the Class-Path entry of the bootstrap.jar manifest, but if you
get a ClassNotFoundException or a NoClassDefFoundError for
a Commons-Daemon class, add the Commons-Daemon JAR to
the -cp argument when launching jsvc.

三:配置tomcat应用实列
浏览器输入:http://localhost:8080/
点击manager:输入刚才配置的tomcat用户:用户admin和密码admin
配置虚拟目录test,并通过 http://localhost:8080/test/来访问
1.手动配置
在 /usr/local/tomcat6/webapps目录下,
建立testdir目录,把相关jsp原代码放入该目录。
新建test.xml,并配置context和连接池
# cd /usr/local/tomcat6/webapps/
# vi test.xml
内容如下:
<Context path="/test" docBase="/usr/local/tomcat6/webapps/testdir"
          debug="5" reloadable="true" crossC>
<Resource name="jdbc/myoracle" auth="Container"
     type="javax.sql.DataSource" driverClassName="oracle.jdbc.OracleDriver"
     url="jdbc:oracle:thin:@192.168.1.159:1521:db01"
     username="oracele_db_user" password="db_owner_password" maxActive="20"
     maxIdle="10" maxWait="-1"/>
</Context>
其中:
context path="/test"----就是http://localhost:8080/test/里的test虚拟目录
<Resource name="jdbc/myoracle"
---- myoracle连接池名
url="jdbc:oracle:thin:@192.168.1.159:1521:db01"
--192.168.1.159:1521:db01是数据库ip:端口:SID,
username="oracele_db_owner_user" password="db_owner_password"
---分别是数据库用户和用户密码
打开http://localhost:8080/,点击manager,进入后,
在Deploy 这一栏里
Deploy directory or WAR file located on server
Context Path (optional): /test
XML Configuration file URL: /usr/local/tomcat6/testdir/test.xml   
WAR or Directory URL: 留空即可
点击deploy按钮。即配置了一个web应用
2.通过war文件来配置。
在WAR file to deploy这一栏里
Select WAR file to upload:通过浏览把war文件路径添加上,
就可以架设应用事例了,可以参照官方文档Sample应用实例
----------------------------------------------------
Sample Application
The example app has been packaged as a war file and can be
downloaded here (Note: make sure your browser doesn't
change file extension or append a new one).
The easiest way to run this application is simply to move the
war file to your CATALINA_HOME/webapps directory.
Tomcat will automatically expand and deploy the application for you.
You can view it with the following URL
(assuming that you're running tomcat on port 8080 as is the default):
http://localhost:8080/sample
If you just want to browse the code you can
unpack the war file with the jar command. jar -xvf sample.war
---------------------------------------------------
**************************************
补充:
当然可以把test.xml的内容放在
/usr/local/tomcat6/conf/server.xml中的<host>与</host>之间
然后这个应用的程序的文件夹里新建 WEB-INF/web.xml
内容如下:   
<resource-ref>
<description>Oracle Datasource example</description>
<res-ref-name>jdbc/myoracle</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref>
测试jdbc代码:
<%@ taglib uri="http://java.sun.com/jsp/jstl/sql" prefix="sql" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<sql:query var="rs" dataSource="jdbc/TestDB">
select id, foo, bar from testdata
</sql:query>
<html>
    <head>
      <title>DB Test</title>
    </head>
    <body>
    <h2>Results</h2>
  
<c:forEach var="row" items="${rs.rows}">
      Foo ${row.foo}<br/>
      Bar ${row.bar}<br/>
</c:forEach>
    </body>
</html>
***************************************
其他的数据源连接池子参照官方文档,
基本上修改test.xml里的<Resource里的内容就好了
***************************************