Introduction to Servlets

Keeping in tune with the OCEJWCD 6 exam and its objectives, we will start a series of posts that will explain the concepts of the exam. This first post will discuss the basic concepts related to servlets and its life cycle.

What are the basic terminologies for the exam?

Client and Server:

A “client” is any user who makes certain requests to the server. The requests may be simple examples as fetching a document or modifying a shopping cart.

A “server” is a device or a program that returns the resources requested by the “client”. Most of the Internet runs on the client-server architecture.

Static pages and Dynamic pages:

“Static pages” are those that are returned “as is” to the client. An example may be fetching a document ‘xyz.html’ from the server.  

“Dynamic pages” on the other hand are more interactive and user friendly. Pages that change according to user preferences, is one example of a dynamic page. The content on dynamic web sites, are not stale and are constantly updated.

 Web components and Web applications:

Servlets and JSPs are considered to be web components.  A web application is a combination of web components and static pages.

Servlets, CGI and Web container:

A “servlet” is a small program that runs inside a server. A servlet is responsible for doing most of the processing that has to be done by the application. A servlet is hence used to create dynamic and interactive pages. In the days prior to servlets, CGI was used to create dynamic pages.  CGI scripts were slow and less secure than servlets. This was one of the reasons that they gave way to servlets in due course of time.

Web applications (servlets, JSPs and static pages) are managed by a web container. The primary function of a web container is to manage interactions between the different components in a web application and to manage the servlet life cycle.

Having seen the important definitions related to the Web component developer exam, let us move onto a discussion of servlets.

What are the important Servlets classes and interfaces?

In order to create servlets and work with them it is necessary to extend the ‘GenericServlet’ abstract class. If we have to work with Http Servlets specifically, it is necessary to extend the ‘HttpServlet’ abstract class. It should be noted that ‘Servlet’ itself is an interface.

It should also be borne in mind that the resource requested by a client is the “request” and the reply from the server is the “response”.

This brings us to two other important objects for the exam – the ‘HttpServletRequest’ interface and ‘HttpServletResponse’ interface.  The ‘HttpServletRequest’ and ‘HttpServletResponse’ interfaces are related to the HTTP servlets.

req-res

This is the hierarchy of the ‘HttpServletRequest’ and ‘HttpServletResponse’ interface:

servlet-class

All servlets must override one of the following methods (which is defined in the HttpServlet class)

doGet()

doPost()

doPut()

doDelete()

 

There are three other doXXX() methods as well – they are doOptions(), doTrace() and doHead()

Servlet life cycle:

All servlets are characterized by the init(), service() and destroy() methods. Let us briefly see the servlet life cycle:

  1. The init() method is where the initialization of the servlet takes place. It takes place only once in a servlet life cycle. The init() method may be overridden.
  2. The servlet spends most of its time in the service() method. The service() method must not be overridden. The service() method in turn calls one of the doXXX() methods mentioned above.
  3. The destroy() method is called once the response is generated and sent back to the client. The destroy() method is again called only once and the final clean up of the servlet occurs here.

We have seen a brief introduction of the web component developer exam terminologies, the servlet interface and a crisp understanding of the servlet life cycle. We will understand more of servlets in the next post.

Preparing for OCEJWCD 6 Certification? Pass in 1st attempt through Whizlabs OCEJWCD 6 Course! Start with Free Trial!

 

About Aditi Malhotra

Aditi Malhotra is the Content Marketing Manager at Whizlabs. Having a Master in Journalism and Mass Communication, she helps businesses stop playing around with Content Marketing and start seeing tangible ROI. A writer by day and a reader by night, she is a fine blend of both reality and fantasy. Apart from her professional commitments, she is also endearing to publish a book authored by her very soon.

3 thoughts on “Introduction to Servlets”

Leave a Comment

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

Scroll to Top