天天看點

servlet 學習筆記(二)

---------------------第二講---------------------------------

開發servlet有三種方法:

  1.實作servlet接口(最原始的)

  實作接口的5個方法,具體在下面的代碼中展現

  2.繼承GenericServlet(較少使用)

       通過GenericServlet去開發servlet,隻需要重寫service方法,相對來說要簡單一些。

  3.繼承HttpServlet(普遍使用的)

      通過HttpServlet去開發servlet,需要重寫doGet、doPost方法,這是目前用的最多的一種方法。

表單送出資料get請求和post請求的差別:

1.從安全性看get<post。get送出的資料會在浏覽器的位址欄顯示

2.從送出的内容大小看get<post。get送出的資料不能大于2K,而post送出的資料理論上不受限制,但是實際程式設計中建議不要大于64K

3.從請求響應速度看get>post。get請求伺服器立即處理請求,而post請求可能形成一個隊列請求

-------------------------------------------------------------------------------------------------------------

servlet的生命周期

servlet部署在容器裡(我們使用的是Tomcat,也可是别的,比如jboss,weblogic。。。),它的生命周期由容器來管理。

servlet的生命周期分為以下幾個階段:

  1.裝載servlet,由相應的容器來完成

  2.建立一個servlet執行個體

  3.調用servlet的init()方法,該方法隻會在第一次通路servlet時被調用一次

  4.服務:調用servlet的service()方法,一般業務邏輯在這裡處理,該方法在通路該servlet時,會被調用

  5.銷毀:調用servlet的destroy()方法,銷毀該servlet執行個體,該方法在以下情況被調用:

  a)tomcat重新啟動

  b)reload該webapps

  c)重新啟動電腦

-------------------------------------------------------------------------------------------------------------

servlet開發流程:

Step1. 在tomcat的webapps目錄下建立myWebSite檔案夾,作為網站的根目錄;myWebSite檔案夾下建立WEB-INF檔案夾;WEB-INF檔案夾下建立classes和lib檔案夾以及web.xml檔案;

Step2. 開發servlet(引入Tomcat檔案夾下lib檔案夾裡的servlet-api.jar),具體過程如下:

在JCreatorb編輯器中建立檔案名為:Hello的java file,然後選擇Cogfigure—Options—JDKProfiles—New(在左側)—選擇jdk的路徑後确定—編輯—Classes—Add—AddArchive—選擇安裝後tomcat目錄下lib檔案夾裡面的servlet-api.jar後确定即可。

Step4. 實作servlet的接口,具體過程如下:

選擇tools—implement interfaces—javax—servlet—Servlet确定即可。

Step5. 在實作的接口裡面填寫相應的語句,具體代碼如下:

Step6. 部署你的servlet(servlet開發流程)

  在web.xml檔案裡面進行配置相關資訊,具體代碼如下:

1 <?xml version="1.0" encoding="ISO-8859-1"?>
 2 <!--
 3  Licensed to the Apache Software Foundation (ASF) under one or more
 4   contributor license agreements.  See the NOTICE file distributed with
 5   this work for additional information regarding copyright ownership.
 6   The ASF licenses this file to You under the Apache License, Version 2.0
 7   (the "License"); you may not use this file except in compliance with
 8   the License.  You may obtain a copy of the License at
 9 
10       http://www.apache.org/licenses/LICENSE-2.0
11 
12   Unless required by applicable law or agreed to in writing, software
13   distributed under the License is distributed on an "AS IS" BASIS,
14   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   See the License for the specific language governing permissions and
16   limitations under the License.
17 -->
18 
19 <web-app xmlns="http://java.sun.com/xml/ns/javaee"
20    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
21    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
22    version="2.5">
23 
24   <display-name>Welcome to Tomcat</display-name>
25   <description>
26      Welcome to Tomcat
27   </description>
28 
29 <!—給servlet取名,可以随意取名-->
30 <servlet-name>hello</servlet-name>
31 <!—指明servlet的路徑,就是servlet的包+類名-->
32 <servlet-class>com.tsinghua.Hello</servlet-class>
33 </servlet>
34 <servlet-mapping>
35 <!—給servlet取名,可以随意取名-->
36 <servlet-name>hello</servlet-name>
37 <!—浏覽器中輸入的url,可以随意取名-->
38 <url-pattern>/sp<url-pattern>
39 </servlet-mapping>
40 
41 
42 </web-app>      

web.xml

Step4.啟動Tomcat,通路你的servlet,在浏覽器的位址欄中輸入:http://127.0.0.1:8080/myWebSite/hello回車就可以看到servlet的輸出

注意:127.0.0.1是伺服器所在的IP,8080是端口号,要根據實際情況定。

另外,繼承GenericServlet和繼承HttpServlet在源代碼中具體展現。源代碼下載下傳點我