iT邦幫忙

2024 iThome 鐵人賽

DAY 4
0
Software Development

從Servlet到Spring MVC系列 第 4

Day03 Basic - Integrate Tomcat in Intellij IDEA

  • 分享至 

  • xImage
  •  

前言

前一天我們介紹了Java Web的標準目錄結構,我們只要把這個符合標準的目錄打包放到Tomcat的webapps下,啟動Tomcat我們就可以看到我們佈署的應用程式了,整體流程為「開發->構建->佈署」
https://ithelp.ithome.com.tw/upload/images/20240918/20128084TStOXkcah8.png
為了開發的方便我們就來介紹Tomcat如何與我們常用的Intellij整合。

Intellij Tomcat設定

進入IDEA頁面點選Customize
https://ithelp.ithome.com.tw/upload/images/20240918/20128084zUhPpAADeG.png
點選Application Server,點選加號
https://ithelp.ithome.com.tw/upload/images/20240918/20128084CgX1Yw6ZG8.png
點選Tomcat Server
https://ithelp.ithome.com.tw/upload/images/20240918/20128084KdEMB3EL6U.png
選擇Tomcat解壓路徑
https://ithelp.ithome.com.tw/upload/images/20240918/20128084JefIozgbXg.png
選擇好後按OK
https://ithelp.ithome.com.tw/upload/images/20240918/201280847FsQmnfJRF.png
它會載入jsp與servlet api jar
https://ithelp.ithome.com.tw/upload/images/20240918/20128084k8mWTaD1b0.png

Intellij創建Web專案

為了後面鐵人賽方便我們先建立一個Empty project
https://ithelp.ithome.com.tw/upload/images/20240918/20128084bM4QbQj8IT.png
輸入project name
https://ithelp.ithome.com.tw/upload/images/20240918/20128084m0Yl9rs7Cw.png
設定專案結構
https://ithelp.ithome.com.tw/upload/images/20240918/20128084zow0h2z6sG.png
選擇JDK17
https://ithelp.ithome.com.tw/upload/images/20240918/20128084wOuyly1KgR.png
創建module
https://ithelp.ithome.com.tw/upload/images/20240918/20128084b9lwais4lc.png
輸入模組名稱,選擇JDK 17,設定完成後按下Create
https://ithelp.ithome.com.tw/upload/images/20240918/20128084fc5pHnlRfG.png
創建完成後選取Project Structure->module>day03->加號
https://ithelp.ithome.com.tw/upload/images/20240918/20128084w3A5mhLrVg.png
加入tomcat的dependancy
https://ithelp.ithome.com.tw/upload/images/20240918/20128084S0q5R9W3MY.png
day03右鍵module setting
https://ithelp.ithome.com.tw/upload/images/20240918/20128084XEBncR2JBU.png
Facets加入Web
https://ithelp.ithome.com.tw/upload/images/20240918/20128084X5Og4zVrb1.png
創建Artifact
https://ithelp.ithome.com.tw/upload/images/20240918/20128084WiOmrxrTLB.png
會多出web資料夾
https://ithelp.ithome.com.tw/upload/images/20240918/20128084mo0Oms8NZm.png
創建resource、lib、static資料夾
https://ithelp.ithome.com.tw/upload/images/20240918/20128084LIG0XA5Ivd.png
加入一些空檔案
https://ithelp.ithome.com.tw/upload/images/20240918/20128084nXO1RyHzsd.png
將resources標記為資源資料夾
https://ithelp.ithome.com.tw/upload/images/20240918/20128084sUpbrE3XNr.png
將lib標記為Library,且名稱只能是lib
https://ithelp.ithome.com.tw/upload/images/20240918/20128084qlBw10kqjr.png
選取Module lib
https://ithelp.ithome.com.tw/upload/images/20240918/20128084lMDd2R8TlA.png
啟動專案設定
https://ithelp.ithome.com.tw/upload/images/20240918/20128084bovaTTkv17.png
點選加號並選擇tomcat
https://ithelp.ithome.com.tw/upload/images/20240918/20128084f6CW2OXq4V.png
佈署專案
https://ithelp.ithome.com.tw/upload/images/20240918/20128084t2QVruaoOe.png
修改訪問路徑,按下確定
https://ithelp.ithome.com.tw/upload/images/20240918/20128084mKYzFj99cX.png
啟動tomcat
https://ithelp.ithome.com.tw/upload/images/20240918/20128084QC5Gh1fpg2.png
瀏覽器訪問WebApp
https://ithelp.ithome.com.tw/upload/images/20240918/20128084gTb0nsopeu.png

Tomcat與Intellij IDEA整合原理

Intellij整合了Tomcat並啟動專案,它並不會在你的Tomcat/webapps下把你的專案放入,這一點我們可以從啟動log看出端倪
https://ithelp.ithome.com.tw/upload/images/20240918/20128084sSVTAdhM0k.png
實際上它是放在紅色框框的路徑下,裡面只放一些佈署相關的配置文件
https://ithelp.ithome.com.tw/upload/images/20240918/20128084HNyKyMTuUC.png
透過設定檔告訴tomcat Webapp瀏覽器訪問路徑
https://ithelp.ithome.com.tw/upload/images/20240918/201280849FOUKadNr4.png

build前與build後資料夾對應

  • web -> day03_Web_exploded
  • resources、src -> classes
    https://ithelp.ithome.com.tw/upload/images/20240918/20128084oasVP1pk2D.png

打包War檔

File -> Project Structure -> Artifacts -> 加入Web Application Archive
https://ithelp.ithome.com.tw/upload/images/20240918/20128084Ebd8Tf9XMW.png
修改名稱為WebApp
https://ithelp.ithome.com.tw/upload/images/20240918/20128084Bap9Xy5uAm.png
Build -> Artifacts -> Build
https://ithelp.ithome.com.tw/upload/images/20240918/20128084PJDEAVJlBu.png
out資料夾產出War檔
https://ithelp.ithome.com.tw/upload/images/20240918/20128084ylqsOWTH6O.png

deploy到Tomcat

https://ithelp.ithome.com.tw/upload/images/20240918/20128084ogFXOAHVVn.png
修改檔名啟動tomcat與訪問localhost
https://ithelp.ithome.com.tw/upload/images/20240918/20128084H0Q4qXSpVI.png

Reference


上一篇
Day02 Basic - Application Server
下一篇
Day04 Servlet - Hello Servlet
系列文
從Servlet到Spring MVC36
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言