JAVAWEB學習——JSP九大內置對象

request :java

  服務器端接受客戶端以http方式傳過來的數據。
  經常使用方法:+getParameter(String args):String ,+getParameterValues():String[], +getParameterNames():Enumeration;web

  request其實是服務器接受客戶端請求的信息的信息以後作進一步處理的。數組

  
一、解決亂碼的問題  瀏覽器

hello:<%=request.getParameter("name")%>

  這麼直接寫出現亂碼的問題:hello:鏁�安全

這個時候就要使用服務器

public void setCharacterEncoding(String env)
          throws UnsupportedEncodingException來設置贊成的編碼格式。cookie

:這裏設置的請求編碼還要跟你JSP中編碼格式同樣纔不會出現亂碼。session

<%
	request.setCharacterEncoding("UTF-8");
%>
		hello:<%=request.getParameter("name")%>

  結果:hello:敢jsp

二、傳遞參數:編碼

獲取表單的一個參數:+getParameter(String args):String(隱藏的表單表單域)

  String name = request.getParameter("username");

獲取表單的多個參數:+getParameterNames():Enumeration;

  Enumeration<String> enume = getParameterNames();

獲取傳遞內容是數組:+getParameterValues():String[](用於下拉列表、複選框等等)

  String [] param = getParameterValues();

總結:
只要是客戶端信息,在服務器端均可以經過 request 對象取得

response:服務器想客戶端發送的消息,http的頭信息,Cookie等等

主要使用:

  重定向response.sendDirection(loc):response.sendRedirect("firstCookie.jsp");
  設置響應頭: 使用這個來刷新頁面,兩秒後跳轉等等。
  設置cookies:response.addCookie(cookie);

總結:服務器想客戶端發送的消息經過response去設置。

session:http協議是無狀態的,他不知道每一次請求的瀏覽器是否是同一個瀏覽器。

  使用:保存用戶的各類信息,直到他的生命週期超時or被釋放。

session對象屬於javax.servlet.http.HttpSession 接口的實例化對象。

session的主要方法:

  方法:session.getId()  獲取一個id,這個id由服務器分配。

     public boolean isNew() 是否是新的session

      設置屬性:session.setsetAttribute(String name ,string value); request.getSession().setAttribute("token", tokenValue);

      取得屬性: public Object getAttribute(String name)   request.getSession().getAttribute("message")

     刪除屬性:public Object removeAttribute(String name)  session.removeAttribute("message");

session失效:

  若是 session 失效,則在session 所保留的所有操做也會消失
  public void invalidate():使session 失效(手工)

session的其餘方法能夠查看servlet API。

  session跟cookie的區別:

  session存在於服務器端、cookie存在客戶端。

  session比cookie跟安全,可是比cookie更加浪費空間、資源。

  session 要儘可能少使用—— 儘可能少向session 中保存信息
  session 使用了cookie 的機制,若是cookie 被禁用,則session 也沒法使用

Application:同session同樣也是用來保存消息的,可是它保存的消息是全部人都能共享的,而session的私有的。(在線人數)

   只要做用:保存公共消息

   屬性操做的三個方法:
    setAttribute()、getAttribute()、removeAttribute()

out:向JSP頁面中動態的輸出一些內容。

  our.print("hello:"+name);可是這個應該較少使用,可使用hello:<%=name  %>來代替。

config:處理web.xml中的配置信息。

 config的方法能夠經過查看API去實現。javax.servlet.ServletConfig

取得初始化參數的方法:public String getInitParameter(String name)

取得所有參數:public Enumeration getInitParameterNames();

其餘較少使用的不在說明;

相關文章
相關標籤/搜索