Tomcat作為一個很優秀的web application server的確值得大家使用,特別是熟悉java的愛好者,不過它的配置并不象它的應用那么優秀,至少在配置上有很多瑣碎的事情要做,現在我將我自己配置最新的tomcat5.5.15的一點心得告訴大家:
總共涉及三個方面,也是tomcat配置比較煩瑣的地方
一.與apache的配置(暫略,晚點再將配置詳細介紹)
二.使tomcat5.5.15支持jstl標簽 這個是最簡單的配置,只要按照以下方法,就可以配置成功. 1.下載 jakarta-taglibs-standard-1.1.2.zip (windows) jakarta-taglibs-standard-1.1.2.tar.gz(linux) 2.解壓在解壓的lib目錄下找到jstl.jar,standard.jar這兩個.jar文件,將他們復制到 你的站點/WEB-INF/lib文件夾下,在tld目錄下將所有的.tld文件復制到 你的站點/WEB-INF/taglib/文件下 3.配置 你的站點/WEB-INF/web.xml文件,在</webapp>前添加如下內容 <taglib> <taglib-uri>http://www.hnlinux.net/core</taglib-uri> <taglib-location>/WEB-INF/taglibs/c.tld</taglib-location> </taglib>
<taglib> <taglib-uri>http://www.hnlinux.net/fn</taglib-uri> <taglib-location>/WEB-INF/taglibs/fn.tld</taglib-location> </taglib>
<taglib> <taglib-uri>http://www.hnlinux.net/sql</taglib-uri> <taglib-location>/WEB-INF/taglibs/sql.tld</taglib-location> </taglib>
<taglib> <taglib-uri>http://www.hnlinux.net/fmt</taglib-uri> <taglib-location>/WEB-INF/taglibs/fmt.tld</taglib-location> </taglib> (注意我的<taglib-uri>引用了http://www.hnlinux.net,呵呵,所以大家如果按照這樣設置的話,在jsp中引用jstl標簽是記得采用這樣寫<%@ taglib uri=http://www.hnlinux.net/core prefix="c" %>,可見這里配置是很靈活的隨便你取什么名字,只要在寫jsp的時候相對應就可以)
現在你就可以使用jstl來書寫你的jsp了.
三.連接mysql數據庫的配置 1.下載 mysql-connector-java-5.0.0-beta.zip 將mysql-connector-java-5.0.0-beta-bin.jar復制到$Tomcat_home$/common/lib文件夾,(如果你不想使用數據連接池的話,可以放在 你的站點/WEB-INF/lib文件夾下) 2.搞定了,你現在就可以使用連接數據庫的的相關語句了(這個是最簡單的,呵呵)
四.mysql連接池的配置. 1.下載commons-dbcp-1.2.1.zip 解壓將commons-dbcp-1.2.1.jar復制到$Tomcat_home$/common/lib下(保證mysql的jdbc的驅動也在此目錄) 2.修改$Tomcat_home$/cof/server.xml在context標簽內添加以下內容,如果沒有找到context標簽,注意在下面加上紅色的部分,蘭色部分根據自己情況修改,不用解釋了吧? <Context path="/hasek" docBase="hasek" debug="5" reloadable="true" crossContext="true"> <Resource name="hasek" auth="Container" type="javax.sql.DataSource" maxActive="100" maxIdle="30" maxWait="10000" username="root" password="root" driverClassName="com.mysql.jdbc.Driver" url="jdbc:mysql://localhost:3306/hasek?autoReconnect=true"/>
</Context> 注意一點的事情是:<Context path="/hasek" docBase="hasek"... 中/hasek代表的是 你的站點 <Resource name="hasek" 這個hasek代表的是你的連接池的名字,這個要和下面的web.xml里的內容相一致. url="jdbc:mysql://localhost:3306/hasek?autoReconnect=true"/>中的hasek代表你的數據庫名字
3.修改 你的站點/WEB-INF/web.xml 在</webapp>標簽前添加以下內容:
<description>MySQL Test App</description> <resource-ref> <description>DB Connection</description> <res-ref-name>hasek</res-ref-name> <res-type>javax.sql.DataSource</res-type> <res-auth>Container</res-auth> </resource-ref> 4.測試代碼(連接池與jstl標簽測試)test.jsp
<%@ taglib uri="http://www.hnlinux.net/sql" prefix="sql" %> <%@ taglib uri="http://www.hnlinux.net/core" prefix="c" %>
<sql:query var="rs" dataSource="hasek"> select * from hasek </sql:query>
<html> <head> <title>DB Test</title> </head> <body>
<h2>測試成功</h2> <c:forEach var="row" items="${rs.rows}"> Foo ${row.id}<br/> Bar ${row.num}<br/> </c:forEach>
</body> </html> 5.輸入http://localhost:8080/hasek/test.jsp看到測試成功字樣就說明你配置ok了 |