NOTE:Jsp and Servlet

來源:互聯網
上載者:User

2007-4-13
____________________________________________________________________________________________
1.web伺服器用於處理HTTP請求,而應用伺服器則用於提供和web應用相關的服務
2.Servlet是利用HttpServletRequest類的getParameter()方法來取得網頁傳來的資料.不過
  資料通過HTTP協議傳輸時會被轉碼,因此在潤滑劑地,必須再做轉碼的工作,才能正確地接收到
資料.

2007-4-16
____________________________________________________________________________________________
1.關於response.sendRedirect(String str)

2.關於response.setHeader(String str1,String str2)

3.session對象不像其他的隱含對象,可以在任何的JSP網頁中使用,如果在JSP網頁中,
page指令的屬性session設為false時,使session對象就會產生編譯錯誤.

4.關於pageContext
預設情況下,pageContext的範圍是其所在網頁,但是它的功能遠不止此.
一方面它能夠讀取和設定四種範圍不同的對象的屬性(通過在它的setAttribute()和getAtrribute()
方法的中添加一個int型參數,來指定其讀取和設定的屬性所處的域)
另一方面它還具有取得當前頁面中session/request/response/application隱含對象的能力!

2007-4-17
____________________________________________________________________________________________
1.關於http的請求與響應的

http作用一種非常簡單的通訊模型:即請求與響應模式.
用戶端需要擷取資源或申請服務時,它會向伺服器發送一個請求,然後伺服器接受請求,給出一個響應.
在http中請求與響應都有固定的形式:

對於一個請求它主要包含一個請求行和一個要求標頭標和一個請求體.如:

GET /index.html HTTP/1.1 /*請求行*/

Host: www.gefionsoftware.com /*要求標頭標*/
User-Agent: Mozilla/5.0 (Windows; U; Win 9x 4.90; en-US; rv: 1.0.2)
Accept: image/gif, image/jpeg, image/pjpeg, image/png, */*
Accept-Language : en
Accept-Charset : iso-8859-1,*,utf-8

對於一個響應,它主要包含一個響應行/一個回應標頭標/和一個可選的響應體(例如一段html代碼)

HTTP/1.1 200 OK //響應行,其中200為響應代碼,它標示了響應的狀態,

Last-Modified: Mon, 20 Dec 2002 23:26:42 GMT //以下為回應標頭標
Date: Tue, 11 Jan 2003 20:52:40 GMT
Status: 200
Content-Type: text/html
Servlet-Engine: Tomcat Web Server/5.0
Content-Length: 59
   
<html> //以下為響應體
  <body>
    <h1>Hello World!</h1>
  </body>
</html>

2.關於請求的參數的傳遞方法
在一個請求中,有用的請求資訊就是請求參數,它是一種map,由鍵-值對構成.
這些參數的傳遞方法有兩種:GET和POST.
對於這兩種方法的不同之處在於:
1) GET方法將參數轉換為查詢字串,把它們加在指定的URL之後,中間有用問題串連,然後傳送至
指定的程式做處理.
2) POST方法將參數做為請求體的一部分傳遞出去.在URL中看不到相關的資訊.
一般來說get方法用於傳送資訊量較少的請求(少於255個字元)
而POST而用於傳送資訊量很大的請求.另外在傳遞使用者名稱稱和密碼時,為防止它們出現在URL
中,應使用POST方法.

3.所有的控制項必須放在<form>.....</form>之中.

2007-4-19
_____________________________________________________________________________________________________________________
1.關於jsp指令.(page/include/taglib)

a directive doesn't directly affect the content of the response sent to the browser, but it tells the container how it should handle the page

一個jsp指令並不會影響送至瀏覽器的響應的具體內容,但是它會告訴容器應該如何處理這個頁面

 

2009-8-10

 

 

1.關於Servlet的生命同期

The servlet is normally
created when a user first invokes a URL corresponding to the servlet, but you
can also specify that the servlet be loaded when the server is first started。

Servlet預設是消極式載入的,即只有請求第一次委派到這個Servlet上時,Servlet容器才會建立這個Servlet的執行個體,然後調用init方法進行初始化。此後再有請求到達時,容器會新起一個線程或是從線程池中分配一個閑置線程,在該線程中進行service方法,而service方法的主要工作就是查看HTTP請求,找出是由哪種方法提交的請求(比如:GET,POST等等),然後調用相應的doXXX方法處理請求。

以下servlet生命週期:

 

2.關於Servlet的類階層

 

3.關於HttpServletRequest和HttpServletResponse

 

Q: Who implements the interfaces for HttpServletRequest and HttpServletResponse? Are those classes in the API?
A: The Container, and No. The classes aren’t in the API because they’re left to the vendor to implement. The good news is, you don’t have to worry about it. Just trust that when the service() method is called in your servlet, it’ll be handed references to two perfectly good objects that implement HttpServletRequest and HttpServletResponse. You should never care about the actual implementation class name or type. All you care about is that you’ll get something that has all the functionality from HttpServletRequest and HttpServletResponse.
In other words, all you need to know are the methods you can call on the objects the Container gives you as part of the request! The actual class in which they’re implemented doesn’t matter to you—you’re referring to the request and response objects only by the interface type.

 

記住:HttpServletRequest和HttpServletResponse都是介面,平台API中沒有任何這兩個介面的實作類別,它們的實作類別都是由Servlet容器定義和建立的,對於開發人員來說,並不需要瞭解這些,只要使用介面就OK了。

 

4.為什麼會有GenericServlet 、ServletRequest 、 ServletResponse?而不是只保留http打頭的那些對應類呢?

Q: I’m still confused about why there’s a GenericServlet and ServletRequest and ServletResponse. If nobody’s doing anything except HTTP servlets... then what’s the point?
A: We didn’t say nobody. Somebody, somewhere, one could imagine, is using the servlet technology model without the HTTP protocol. Just nobody we’ve met personally or read about. Ever.
Still, the flexibility was designed into the servlet model for those who might want to use servlets with, say, SMTP or perhaps a proprietary custom protocol. The only support built-in to the API, though, is for HTTP, and that’s what virtually everyone’s using.

原因在於:原則上講Servlet技術並不只是單單同http協議工作的,理論上講Servlet能和應用程式層的各種協議工作,比如SMTP,FTP,Talnet等等。這就是設計GenericServlet 、ServletRequest 、 ServletResponse的用意,可以由他們擴充出像FTPServlet,FTPRequest這樣的類來,使得Servlet可以和其他應用程式層協議工作。

 

 

 

5.關於Servlet生命週期和Request的重點:

 


相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.