<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    kapok

    垃圾桶,嘿嘿,我藏的這么深你們還能找到啊,真牛!

      BlogJava :: 首頁 :: 新隨筆 :: 聯系 :: 聚合  :: 管理 ::
      455 隨筆 :: 0 文章 :: 76 評論 :: 0 Trackbacks

    http://java.sun.com/webservices/docs/1.0/tutorial/doc/Servlets4.html#64218

     

    Servlet Life Cycle

    The life cycle of a servlet is controlled by the container in which the servlet has been deployed. When a request is mapped to a servlet, the container performs the following steps.

    1. If an instance of the servlet does not exist, the Web container
      1. Loads the servlet class.
      2. Creates an instance of the servlet class.
      3. Initializes the servlet instance by calling the init method. Initialization is covered in Initializing a Servlet.
    2. Invokes the service method, passing a request and response object. Service methods are discussed in Writing Service Methods.

    If the container needs to remove the servlet, it finalizes the servlet by calling the servlet's destroy method. Finalization is discussed in Finalizing a Servlet.

    Handling Servlet Life Cycle Events

    You can monitor and react to events in a servlet's life cycle by defining listener objects whose methods get invoked when life cycle events occur. To use these listener objects you must define the listener class and specify the listener class.

    Defining The Listener Class

    You define a listener class as an implementation of a listener interface. Servlet Life Cycle Events lists the events that can be monitored and the corresponding interface that must be implemented. When a listener method is invoked, it is passed an event that contains information appropriate to the event. For example, the methods in the HttpSessionListener interface are passed an HttpSessionEvent, which contains an HttpSession.

    Table 12-3 Servlet Life Cycle Events 
    Object
    Event
    Listener Interface and Event Class
    Web context(See Accessing the Web Context)
    Initialization and destruction
    javax.servlet.ServletContextListener and
    ServletContextEvent
    Attribute added, removed, or replaced
    javax.servlet.ServletContextAttributeListener and
    ServletContextAttributeEvent
    Session(See Maintaining Client State)
    Creation, invalidation, and timeout
    javax.servlet.http.HttpSessionListener and
    HttpSessionEvent
    Attribute added, removed, or replaced
    javax.servlet.http.HttpSessionAttributeListener and
    HttpSessionBindingEvent

    The listeners.ContextListener class creates and removes the database helper and counter objects used in the Duke's Bookstore application. The methods retrieve the Web context object from ServletContextEvent and then store (and remove) the objects as servlet context attributes.

    import database.BookDB;
    import javax.servlet.*;
    import util.Counter;
    
    public final class ContextListener
      implements ServletContextListener {
      private ServletContext context = null;
      public void contextInitialized(ServletContextEvent event) {
        context = event.getServletContext();
        try {
          BookDB bookDB = new BookDB();
          context.setAttribute("bookDB", bookDB);
        } catch (Exception ex) {
          System.out.println(
            "Couldn't create database: " 
            + ex.getMessage());
        }
        Counter counter = new Counter();
        context.setAttribute("hitCounter", counter);
        context.log("Created hitCounter"
          + counter.getCounter());
        counter = new Counter();
        context.setAttribute("orderCounter", counter);
        context.log("Created orderCounter"
          + counter.getCounter());
      }
    
      public void contextDestroyed(ServletContextEvent event) {
        context = event.getServletContext();
        BookDB bookDB = context.getAttribute(
          "bookDB");
        bookDB.remove();
        context.removeAttribute("bookDB");
        context.removeAttribute("hitCounter");
        context.removeAttribute("orderCounter");
      }
    }
     
    

    Specifying Event Listener Classes

    To specify an event listener class, you add a listener element to the Web application deployment descriptor. Here is the listener element for the Duke's Bookstore application:

    <listener>
      <listener-class>listeners.ContextListener</listener-class>
    </listener>
     
    

    You specify a listener class for a WAR in the deploytool Event Listeners inspector (see Event Listeners).

    Handling Errors

    Any number of exceptions can occur when a servlet is executed. The Web container will generate a default page containing the message A Servlet Exception Has Occurred when an exception occurs, but you can also specify that the container should return a specific error page for a given exception. To specify such a page, you add an error-page element to the Web application deployment descriptor. These elements map the exceptions returned by the Duke's Bookstore application to errorpage.html:

    <error-page>
      <exception-type>
        exception.BookNotFoundException
      </exception-type>
      <location>/errorpage.html</location>
    </error-page>
    <error-page>
      <exception-type>
        exception.BooksNotFoundException
      </exception-type>
      <location>/errorpage.html</location>
    </error-page>
    <error-page>
      <exception-type>exception.OrderException</exception-type>
      <location>/errorpage.html</location>
    </error-page>
     
    

    You specify error pages for a WAR in the deploytool File Refs inspector (see Error Mappings).

    posted on 2005-04-14 11:56 笨笨 閱讀(400) 評論(0)  編輯  收藏 所屬分類: J2EEALLJ2SE
    主站蜘蛛池模板: 亚洲色爱图小说专区| 免费无码又黄又爽又刺激| 亚洲 综合 国产 欧洲 丝袜| 亚洲综合中文字幕无线码| 免费看污成人午夜网站| 亚洲国产日韩在线成人蜜芽| 久久亚洲精品国产精品婷婷| 99在线视频免费观看视频| 最近免费中文字幕大全| 亚洲JIZZJIZZ妇女| 国产免费午夜a无码v视频| 亚洲AV无码专区在线厂| 亚洲一级片内射网站在线观看| 一级做a爱过程免费视频高清| 日韩亚洲变态另类中文| 亚洲免费人成在线视频观看| 亚洲视频在线观看免费| 97在线观免费视频观看| 国产成人精品久久亚洲高清不卡| 国产免费一区二区三区免费视频| 亚洲午夜久久久久久久久久| 亚洲欧洲国产成人精品| 成人免费无码大片A毛片抽搐| 国产亚洲视频在线观看| 亚洲精品亚洲人成在线观看| 8888四色奇米在线观看免费看| 亚洲一卡2卡3卡4卡国产网站| 成人啪精品视频免费网站| 成人特级毛片69免费观看| 亚洲国产精品VA在线看黑人| 日韩版码免费福利视频| 免费一级毛片在线播放视频免费观看永久 | 9久热这里只有精品免费| 亚洲AV成人一区二区三区AV| 国产片AV片永久免费观看| 黄色网址免费在线| 亚洲黄色在线电影| xvideos亚洲永久网址| 久久久久免费看成人影片| 老司机午夜精品视频在线观看免费| 国产成A人亚洲精V品无码性色 |