Tomcat容器入门介绍
Tomcat容器入門介紹
Tomcat環境配置
PS:JDK的安裝這里就不講了,找到安裝包直接下一步下一步就行了。
1、配置JDK
???? 在Windows10下,找到環境變量
在環境變量中添加JDK主目錄
格式為:JAVA_HOME=?指向你的jdk的主目錄(并不是bin文件目錄)
? ??在環境變量中添加路徑
格式為:path?=?%JAVA_HOME%\bin;
? ??在環境變量中添加classpath
格式為:classpath:?.;%JAVA_HOME%\lib\dt.jar;%JAVA_HOME%\lib\tools.jar;
至此,JDK就配置完了。
PS:如果在不配置JAVAHOME的前提下可以用如下方法啟動tomcat
在startup.bat的第25行中添加set?JAVA_HOME=JKD路徑
2、啟動tomcat
???? 到?tomcat?主目錄下的bin/startup.bat點擊啟動
3、驗證是否安裝成功
???? 在瀏覽器中輸入http://127.0.0.1:8080(8080是默認端口,如果該端口被占用需要到主目錄/conf/server.xml中改端口號)
出現這個界面,就表示tomcat已經配置成功了。
Tomcat配置異常及其解決
1、JAVA_HOME?配置錯誤,或者沒有配置?
? ? 這時候,可以按照上面的步驟在來一次,在命令行中輸入Java?-version,如果顯示
就表示成功了。
2、如果你的機器已經占有了8080?端口,則無法啟動
解決方法
(1)?你可以8080?先關閉
netstat?–an?
netstat?–anb?來查看誰占用該8080
(2)?主動改變tomcat的端口.
到?conf/server.xml?文件中修改
3、?能夠正常啟動,但是會導航到另外一個頁面
???? 去修改工具->管理加載項,把默認的導航給禁用即可
?
4、瀏覽器顯示404?Not?Found
???? 在訪問?tomcat時候,一定要保證?tomcat?服務器是啟動,不然就會出現這種錯誤。
Tomcat的目錄結構文件
bin:?啟動和關閉tomcat的bat文件
conf:?配置文件?
-->server.xml?:?該文件用于配置和?server?相關的信息,?比如?tomcat啟動端口后,配置Host,??配置Context?即web應用?
-->web.xml?:?該文件配置與?web應用(web應用就相當于是一個?web站點)
-->tomcat-users.xml:?該文件用戶配置tomcat?的用戶密碼?和?權限
lib?目錄:?該目錄放置運行tomcat?運行需要的jar包
logs?目錄:存放日志,?當我們需要去查看日志的時候,很有用!,當我們啟動tomcat錯誤時候,可以查詢信息.
webapps?目錄:?該目錄下,放置我們的web應用(web?站點),?比如:
建立??web1?目錄??下面放置我們的html?文件?jsp?文件..圖片...?則?web1就被當做一個web應用管理起來(??特別說明tomcat?6.0?以后支持?tomcat?5?版本?還有別的設置)
work:?工作目錄:?該目錄用于存放jsp被訪問后?生成的對應的?server文件?和.class文件
如何訪問一個web應用的某個文件
PS:想要訪問一個WEB應用中的某個文件可以采用url(Uniform?Resource?Locator)統一資源定位符來訪問,其格式如上。
Tomcat應用部署目錄結構規范
Tomcat管理虛擬目錄
? ??需求:當希望將web應用部署到非webapps目錄下時,怎么解決這問題。
PS:可以通過虛擬目錄配置技術解決。
配置步驟
???? 1、找到server.xml文件
???? 2、編輯host節點,添加Context?path
<Context?path="/應用名"?docBase="web應用所在的絕對路徑"/>
例如:要訪問該web應用根目錄下的hello.html文件
實際訪問時輸入的地址:http://localhost:8088/應用名/hello.html
? ? 3、重啟、重新部署生效
?
context的幾個屬性的說明
path:?應用名稱
docbase:??web應用所在的絕對路徑
reloadable:?如果設為ture?,表示?tomcat?會自動更新?web應用,但是這個開銷大,建議在開發過程中,可以設為true,?但是真的發布了,則應當設為false
upackWAR:?如果設為?ture?,則自動解壓,否則不自動解壓.
?
PS:war包的打包和Tomcat怎么部署war包可以使用搜索引擎找到。
配置域名
實現的步驟如下:
(1)?在C:\WINDOWS\system32\drivers\etc?下的host文件?添加127.0.0.1?www.myweb.com
(2)?在tomcat?的server.xml文件添加主機名?
<Host?name="www.myweb.com"?appBase="d:\web3”>
<Context?path="/"?docBase="d:\web3"?/>
</Host>
(3)?在d:\web3?加入了一個?/WEB-INF/web.xml?把?hello2.html設為首頁面,如果連端口都不希望帶,則可以把tomcat的啟動端口設為80即可.
(4)?重啟生效
Tomcat框架機制
Tomcat配置默認主機
在tomcat/conf/server.xml?文件
<Engine?name="Catalina"?defaultHost="主機名">
如:<Engine?name="Catalina"?defaultHost="www.myweb.com">
?
Tomcat配置線程池
Connector是可以配置共享線程池的,其作用是在訪問量多的情況下,自動增加線程數量,直到maxThread為止,訪問量趨于平緩之后漸漸減少線程數量,最少到minSpareThreads。開啟Tomcat,該線程池即有minSpareThread條線程。
[html]?view plaincopy
Executor標簽即為線程池
maxThreads屬性即為最大線程數
minSpareThreads即為最小空閑的線程數
Connector標簽即為連接器
executor屬性即為指定線程池
PS:Connector標簽可以配置,maxThread,minSpareThreads兩個屬性,其作用和配置executor屬性一樣,但同時配置了executor和maxThread,minSpareThread。Connector會優先選擇executor配置的線程池。
PS:建議使用executor屬性和Executor標簽來配置線程池,這樣不僅可設置的線程池參數更多,而且也可以實現幾個Connector共享一個線程池的配置,注意:不是共享一個線程池。
Tomcat配置訪問日志
[html]?view plaincopy
className:類名,默認的訪問日志類為org.apache.catalina.valves.AccessLogValve
directory:文件存放目錄,默認是:logs,其相對路徑是Tomcat的主目錄
prefix:前綴
suffix:后綴
fileDateFormat:按照什么時間給日志切片(分成不同日志文件,防止單一日志文件過大)。官方文檔的描述:The default value is?yyyy-MM-dd. If you wish to rotate every hour, then set this value to?yyyy-MM-dd.HH.
rotatable:該屬性是與fileDateFormat配套使用的,默認為true,如果設置為false,日志文件將不切片。
?pattern:輸出文件的模式(格式)。
有以下模式:(截取自官方文檔)
- %a?- Remote IP address
- %A?- Local IP address
- %b?- Bytes sent, excluding HTTP headers, or '-' if zero
- %B?- Bytes sent, excluding HTTP headers
- %h?- Remote host name (or IP address if?enableLookups?for the connector is false)
- %H?- Request protocol
- %l?- Remote logical username from identd (always returns '-')
- %m?- Request method (GET, POST, etc.)
- %p?- Local port on which this request was received. See also?%{xxx}p?below.
- %q?- Query string (prepended with a '?' if it exists)
- %r?- First line of the request (method and request URI)
- %s?- HTTP status code of the response
- %S?- User session ID
- %t?- Date and time, in Common Log Format
- %u?- Remote user that was authenticated (if any), else '-'
- %U?- Requested URL path
- %v?- Local server name
- %D?- Time taken to process the request, in millis
- %T?- Time taken to process the request, in seconds
- %F?- Time taken to commit the response, in millis
- %I?- Current request thread name (can compare later with stacktraces)
There is also support to write information incoming or outgoing headers, cookies, session or request attributes and special timestamp formats. It is modeled after the?Apache HTTP Server?log configuration syntax. Each of them can be used multiple times with different?xxx?keys:
- %{xxx}i?write value of incoming header with name?xxx
- %{xxx}o?write value of outgoing header with name?xxx
- %{xxx}c?write value of cookie with name?xxx
- %{xxx}r?write value of ServletRequest attribute with name?xxx
- %{xxx}s?write value of HttpSession attribute with name?xxx
- %{xxx}p?write local (server) port (xxx==local) or remote (client) port (xxx=remote)
- %{xxx}t?write timestamp at the end of the request formatted using the enhanced SimpleDateFormat pattern?xxx
手工方式打WAR包
第一步:進入到要打包的目錄下,注意,目錄結構必須符合Tomcat規范。
SwitchdeMacBook-Pro:webapps switch$ cd Restaurant/
第二步:使用jar cvf 打包后文件名 打包的文件夾
SwitchdeMacBook-Pro:Restaurant switch$ jar cvf Restaurant.war .
已添加清單
正在添加: SoybeanMilk.html(輸入 = 58) (輸出 = 48)(壓縮了 17%)
正在添加: WEB-INF/(輸入 = 0) (輸出 = 0)(存儲了 0%)
正在添加: WEB-INF/classes/(輸入 = 0) (輸出 = 0)(存儲了 0%)
正在添加: WEB-INF/classes/com/(輸入 = 0) (輸出 = 0)(存儲了 0%)
正在添加: WEB-INF/classes/com/netease/(輸入 = 0) (輸出 = 0)(存儲了 0%)
正在添加: WEB-INF/classes/com/netease/NoodlesServlet.class(輸入 = 1114) (輸出 = 608)(壓縮了 45%)
正在添加: WEB-INF/classes/com/netease/NoodlesServlet.java(輸入 = 740) (輸出 = 318)(壓縮了 57%)
正在添加: WEB-INF/web.xml(輸入 = 361) (輸出 = 173)(壓縮了 52%)
PS:可以看到打包成功了
SwitchdeMacBook-Pro:Restaurant switch$ ls
Restaurant.war?? ??? ?SoybeanMilk.html?? ?WEB-INF
第三步:將打包好的文件移動至應用目錄,默認為:Tomcat主目錄下的webapps
SwitchdeMacBook-Pro:Restaurant switch$ mv Restaurant.war ..
PS:可以看到移動到了webapps目錄下
SwitchdeMacBook-Pro:Restaurant switch$ cd ..
SwitchdeMacBook-Pro:webapps switch$ ls
ROOT?? ??? ?Restaurant?? ?Restaurant.war?? ?docs?? ??? ?examples?? ?host-manager?? ?manager
PS:這是將原來的應用刪除
SwitchdeMacBook-Pro:webapps switch$ rm -rf Restaurant
PS:原來的應用已被刪除
SwitchdeMacBook-Pro:webapps switch$ ls
ROOT?? ??? ?Restaurant.war?? ?docs?? ??? ?examples?? ?host-manager?? ?manager
PS:啟動Tomcat
SwitchdeMacBook-Pro:webapps switch$ ../bin/startup.sh?
Using CATALINA_BASE:?? /Users/switch/apache-tomcat-7.0.70
Using CATALINA_HOME:?? /Users/switch/apache-tomcat-7.0.70
Using CATALINA_TMPDIR: /Users/switch/apache-tomcat-7.0.70/temp
Using JRE_HOME:??????? /Library/Java/JavaVirtualMachines/jdk1.8.0_40.jdk/Contents/Home
Using CLASSPATH:?????? /Users/switch/apache-tomcat-7.0.70/bin/bootstrap.jar:/Users/switch/apache-tomcat-7.0.70/bin/tomcat-juli.jar
Tomcat started.
PS:訪問該應用
SwitchdeMacBook-Pro:webapps switch$ curl http://localhost:8080/Restaurant/noodles
<html><body>
<h1> Noodles with Tomcat</h1>
</body></html>
PS:可以看到webapps目錄下自動生成了該war包對應的應用
SwitchdeMacBook-Pro:webapps switch$ ls
ROOT?? ??? ?Restaurant?? ?Restaurant.war?? ?docs?? ??? ?examples?? ?host-manager?? ?manager
SwitchdeMacBook-Pro:webapps switch$?
----------參考《Apache Tomcat 7 Configuration Reference (7.0.70) - The Valve Component》
----------參考《韓順平.細說Servlet》
from:?http://blog.csdn.net/q547550831/article/details/50435038
總結
以上是生活随笔為你收集整理的Tomcat容器入门介绍的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Transact_SQL小手册(各种sq
- 下一篇: BeanUtils入门