You are on page 1of 5

Web Technology

Question 1. Explain the servlet API Life Cycle methods in brief

Three methods are central to the life cycle of a servlet. These are init(),service() and
destroy().They are implemented by every servlet and are invoked at a specific time by the
server.

What are the phases of Servlet life cycle?


The servlet life cycle contains five phases:

1) load a servlet class -

The class Classloader is responsible to load servlet class.ClassLoader object is designed to


load a class just once. It is loaded, when the first request for the servlet is received by the web
container.

2) Servlet instance is created -

At the time code for a servlet is loaded, the server creates a single instance. That single
instance handles every request made of the servlet. The servlet object is created once in the
entire servlet life cycle.

3) init() method -

init() method is called after creating the servlet instance. It is called only once in the entire
lifecycle. It is used to initialize the servlet.Init() is guaranteed to be called and completed
before the servlet handles its first request. During init() method a servlet may want to read its
initialization (init) parameter.

4) service() method -

Service() method is called every time when a request for a servlet is received.
Service() method is used to handle requests as appropriate for the servlet.The service()
method accepts 2 parameters: a request object and a response object.
It overrides doGet() and doPost() method.
doGet() method — doGet() method is used to handle the get request.

doPost() method — doPost() method is used to handle the Post request.

Servlet Life Cycle

5) Destroy() method -

This method is called only once in the entire life cycle of a servlet. The servlet calls the
destroy() method after the servlet has been taken out of service and all pending requests have
completed or timed out.
Question 2 : Discuss the basic differences between Servlet and JSP
Question 3: Explain in Detail the creation, instantiation and usage of
java beans objects

A JavaBean is a specially constructed Java class written in the Java and


coded according to the JavaBeans API specifications.
Following are the unique characteristics that distinguish a JavaBean from
other Java classes −
 It provides a default, no-argument constructor.
 It should be serializable and that which can implement
the Serializable interface.
 It may have a number of properties which can be read or written.
 It may have a number of "getter" and "setter" methods for the
properties.

JavaBeans Properties
 A JavaBean property is a named attribute that can be accessed by
the user of the object. The attribute can be of any Java data type,
including the classes that you define.
 A JavaBean property may be read, write, read only, or write only.
JavaBean properties are accessed through two methods in the
JavaBean's implementation class −

S.No. Method & Description

getPropertyName()
1
For example, if property name is firstName, your method name
would be getFirstName() to read that property. This method is
called accessor.

setPropertyName()

2 For example, if property name is firstName, your method name


would be setFirstName() to write that property. This method is
called mutator.

 A read-only attribute will have only a getPropertyName() method,


and a write-only attribute will have only
a setPropertyName() method.

You might also like