Servlet API - Smart Tech Guides

Latest

Hi this is Sravan Kumar from India. SMART TECH GUIDES is a technical blog. it helps you to learn about Java topics, frameworks and tools like Jenkins, GitHub & related explanations, data base related topics. All these Explanations are explained in simple and understandable manner.

Subscribe Us

SMART TECH GUIDES

Servlet API

Servlet API

Servlet technology is used to develop web application.

Servlet is an API, that contains 4 packages and documentations.

 

Servlet API packages are

1) javax.servlet

2) javax.servlet.annotation

3) javax.servlet.descriptor

4) javax.servlet.http

 

All these packages contain classes and methods.

Servlet container processing Servlet components and its methods, so we are overriding this component methods and writes business logic inside these methods.

When Java class implementing Servlet Interface, we can override these methods. 


Java class implements Servlet Interface Directly/ Indirectly that Java class is called as servlet component.

 

Note: Servlet Interface available inside Servlet API, so we should download servlet API, and must add to our Java project build path.

   

Servlet API contains, 3 major classes/interfaces, 1)Servlet(Interface), 2)GenericServlet, 3)HttpServlet.

these 3 classes having methods for Request/Response processing, Initializing /destroying objects, and configurations. this 3 classes available under below 2 packages.

1) javax.servlet

2) javax.servlet.http

servlet api


Servlet(Interface) 

under javax.servlet package Servlet interface available.

this Servlet Interface having 5 methods. with the help of these methods we are processing our requests, object creation, destroying object and configurations all these process possible.


Servlet Interface methods

1) destroy()

2) getServletConfig()

3) getServletInfo()

4) init()

5) service()


GenericServlet 

under javax.servlet package GenericServlet class available.

it is an abstraction class, and it implements above servlet Interface, so we are utilizing above 5 features also.

This GenericServlet abstraction class also contains it own methods, and one abstraction method i.e., service() method. 



HttpServlet

under javax.servlet.http package HttpServlet Class available.

it is an abstraction class it extends properties of above GenericServlet class. so, we can utilize properties of above GenericServlet class and Servlet Interface features also.

HttpServlet class doesn’t contain any abstraction methods.


HttpServlet class methods(doXXX() methods) are:

1) doGet(request, response);

2) doPost(request, response);

3) doHead(request, response);

4) doTrace(request, response);

5) doOptions(request, response);

6) doPut(request, response);

7) doDelete(request, response); 

and also 2 service methods

8) service(ServletRequest req, ServletResponse res)

9) service(HttpServletRequest req, HttpServletResponse res)

 

While developing servlet component we are using service() method for processing our requests/responses, and also we can use doXXX() methods.

if we use single service() method, we cant differentiate processing logics, whether it is getting data, posting data, deleting data or updating data etc.. so to differentiate processing request, in place of service() method, we are using 7 doXXX() methods.

No comments:

Post a Comment