Head-First Servelts&JSP reading note 3

來源:互聯網
上載者:User

標籤:des   style   blog   http   java   color   

<servlet>  <servlet-name>BeerParamTests</servlet-name>  <servlet-class>TestInitParams</servlet-class>  <init-param>    <param-name>adminEmail</param-name>    <param-value>[email protected]</param-value>  </init-param></servlet>

web.xml中的片段。

這個片段針對的是TestInitParams這個servlet。

getServletConfig()能夠獲得這個servlet相關的資訊。

getServletConfig().getInitParameter("adminEmail");

能夠獲得對應的paramValue——[email protected]

 

當容器初始化一個Servlet的時候,會建立一個唯一對應的ServletConfig。

Container會從DD中讀取相應的資訊,綁定到這個ServletConfig對象上。

然後,調用Servlet類的init方法,把servletConfig對象的引用給Servlet對象。

 

ServletConfig對象的方法:

1 String getInitParameter(String);

2 Enumeration getInitParameterNames();  // Enumeration的一個實作類別是StringTokenizer。。。

3 ServletContext getServletContext();

4 String getServletName();  // nearly with no use

 

 

<servlet>  <servlet-name>BeerParamTests</servlet-name>  <servlet-class>TestInitParams</servlet-class></servlet><context-param>  <param-name>adminEmail</param-name>  <param-value>[email protected]</param-value></context-param>

web.xml中的片段。

ServletContext,屬於整個web-app,而不是某一個servlet。

this.getServletContext().getInitParameter("adminEmail");

this.getServletConfig().getServletContext().getInitParameter("adminEmail");

BOTH RIGHT!!! If the servlet extends HttpServlet or GenericServlet.

 

ServletContext對象的方法:

1 String getInitParameter(String)

2 Enumeration getInitParameterNames();

3 Object getAttribute(String);

4 getAttributeNames();

5 setAttribute(String, Object);

6 removeAttribute(String);

7 getRequestDispatcher(String);

 

 

Listener

有的時候需要在某一個或某些事件執行之前去完成一些工作,或之後完成一些工作。

這個工作可以讓每一個servlet去實現,但是顯得累贅,這個時候可以把這些工作提取出來,交給一個Java類去完成,這個java類就是一個Listener。

 

ServletContextListener

監聽ServletContext的動作的監聽器

public class SomeServletContextListener implements ServletContextListener {  public void contextInitialized(ServletContextEvent event){    ServletContext sc = event.getServletContext;
    String some = sc.getInitParameter("some");
    Dog dog = new Dog(some);
    sc.setAttribute("dog", dog);
  }  public void contextDestoryed(ServletContextEvent event){    // NOTHING TO DO HERE~  }}

 

 

設定監聽器,在web.xml中,追加

<listener>    <listener-class>xx.xx.SomeServletContextListener</listener-class></listener>

 

這樣,Container就會去指定的位置尋找監聽器類了,然後

 

Servlet類中,通過

Dog dog = (Dog) getServletContext().getAttribute(“dog”);

 

就可以獲得這個dog對象了,注意強制類型轉換。

 

 

其他類型的監聽器:

ServletContextAttributeListener:用於監聽ServletContext中的Attribute變動的。

  1. attributeAdded
  2. attributeRemoved
  3. attributeReplaced

HttpSessionListener:用於監聽Session建立和銷毀的,也就是說多少個使用者在使用該程式。

  1. sessionCreated
  2. sessionDestroyed

ServletRequestListener:用於監聽對該程式進行請求,以便對請求進行封裝或者log情報列印等。

  1. requestInitialized
  2. requestDestroyed

等等。。。

ServletRequestAttributeListener

HttpSessionBindingListener

HttpSessionAttributeListener

HttpSessionActivationListener

 

所謂監聽器,就是對某些東西進行監聽,如追加,刪除,變化,初始化和銷毀。

 

Attribute的種類:ServletContext,HttpSession,HttpServletRequest

Parameter的種類:ServletContext init Parameter,Request Parameter,ServletConfig init Parameter

 

可以對Attribute進行設定,如this.getServletContext().setAttribute("Some", new Some());

但是不能對parameter進行修改。

 

可以擷取Attribute和Parameter,但是Attribute的傳回型別是Object,而Parameter傳回型別是String,如:

Object o = request.getAttribute("Some");

String str = request.getParameter("Some");

String str = this.getServletConfig().getInitParameter("Some");

 

Attribute的存在的三個地區:

Context:ServletContext,對所有的使用者可見;

Session:HttpSession,只對當前Session可見;

Request:ServletRequest,只對當前請求可見。

 

Object getAttribute(String name)
void setAttribute(String name, Object value)
void removeAttribute(String name)
Enumeration getAttributeNames()

 

Context Scope is not thread-safe

因為application的任何一個使用者都可以建立一個servletcontext的一個attribute,當然也有修正和刪除它的權利。

所以放到servletContext中的attribute不能保證其安全性。

syncronized(this.getServletContext) 能夠保障其執行緒安全性。

 

HttpSession Scope is not thread-safe

synchronized(request.getSession());

 

HttpServletRequest Scope is thread-safe.

 

 

RequestDispatcher有兩種獲得方式,一種是通過request,一種是通過ServletContext

RequestDispatcher view = request.getRequestDispatcher(“result.jsp”);

RequestDispatcher view = getServletContext().getRequestDispatcher(“/result.jsp”);

注意:他倆是相當不同的!!!

request的方式的話,如果參數是以"/"開頭的話,它回去該項目的root目錄下去尋找該頁面,否則他會去request的相對路徑下去尋找頁面。

servletContext,只能以用“/”開頭的字串作為參數!

之後就可以用這個view.forward(request, response);了。

 

You can’t forward the request if you’ve already committed a response!

public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {  response.setContentType(“application/jar”);  ServletContext ctx = getServletContext();  InputStream is = ctx.getResourceAsStream(“bookCode.jar”);  int read = 0;  byte[] bytes = new byte[1024];  OutputStream os = response.getOutputStream();  while ((read = is.read(bytes)) != -1) {    os.write(bytes, 0, read);  }  os.flush();  // cause an illegalStateException  RequestDispatcher view = request.getRequestDispatcher(“result.jsp”);  view.forward(request, response);  os.close();}

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.