Mc0078 Assignment Answer

Bigg Boss

Ans:

   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.

2. Invokes the service method, passing a request and response object.

If the container needs to remove the servlet, it finalizes the servlet by calling the servlet's destroy method.

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.

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.

CODE:

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");

   }

}

 

K-2

Appendix K Answers to Checkpoints

1.12 The original value is overwritten.1.13 A compiler

is a program that translates source code into an executable form.1.14 Syntax

errors

are mistakes that the programmer has made that violate the rulesof the programming language. These errors must be corrected before the com-piler can translate the source code.1.15 The Java compiler translates Java source code into byte code, which is an inter-mediate language. The Java Virtual Machine executes the byte code instructions.1.16 The Java Virtual Machine (JVM) is a program that reads Java byte code instruc-tions and executes them as they are read. In other words, it interprets byte codeinstructions. You can think of the JVM as a program that simulates a computerwhose machine language is Java byte code.1.17 The program’s purpose, input, process, and output.1.18 Before you create a program on the computer, it is often a good idea to imaginewhat the computer screen will look like while the program is running. If it helps,draw pictures of the screen, with sample input and output, at various points inthe program.1.19 A cross between human language and a programming language. Pseudocode isespecially helpful when designing an algorithm. Although the computer can’tunderstand pseudocode, programmers often find it helpful to write an algorithmin a language that’s “almost” a programming language, but still very similar tonatural language.1.20 A compiler

translates source code into an executable form.1.21 A runtime error is an error that occurs while the program is running. These areusually logical errors, such as mathematical mistakes.1.22 Syntax errors are found by the compiler.1.23 You can provide sample data and predict what the output should be. If the pro-gram does not produce the correct output, a logical error is present in the pro-gram.1.24 Data and the code that operates on the data.1.25 The data contained in an object.1.26 The procedures, or behaviors, that an object performs.1.27 Encapsulation refers to the combining of data and code into a single object.1.28 Data hiding refers to an object’s ability to hide its data from code that is outsidethe object. Only the object’s methods may then directly access and make changesto the object’s data. An object typically hides its data, but allows outside code toaccess the methods that operate on the data.

0 thoughts on “Mc0078 Assignment Answer

Leave a Reply

Your email address will not be published. Required fields are marked *