打算這個星期把SERVLET和JSP復習下,我認為基礎對一個人很重要,所以會經常回過頭來看看.
1.HTTP協議
每個做web應用都應該熟悉HTTP協議吧,如果不熟悉大家找些資料去學學.
GET: 參數直接作為一個query string放到URL后面傳遞的.可以是text
POST:參數是作為封裝一個請求信息體里的.可以是text和binary
servlet里有多個方法用來對應處理http協議的請求方法
doGet() : link,和直接在瀏覽器中輸入URL
doPost():? 通過form表單顯示聲明method="post",否則默認為get
doDelete(): 針對要刪除服務器的某些資源的請求
doTrace(): 調試服務器連接的http方式
doOptions() : The OPTIONS request determines which HTTP methods the server supports and returns an appropriate header. For example, if a servlet overrides doGet, this method returns the following header:
Allow: GET, HEAD, TRACE, OPTIONS
doPut() : 針對要向服務器放入新的文件的請求
doHead(): 針對只要response的Header信息的請求
當client來一個請求時,doService(HttpServletRequest request, HttpServletResponse response) throws ServletException ,IOException接受這個,并選擇相應的請求選擇相應的方法進行處理
2.HttpServletResquest介紹
常用的方法:String getParameter(String name)
Enumeration getParameterNames()
String[] getParameterValues(String name)
String?? getHeader(String name)
int getIntHeader(String name)
long getDateHeader(String name)
Enumeration getHeaderNames()
String[] getHeaderValus(String name)
Cookies[] getCookies()
3.HttpServletResponse介紹
void setContentType(String type)
setHeader(String name, String value)
setIntHeader(String name, int value)
setDateHeader(String name, long value)
addHeader(String name, String name)
addIntHeader(String name, int value)
addDateHeader(String name ,long value)
boolean containsHeader(String name)
PrintWriter getWriter()? //返回字符流
OutputStream getOutputStream()? //返回字節流
void sendRedirect(String URL) //重定向到某個頁面
void sendError(int sc) //發生錯誤向客戶端發送狀態碼
void sendError(int sc, String message)
void addCookie(Cookie cookie) //增加一個cookie
Cookie(String name, String value) //? cookie的構造方法
4.SERVLET的生命周期
servlet loading? ---> servlet instantiation ---> call init() ---> call doService handle request ---> call destroy()
public void init(ServletConfig config) throws ServletException
如果override這個方法必須先調用super.init(config)
public void init() throws ServletException
public void service() throws ServletException, IOException
public void destroy() throws ServletException