servlet簡介

簡述

servlet是動態網頁的一種技術,用java語言實現。在tomcat中,servlet的建立是有tomcat實現。html

Servlet對象介紹:

servlet接口中有5個函數:java

  1. void init(ServletConfig config):系統的初始化,在瀏覽器第一次訪問servlet對象的時候有tomcat的容器管理器建立。建立之後就初始化Servlet對象,配置參數信息與初始化。ServletConfig對象是管理器給的。
  2. ServletConfig getServletConfig():調用配置信息,這個通常是service函數中調用。
  3. void service(ServletRequest req, ServletResponse res):處理請求,每次網頁有請求的時候就調用一次這個函數,注意每個網頁請求是不一樣的線程,servlet對象是線程不安全的
  4. String getServletInfo():serlvet的配置信息,這個對servlet對象的描述,至關於toString()函數。
  5. void destroy():管理器調用的函數,收回內存。


其餘信息:init、service、destroy是生命週期函數。init destroy是調用一次。而service是調用屢次。web

servlet的路徑配置

servlet其實就是應用中的一個小程序,一個應用部署中能夠由多個servlet。根據url的應用信息能夠找到對應的web應用。servlet的路徑須要在web的文件中進行指定。多個url能夠指定一個name。或者使用通配符。
url路徑匹配:小程序

  • 路徑匹配: /abc/*
  • 後綴匹配: *.do
  • 匹配全部: /*
  • *不能放到中間。
<servlet>
    <name></name>
    <servlet-class></servlet-class>
</servlet>
<servlet-mapping>
    <name></name>
    <url-pattern></url-pattern>
</servlet-mapping>


複製代碼

*********注意servlet的url路徑前要有/表示相對地址。若是沒有/部署會報錯。瀏覽器

servletConfig類

主要實現是管理容器傳給servlet一些信息。初始化參數與servletContext,serverlet標籤在配置文件中的寫法以下圖:name是惟一標識,class表明真是的servlet類。裏面包含一些initparameter配置信息。緩存

servletConfig函數

  1. String getServletName() servlet標籤的名字。
  2. ServletContext getServletContext():servlet的環境信息,這個很重要包含應用的私有信息與公共信息。下一章節介紹。
  3. String getInitParameter(String name):初始化參數的name獲得值。
  4. Enumeration getInitParameterNames():獲得初始化參數name迭代器。

genericservlet類

繼承了servlet與configservlet接口。而且實現log的方法,這個方法主要方便咱們編寫servlet接口的函數。tomcat

httpservlelt類

基於http協議繼承了servlet類。將service()函數分散到http中的各類方法中 相關函數介紹:安全

  • HttpServlet()構造函數,donothing。通常經過反射調用類,構造函數都是無參的。
  • protected void doGet(HttpServletRequest req, HttpServletResponse resp)針對http的get方法,servlet的處理過程,通常get方法都是相對安全的,每次請求是相同的。
  • protected long getLastModified(HttpServletRequest req)這個函數是get函數調用,在http使用緩存的時候,請求報文中有if-Modified-Since,servlet對比這個時間,若是cookie的是新的,就返回304方法。
  • protected void doHead(HttpServletRequest req, HttpServletResponse resp)head請求方法是服務器只返回包的頭部。這個是針對head請求報文的處理
  • protected void doPost(HttpServletRequest req, HttpServletResponse resp)針對http中post報文的處理方法,會修改服務信息的內容,通常是不安全的。
  • protected void doPut(HttpServletRequest req,HttpServletResponse resp)http中上傳文件報文,處理對應的方法
  • protected void doDelete(HttpServletRequest req,HttpServletResponse resp)刪除文件的方法
  • protected void doOptions(HttpServletRequest req,HttpServletResponse resp)查詢服務器支持的方法,通常不用覆蓋
  • protected void doTrace(HttpServletRequest req, HttpServletResponse resp)原樣返回收到內容。用於調試。通常不用覆蓋
  • protected void service(HttpServletRequest req, HttpServletResponse resp)http的協議處理service函數,把功能分給以上的doXXX函數;這個函數是被下面的函數調用。不能夠覆蓋
  • public void service(ServletRequest req, ServletResponse res)調用上面的service函數,不能夠覆蓋

ServletContext

簡介

servletContext對應一個應用,包含一個servlet容器的相關信息。這個類是很重要的,也稱爲一個數據域(一個應用的servlet能夠相互傳遞信息)bash

函數介紹

  1. String getContextPath()返回uri中的應用地址,以/開頭。若是是默認根context,返回一個空字符串。注意這個方法可能與HttpServletRequest.getContextPath()返回的不一樣,但context對應幾個不一樣的context path的時候。後面再分析HttpServletRequest的函數。
System.out.println(context1.getContextPath());//  return  /web3  。uri對象的應用地址
複製代碼
  1. ServletContext getContext(String uripath):經過context path地址返回相應的context對象。這個函數基本沒有什麼用處,若是一個servlet調用其餘servlet的相對路徑,會返回null。
ServletContext  context2=context1.getContext("/web2");
       System.out.println(context2.getContextPath());//拋出異常,null指針
        ServletContext  context3=context1.getContext("/web3");
        System.out.println(context3.getContextPath());//  /web3  沒有任何用處。
複製代碼
  1. int getMajorVersion();int getMinorVersion();int getEffectiveMajorVersion();int getEffectiveMinorVersion()前兩個函數返回servlet容器支持的版本號,下面兩個返回serlvet的真實版本號。
  2. String getMimeType(String file):返回MIME的類型。MIME的類型是對互聯網文件的標識,存放在/config/web.xml中
System.out.println(context1.getMimeType("abc.text"));  //text/plain
        System.out.println(context1.getMimeType("abc.jpg"));   //image/jpeg
複製代碼
  1. Set getResourcePaths(String path):返回文件夾路徑下對應的資源路徑。子文件夾以/結束。
For example, for a web application containing:

   /welcome.html
   /catalog/index.html
   /catalog/products.html
   /catalog/offers/books.html
   /catalog/offers/music.html
   /customer/login.jsp
   /WEB-INF/web.xml
   /WEB-INF/classes/com.acme.OrderServlet.class
   /WEB-INF/lib/catalog.jar!/META-INF/resources/catalog/moreOffers/books.html
 
getResourcePaths("/") would return {"/welcome.html", "/catalog/", "/customer/", "/WEB-INF/"}, and getResourcePaths("/catalog/") would return {"/catalog/index.html", "/catalog/products.html", "/catalog/offers/", "/catalog/moreOffers/"}.
複製代碼
  1. URL getResource(String path):這個函數是調取資源的URL地址,若是以/開頭是相對地址,相對與context的地址。若是是其餘地址默認調用url的函數處理
System.out.println(context1.getResource("a.txt")); 
//print:file:/D:/java_temp/plug1/out/artifacts/web3_war_exploded/a.txt
複製代碼
  1. InputStream getResourceAsStream(String path)在上一步的繼承上打開資源,變爲輸入流。
  2. RequestDispatcher getRequestDispatcher(String path)返回一個資源調度字,內容指向path的資源,path以/開始,
  3. RequestDispatcher getNamedDispatcher(String name):同上,知識name表明servlet的name。
  4. void log(String msg):void log(String message,Throwable throwable)。log內容的寫入
  5. String getRealPath(String path):打開資源相對與操做系統的地址。
  6. String getServerInfo():返回servlet container的信息。

  7. String getInitParameter(String name)
    Enumeration getInitParameterNames()
    boolean setInitParameter(String name, String value)
    這三個方法是關於servletContext初始化參數的信息。初始化參數不能被覆蓋,serinitparameter覆蓋的時候返回false,而且初始化參數不能被設置,當應用啓動的時候;初始化的標籤爲:
<context-param>
        <param-name>name1</param-name>
        <param-value>value1</param-value>
    </context-param>

複製代碼

  1. Object getAttribute(String name)
    Enumeration getAttributeNames()
    void setAttribute(String name, Object object)
    void removeAttribute(String name)
    屬性參數,能夠覆蓋。進而完成servlet之間的數據通訊
  2. String getServletContextName()返回servletContext的name。通常在配置文件的標籤中存在。沒有返回null


ServletRegistration.Dynamic addServlet(String servletName, String className)
ServletRegistration.Dynamic addServlet(String servletName, Servlet servlet)
ServletRegistration.Dynamic addServlet(String servletName, Class<? extends Servlet> servletClass)把servlet增長到context裏面,至關於註冊。服務器

  1. T createServlet(Class clazz) throws ServletException建立servlet的類 18.
    ServletRegistration getServletRegistration(String servletName)
    Map<String,? extends ServletRegistration> getServletRegistrations()返回註冊的servlet。


  2. FilterRegistration.Dynamic addFilter(String filterName, String className)
    FilterRegistration.Dynamic addFilter(String filterName,Filter filter)
    FilterRegistration.Dynamic addFilter(String filterName, Class<? extends Filter> filterClass)
    T createFilter(Class clazz)
    FilterRegistration getFilterRegistration(String filterName)
    Map<String,? extends FilterRegistration> getFilterRegistrations() 同上servlet 20.其餘的都是關於listener、session、classloader、declareRoles

網頁的相對地址與絕地地址

web的工程以下圖,在hello.html增長超連接的網頁,其中第一個不能進行相對地址到servlet程序:相對地址爲http://localhost:8080/bservlet。相對地址通常採用三張方式:

  1. 絕地地址
  2. 相對當前站點的地址。以/開始
  3. 相對當面目錄的地址,開頭不能以/開始

<a  href="/bservlet" target="_blank">bservlet</a>
	<a  href="bservlet" target="_blank">bservlet</a>
	<a  href="/web3/bservlet" target="_blank">bservlet</a>
	<a  href="http://localhost:8080/web3/bservlet" target="_blank">bservlet</a>
複製代碼

tomcat 安裝環境變量設置

tomcat也是有java程序寫的,運行tomcat須要設置幾個一些變量讓程序的使用。

  1. Set CATALINA_HOME (required) and CATALINA_BASE (optional) home是必須設置的,告訴腳本文件tomcat的位置;base可選,只有在多用戶的系統中須要啓動多個tomcat的時候設置相應的配置文件用
  2. Set JRE_HOME or JAVA_HOME (required)設置 JAVA_HOME必須的,由於tomcat須要java虛擬機運行。而jre是可選的,jvm中包含jre。
相關文章
相關標籤/搜索