Discuss different methods used in life cycle of servlet.

A servlet is a small java program that executes on the server-side of web connection and dynamically extends the functionality of a web server. Servlet technology is used to create Dynamic web application.

In the life cycle of servlet there are three important methods. These methods are: init(), service() and destroy().



The client enters the URL in the web browser and makes a request. The browser then generates the HTTP request for this URL and sends it to the web server. This HTTP request is received by the web server. The web server maps this request to the corresponding servlet.

init():

- The server invokes the init() method of the servlet. This method is invoked only when the servlet is loaded in the memory for the first time. It is possible to pass initialization parameters to the servlet so it may configure itself.


service():

- The service() method is the main method to perform the actual task.

- The web server calls the service() method to handle requests coming from the client/browsers and to write the response back to the client (to process the HTTP rerquest). The service() method is called for each HTTP request.


destroy():

- Finally server unloads the servlet from the memory using the destroy() method to clean any resources.

- The destroy() method is called only once at the end of the life cycle of a servlet.

                      OR,

A Java servlet is a Java programming language class that is used to extend the capabilities of servers that host applications accessed by means of a request-response programming model.

The javax.servlet and javax.servlet.http packages provide interfaces and classes for writing servlets. To run a servlet, a web container must be used. One of the best known open source web container for servlet is Tomcat.


All servlets implement the Servlet interface, which defines its life-cycle methods. A servlet life cycle can be defined as the entire process from its creation till the destruction. The following are the paths followed by a servlet during its life cycle:

a. The servlet is initialized by calling the init() method.

b. The servlet calls service() method to process a client’s request.

c. The servlet is terminated by calling the destroy() method.

d. Finally, the servlet is garbage collected by the garbage collector of the JVM.


The different methods used in the life cycle of servlet are:

(a) The init() method:

The init​() method is designed to be called only once during the life cycle of a servlet. It is called when the servlet is first created, and not called again for each user request.

The servlet is normally created when a user first invokes a URL corresponding to the servlet. When a user invokes a servlet, a single servlet instance of the servlet gets created and is initialized using the server’s configuration. After the init() method is completed (i.e. servlet has been initialized if it has not been initialized before), the service() method is called. The init() method looks like this:

public void init(ServletConfig config) throws ServletException


(b) The service() method:

The service() method is the main method to perform the actual task. The servlet container (i.e. web server) calls the service() method to handle requests coming from the client (browsers) and to write the formatted response back to the client. Each time the server receives a request for a servlet, the server spawns a new thread and calls service(). The service() method checks the HTTP request type (GET, POST, etc) and calls doGet(), doPost(), etc methods as appropriate. The signature of the service() method is:

public void service(ServletRequest req, ServletResponse res) throws ServletException,

IOException

The doGet() or doPost() methods are invoked by the service() method and thus they should be defined by us depending on what type of request is received from the client. If it is a normal request for a URL or a request from an HTML form that has no METHOD specified, then doGet() method is called by service(). If it is a request from an HTML form that specifically lists POST as the METHOD, then doPost() method is called by service(). Their signatures are:


protected void doGet(HttpServletRequest request, HttpServletResponse response)

throws ServletException, IOException

protected void doPost(HttpServletRequest request, HttpServletResponse response)

throws ServletException, IOException


(c) The destroy() method:

The destroy() method is called only once at the end of the life cycle of a servlet. This method gives your servlet a chance to close database connections, halt background threads, and perform other such cleanup activities. Its signature is: public void destroy()

After the destroy() method is called, the servlet object is marked for garbage collection.

Comments

Popular posts from this blog

Suppose that a data warehouse for Big-University consists of the following four dimensions: student, course, semester, and instructor, and two measures count and avg_grade. When at the lowest conceptual level (e.g., for a given student, course, semester, and instructor combination), the avg_grade measure stores the actual course grade of the student. At higher conceptual levels, avg_grade stores the average grade for the given combination. a) Draw a snowflake schema diagram for the data warehouse. b) Starting with the base cuboid [student, course, semester, instructor], what specific OLAP operations (e.g., roll-up from semester to year) should one perform in order to list the average grade of CS courses for each BigUniversity student. c) If each dimension has five levels (including all), such as “student < major < status < university < all”, how many cuboids will this cube contain (including the base and apex cuboids)?

Suppose that a data warehouse consists of the three dimensions time, doctor, and patient, and the two measures count and charge, where a charge is the fee that a doctor charges a patient for a visit. a) Draw a schema diagram for the above data warehouse using one of the schemas. [star, snowflake, fact constellation] b) Starting with the base cuboid [day, doctor, patient], what specific OLAP operations should be performed in order to list the total fee collected by each doctor in 2004? c) To obtain the same list, write an SQL query assuming the data are stored in a relational database with the schema fee (day, month, year, doctor, hospital, patient, count, charge)

Suppose that a data warehouse consists of the four dimensions; date, spectator, location, and game, and the two measures, count and charge, where charge is the fee that a spectator pays when watching a game on a given date. Spectators may be students, adults, or seniors, with each category having its own charge rate. a) Draw a star schema diagram for the data b) Starting with the base cuboid [date; spectator; location; game], what specific OLAP operations should perform in order to list the total charge paid by student spectators at GM Place in 2004?