1. 程式人生 > >與servlet相關的接口

與servlet相關的接口

names p s post方法 cif 不依賴 ring odin ext cte

(二)與servlet相關的接口

從servlet僅有的5個方法當中,我們知道其涉及3個接口,分別是:

ServletConfig

ServletRequest

ServletResponse

2.1. ServletConfig

主要方法:

技術分享

重點關註getServletContext,之前說servletConfig是容器向servlet傳遞參數的載體,那麽它也可以讓Servlet獲取其在容器中的上下文。

ServletContext是針對一個web應用,jdk中具體描述——

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.)

2.2.ServletRequest

獲取客戶端發來的請求數據。(查看)

note:註意getAttribute和getParameter的區別。

getAttribute( String name )可以得到由setAttribute()設置的參數值,相當於是使用getAttribute()得到一

個自己定義的參數,而不是從客戶端得到的參數。

getParameter( String name )它用來獲取客戶端通過get或post方法等傳遞過來的值,是從客戶端傳遞過來的,

一般指的是客戶端提交的表單組件的值。

note:setCharacterEncoding在什麽時候使用才有效?

它可以覆蓋請求正文中所使用的字符編碼,但是它必須在讀取parameters之前設置,否則無效。

2.3.ServletResponse

響應客戶端請求。(查看)

(三)GenericServlet抽象類

為了簡化serlvet的編寫,在javax.servlet包中提供了一個抽象類GenericServlet,它給出了除service()方法以外的簡單實現。

GenericServlet定義了一個通用的,不依賴具體協議的Servlet,它實現了Servlet接口和ServletConfig接口。

public abstract class GenericServlet implements Servlet, ServletConfig, java.io.Serializable

(四)HttpServlet抽象類

HttpServlet主要是應用於HTTP協議的請求和響應,為了快速開發HTTP協議的serlvet,sun提供了一個繼承自GenericServlet的抽象類HttpServlet,

用於創建適合Web站點的HTTP Servlet。

public abstract class HttpServlet extends GenericServlet implements java.io.Serializable

重點關註HttpServlet中的一個私有方法service。

與servlet相關的接口