apache tomcat

เขียนโดย dsin ที่ 12:01 ก่อนเที่ยง

1. download apache tomcat
ผมโหลด ตัว Core : tar.gz นะ

2. extract to folder
3. $ bin/startup.sh
แล้วจะเข้าเวปได้จาก http://localhost:8080
5. เข้า tomcat manager
6. มันจะถาม username, password
ซึ่งจริงๆต้อง ไปใส่ username, password ในไฟล์ /conf/tomcat-users.xml ก่อน

ตามนี้
เปิดuser username="dsin" password="somepassword" roles="manager,admin"/ปิดวงเล็บ

แล้ว restart tomcat ทีนึง

>> ถ้า start webapp บางตัวไม่ติด ให้ไปดูไฟล์ log/catalina.out หรือ $ tail -f log/catalina.out ก็ได้

>> ถ้าอยากเปลี่ยน port ที่รัน tomcat แก้ใน conf/server.xml

วิธี deploy webapp ( เขียน app ลง tomcat นั่นแหละ )

1. ลง ant ก่อน apt-get install ant

Ant จะอ่าน build conf จาก build.xml
ดูตัวอย่างไฟล์ build
เราอาจเปลี่ยน project name="My Project"
ถ้าเปลี่ยน port tomcat ต้องแก้ property name="manager.url" value="http://localhost:8088/manager"/> เป็น port ใหม่ด้วย

( ถ้ามี config พวก specific ที่ต่างๆกันในแต่ละ development environment
ให้ไปเขียนไว้ใน build.properties
ซึ่งมันจะ override build.xml อีกที )

2. ทำให้ ant รู้จัก apache tomcat
copy ไฟล์ lib/catalina-ant.jar ไปลงที่ ant library directory
ของผมอยู่ที่ /usr/share/ant/lib ( เราสามารถดูจาก snaptic ได้ )

3.
สร้างโฟสเดอร์ app ไว้ที่ไหนก็ได้บนเครื่อง เช่น [project dir]

- ใส่ไฟล์ build.xml ไว้ที่ top level ของ project directory
แล้วใส่ build.properties override ตามนี้ ไว้ที่เดียวกัน

# Context path to install this application on
app.path=/hello

# Tomcat 6 installation directory
catalina.home=/home/dsin/apache-tomcat-6.0.18

# username password ของ Tomcat manager ที่ตั้งไว้ใน conf/tomcat-users.xml
manager.username=dsin
manager.password=somepassword

4. ไปที่ top level
$ ant install
ถ้าสำเร็จ tomcat จะสร้างไฟล์ไว้ที่ $CATALINA HOME/webapps/ โดยอัตโนมัติ

ถ้าลงแล้ว fail อยากลงใหม่ ใช้ $ ant reload

ถ้าอยาก remove $ ant remove # uninstall from /webapps/

5. สร้างคลาสใน src
เช่น
[project dir]/src/mypackage/Hello.java
NOTE : สร้าง dir ชื่อแพคเกจก่อน ในที่นี้คือ mypackage

เอาคลาสใน src ไปสร้าง war file ด้วย
$ ant dist

6. ต่อไปพวก static ไฟล์ทั้งหลาย
เอาไฟล์ hello.jsp, index.html ไว้ที่ [project dir]/src/web
tomcat.gif ไว้ใน [project dir]/src/web/images

ใส่ไฟล์ web.xml ไว้ที่ [project dir]/web/WEB-INF

บอก tomcat ว่า จะให้ แพคเกจนี้ไปขึ้นที่ URI ไหน โดยเพิ่มโค้ดในไฟล์ web.xml

7. ถ้าแก้ code ให้สั่ง
$ ant reload

ถ้าอยาก remove สั่ง
$ ant remove

NOTE : ก่อนรันต้อง start tomcat ก่อนด้วยวุ้ย แปลกดี

reference
hello
โฟลเดอร์ที่แนะนำให้สร้างในโฟลเดอร์ app ของเรา
docs/

src/
.java to generate Servlets, JavaBeans
ส่วนใหญ่ คลาส ของเวปจะถูกเขียนเป็น packages ดังนั้น โฟลเดอร์ src นี้ก็จะมี โฟลเดอร์ย่อยๆ เป็น โฟลเดอร์ของ package อยู่

web/

นี่คือ document-root เลยแหละ
เอาไว้ใส่พวกไฟล์ static ทั้งหลาย (.jsp , .html, .js , .css, image files)

web/WEB-INF/
เก็บไฟล์ config, application server ไม่อนุญาตให้เข้าถึง directory นี้ผ่านทางเวปได้

• web.xml:
• tag : library descriptor files
• classes/ server-side classes: servlets, utility classes, and JavaBeans components.
If your classes are organized into packages, the package structure should be replicated as directories under classes/
• tags/: contain tag files implementing tag libraries
• lib/: contain JAR archives of any libraries called by the server-side classes

web/META-INF/context.xml
Tomcat-specific configuration, e.g., for logging, data sources, etc.

และระหว่าง development แนะนำให้สร้างสองไดเรกทอรี่ข้างล่างนี้ด้วย
build/
: the image to be archived and deployed to the container.
dist/
: the binary image to be created for distribution.

build and dist directories should not be archived in source code control. Likewise, it is recom-
mended that any libraries you need, for example JDBC drivers, should not be stored in source code control. They should be copied in at build time with ant.

0 ความคิดเห็น: