2010年4月15日 星期四

Java Web Programming 開發環境建置

WebContainer Hosting

1. 下載Tomcat (環境為ubuntu 9.10,所以下載tar.gz)

2. 設定環境變數:
export JAVA_HOME=/usr/lib/jvm/java-1.5.0-sun
3. 執行 $CATALINA_HOME/bin/startup.sh,到這裡Tomcat就起來了(可以用browser連 http://localhost:8080,可以看到經典的湯姆貓網頁)

Web Project Configuration

1. 先將eclipse裝上wtp plugin

2. New Dynamic Web Project

3. eclipse會產生3個資料夾project所在的目錄:
 i. build:eclipes預設以此dir當作build output dir.
 ii. src: 存放source code
 iii. WebContent : 會自動產生WEB-INF, META-IN兩個資料夾, 網頁檔(jsp, html)會放在此處

4. 由於tomcat預設classes是在WEB-INF底下,所以在eclipse裏面剛new 出來的project設定property以 將output path改為WebContent/WEB-INF/classes

5. 在$CATALINA_HOME/conf/server.xml的中增加內容如下
Context path="/test" docBase="/home/funkie/data/document/eclipse_workspace/DMServer/WebContent"
debug="0" reloadable="true" crossContext="true" />
docBase: 為project 網頁的實體路徑
path: 為append在url的路徑名稱,如http://localhost:8080/test/ 就會連到 <略>/DMServer/WebContent/index.html

基本上這樣開發環境就搞定啦~~~~


Servlet

先寫個servlet來小試身手一下,在eclipse中的project按左鍵就有new servlet的選項可以選啦~~

基本上一個servlet要能work必須:
1. extends HttpServlet及implement doGet()和doPost().
2. 於WEB-INF/web.conf中declare該servlet,如下
<display-name>ServletTest</display-name>
<servlet-name>ServletTest</servlet-name>
<servlet-class> MyServlet2.ServletTest</servlet-class>
<servlet-mapping>
<servlet-name>ServletTest</servlet-name>
<url-pattern>/ServletTest</url-pattern>
</servlet-mapping>
3. compile的時候要include $CATALINA_HOME/lib/servlet-api.jar

最後用http://localhost:8080/test/ServletTest就可以連到該servlet了。其中,test為$CATALINA_HOM/conf/server.xml設定的context path,ServletTest為WEB-INF/web.conf設定的url-pattern


P.S.
  今天遇到一個白爛問題害我搞超久,由於平常都在寫android,所以一直使用jdk1.5 (因此tomcat也是使用1.5,i.e., 我有export JAVA_HOME),可是今天在web的project中用1.6 compile servlet,結果在啟動tomcat時就這樣起都起不來。

除了設定eclipse project的compiler外,還有個叫作"Project Facets"裏面的java選項也要改成1.5,
就是這東西害我今天搞超久.....囧。

1 則留言:

  1. A time seems to come in every woman’s life when she gets an uncontrollable urge to give her home a thorough cleaning. Many women still follow the old-fashioned spring and fall schedule. Spring cleaning was unavoidable many years ago when coal stoves were used all winter, coating everything with a thick, greasy dirt.

    removal company London | removal companies London

    回覆刪除