http://zhidao.baidu.com/question/60394105.html
something need to improve:
how to use eclipse to create a servlet?
what's a servlet's basic code (a template)
refered to the link:
http://blog.csdn.net/wangzeyou0415/archive/2006/10/08/1326339.aspx
How do a servlet work?
please check the servlet's working procession via the attached file: Servlet work Flow.jpg
![](https://p-blog.csdn.net/images/p_blog_csdn_net/dianfeng138/EntryImages/20081112/Servlet%20work%20Flow.jpg)
那WEBAPP的Classes,lib下面的類改變后,就会在一小段时间后自动重新被裝載
initial a servlet:
control the time servlet initial once the tomcat start
<servlet>
<servlet-name>invoker</servlet-name>
<servlet-class>
org.apache.catalina.servlets.InvokerServlet
</servlet-class>
<init-param>
<param-name>debug</param-name>
<param-value>0</param-value>
</init-param>
<load-on-startup>2</load-on-startup>
// the more little, the earlier tomcat will load this servlet, once it less than or equals 0, it mean nothing, so the tomcat will judge itself when
should load it
//數字越小越早加載,依賴性越小.負數跟0無意義,加載順序由Web服務器判斷
</servlet>
control a servlet's initial parameters:
<servlet>
<servlet-name></servlet-name>
<servlet-class></servlet-class>
<init-param>
<param-name> ParameterName1 </param-name>
<param-value>ParameterValue1</param-value>
</init-param>
<init-param>
<param-name> ParameterName2 </param-name>
<param-value>ParameterValue2</param-value>
</init-param>
</servlet>
通过Servlet接口的init(ServletConfig)傳入的物件來取得參數,
一些相关类:
ServletContext
à每個WebApp都有一個上下文
ServletConfig
à被GenericServlet類實現
getServletContext()
getInitParameterNames()
Default servlet: org.apache.catalina.servlets.DefaultServlet (define in %tomcat%/conf/web.xml)
凡是在web.xml文件中找不到匹配的Serlvet映射URL時,他們的請求都交由預設servlet來處理,即缺省Servlet用於處理所以有其他Servlet都不處理的訪問請求.就算訪問一個HTML文件
<servlet-mapping>
<servlet-name>default</servlet-name>
<url-pattern>/</url-pattern> //用這個來標示缺省的
</servlet-mapping>
//所有的靜態資源(靜態HTML,File,Img)等等都由這個默認的Servlet來響應請求.
//所有的靜態資源(靜態HTML,File,Img)等等都由這個默認的Servlet來響應請求.
(若某个context被删除后不久又自动重新出现) if a context auto-create after it was deleted, It just because:
there are some define content in the server.xml file, just search with the context's path to search it.
web服务器内幕:
set enviroment varities for tomcat itself: (just define them in these two files)
setclasspath.bat setclasspath.sh
How do Tomcat start?
check the attached file: Tomcat Start Procession.jpg
System類裝載器通過bin/setclasspath.bat來設置CLASSPATH,可以通過修改該批處理文件來添加新的CLASSPATH
Common類所裝載的類對於Tomcat跟WEbApp都是可見的.負責加載common/lib下的所有jar包
Catalina類所裝載的類只對於Tomcat可見.負責加載server/lib跟server/classes的Jar包
Shared負責加載shared/lib 跟 shared/classes下面的jar包
WebAppX類裝載器負責加載當前應用程序的/WEB-INF/classes中的類文件
,還有/WEB-INF/lib中的jar包.用到的第三方jar包都應該放到lib下面,或是把自己的類打包圍jar放到lib下
SUN公司的Servlet的類裝載器建議是子裝載器能裝就用子裝載器裝,不然在搜尋父曾,跟原來的JDK標準剛好相反..但是TOMCAT沒有遵守SUN的Servlet建議,還是跟JDK標準一樣~~~
![](https://p-blog.csdn.net/images/p_blog_csdn_net/dianfeng138/EntryImages/20081112/Tomcat%20Start%20Procession.jpg)