Tomcat作為一個(gè)很優(yōu)秀的web application server的確值得大家使用,特別是熟悉java的愛好者,不過它的配置并不象它的應(yīng)用那么優(yōu)秀,至少在配置上有很多瑣碎的事情要做,現(xiàn)在我將我自己配置最新的tomcat5.5.15的一點(diǎn)心得告訴大家:
總共涉及三個(gè)方面,也是tomcat配置比較煩瑣的地方
一.與apache的配置(暫略,晚點(diǎn)再將配置詳細(xì)介紹)
二.使tomcat5.5.15支持jstl標(biāo)簽 這個(gè)是最簡(jiǎn)單的配置,只要按照以下方法,就可以配置成功. 1.下載 jakarta-taglibs-standard-1.1.2.zip (windows) jakarta-taglibs-standard-1.1.2.tar.gz(linux) 2.解壓在解壓的lib目錄下找到j(luò)stl.jar,standard.jar這兩個(gè).jar文件,將他們復(fù)制到 你的站點(diǎn)/WEB-INF/lib文件夾下,在tld目錄下將所有的.tld文件復(fù)制到 你的站點(diǎn)/WEB-INF/taglib/文件下 3.配置 你的站點(diǎn)/WEB-INF/web.xml文件,在</webapp>前添加如下內(nèi)容 <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,呵呵,所以大家如果按照這樣設(shè)置的話,在jsp中引用jstl標(biāo)簽是記得采用這樣寫<%@ taglib uri=http://www.hnlinux.net/core prefix="c" %>,可見這里配置是很靈活的隨便你取什么名字,只要在寫jsp的時(shí)候相對(duì)應(yīng)就可以)
現(xiàn)在你就可以使用jstl來書寫你的jsp了.
三.連接mysql數(shù)據(jù)庫(kù)的配置 1.下載 mysql-connector-java-5.0.0-beta.zip 將mysql-connector-java-5.0.0-beta-bin.jar復(fù)制到$Tomcat_home$/common/lib文件夾,(如果你不想使用數(shù)據(jù)連接池的話,可以放在 你的站點(diǎn)/WEB-INF/lib文件夾下) 2.搞定了,你現(xiàn)在就可以使用連接數(shù)據(jù)庫(kù)的的相關(guān)語句了(這個(gè)是最簡(jiǎn)單的,呵呵)
四.mysql連接池的配置. 1.下載commons-dbcp-1.2.1.zip 解壓將commons-dbcp-1.2.1.jar復(fù)制到$Tomcat_home$/common/lib下(保證mysql的jdbc的驅(qū)動(dòng)也在此目錄) 2.修改$Tomcat_home$/cof/server.xml在context標(biāo)簽內(nèi)添加以下內(nèi)容,如果沒有找到context標(biāo)簽,注意在下面加上紅色的部分,蘭色部分根據(jù)自己情況修改,不用解釋了吧? <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> 注意一點(diǎn)的事情是:<Context path="/hasek" docBase="hasek"... 中/hasek代表的是 你的站點(diǎn) <Resource name="hasek" 這個(gè)hasek代表的是你的連接池的名字,這個(gè)要和下面的web.xml里的內(nèi)容相一致. url="jdbc:mysql://localhost:3306/hasek?autoReconnect=true"/>中的hasek代表你的數(shù)據(jù)庫(kù)名字
3.修改 你的站點(diǎn)/WEB-INF/web.xml 在</webapp>標(biāo)簽前添加以下內(nèi)容:
<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.測(cè)試代碼(連接池與jstl標(biāo)簽測(cè)試)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>測(cè)試成功</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看到測(cè)試成功字樣就說明你配置ok了 |