歡迎您光臨本站 註冊首頁

tomcat6-jdk1.6實戰篇

←手機掃碼閱讀     火星人 @ 2014-03-05 , reply:0

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,回車
然後在當前目錄下就生成了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里的內容就好了
***************************************

[火星人 ] tomcat6-jdk1.6實戰篇已經有543次圍觀

http://coctec.com/docs/service/show-post-37939.html