2011年5月23日

JSP request session application 分析



JSP request session application 分析

Request, Session, Application

JSP網頁中我們有三個常用的對象
而這三個對象分別對應到了Serverlet中的三個類別,分別是ServerletRequestHttpSessionServerletContext
request
javax.servlet
Interface ServletRequest
All Known Subinterfaces:
All Known Implementing Classes:


public interface ServletRequest
Defines an object to provide client request information to a servlet. The servlet container creates a ServletRequest object and passes it as an argument to the servlet's service method.
A ServletRequest object provides data including parameter name and values, attributes, and an input stream. Interfaces that extend ServletRequest can provide additional protocol-specific data (for example, HTTP data is provided by HttpServletRequest.
session
javax.servlet.http
Interface HttpSession


public interface HttpSession
Provides a way to identify a user across more than one page request or visit to a Web site and to store information about that user.
The servlet container uses this interface to create a session between an HTTP client and an HTTP server. The session persists for a specified time period, across more than one connection or page request from the user. A session usually corresponds to one user, who may visit a site many times. The server can maintain a session in many ways such as using cookies or rewriting URLs.
application
javax.servlet
Interface ServletContext


public interface ServletContext
Defines a set of methods that a servlet uses to communicate with its servlet container, for example, to get the MIME type of a file, dispatch requests, or write to a log file.
There is one context per "web application" per Java Virtual Machine. (A "web application" is a collection of servlets and content installed under a specific subset of the server's URL namespace such as /catalog and possibly installed via a .war file.)
In the case of a web application marked "distributed" in its deployment descriptor, there will be one context instance for each virtual machine. In this situation, the context cannot be used as a location to share global information (because the information won't be truly global). Use an external resource like a database instead.
The ServletContext object is contained within the ServletConfig object, which the Web server provides the servlet when the servlet is initialized.

然而這三個對象使用的時機是不同的,我們可以透過API文件中發現他們皆有get Attribute()方法以及對應的set方法可以使用,以下來說明他們之間的差異點。
Request(範圍:獨立的request對象中)
請求對象被製造出來是客戶端透過超連結或是表單傳送需求時被產生的,因此每一筆新的Request伺服器都會產生一個新的request對象,所以假設A請求123,接下來B請求321時,request對象將會重新被產生,因此只會剩下最後的請求321,如何將他們都留下來呢,方法就是使用request對象中的setAttribute()方法,該方法可以將客戶請求存入並且轉發到下個頁面,下個頁面可以由getAttribute來取得該值進而達到頁面的轉發(forward)。
Seesion(範圍:瀏覽器)
會議對象使用時機通常是使用在一些需要登入的網頁上,可以透過session的存活判斷用戶是否登入等等資訊,這些資訊會一直存活在瀏覽器上。
Application(範圍:伺服器上):
應用對象的使用時機可以透過一個例子來實現,有時候我們想要寫一個網頁計數器,透過requestsession是無法達成的,首先request在每次請求時他都會被重新產生,session則是關閉瀏覽器時就會被銷毀,這樣皆不能達成該功能,此時我們可以透過application對象,這個對象所存下的訊息是存在伺服器端的,因此不管我們是在A電腦瀏覽還是B電腦瀏覽,該值都是一樣的,因此通常application是使用在取值而不是改值的應用上。



沒有留言:

ShareThis