6. Servlet Introduction and Servlet API

 Servlet Introduction

Servlet is a java program that runs inside JVM on the web server. It is used for developing dynamic web applications.


Features of Servlet

- Platform Independence: Servlets are written in Java, making them platform-independent and capable of running on any server that supports the Java platform.

- Server-side Execution: Servlets execute on the server, allowing them to handle business logic, database operations, and other server-side tasks.

- Multithreading Support: Servlets can handle multiple requests simultaneously through the use of multithreading, improving efficiency and responsiveness.

- Security: Servlets benefit from Java's built-in security features, including the ability to define and enforce access controls.

- Portable and Reusable: Servlets can be developed and deployed independently of the underlying server, promoting code reusability and portability across different environments.


Way to create Servlet

1. By extending HttpServlet class

2. By extending GenericServlet class


Note:
we should always prefer the first way of creating servlet i.e. by extending HttpServlet class.

Servlet API

We need to use Servlet API to create servlets. 

There are two packages that you must remember while using API, the javax.servlet package that contains the classes to support generic servlet (protocol-independent servlet) and the javax.servlet.http package that contains classes to support http servlet.


java.lang.Object

|_extended byjavax.servlet.GenericServlet

        |_extended byjavax.servlet.http.HttpServlet


1.Generic Servlet

- Creating a Generic Servlet then you must extend javax.servlet.GenericServlet class. GenericServlet class has an abstract service() method. Which means the subclass of GenericServlet should always override the service() method.

Signature of service() method:


public abstract void service(ServletRequest request, ServletResponse response) throws ServletException, java.io.IOException { }


2. HTTP Servlet

- creating Http Servlet you must extend javax.servlet.http.HttpServlet class, which is an abstract class. Unlike Generic Servlet, the HTTP Servlet doesn’t override the service() method. Instead it overrides one or more of the following methods. 


doGet() – This method is called by servlet service method to handle the HTTP GET request from client. The Get method is used for getting information from the server

doPost() – Used for posting information to the Server

doPut() – This method is similar to doPost method but unlike doPost method where we send information to the server, this method sends file to the server, this is similar to the FTP operation from client to server

doDelete() – allows a client to delete a document, webpage or information from the server

init() and destroy() – Used for managing resources that are held for the life of the servlet

getServletInfo() – Returns information about the servlet, such as author, version, and copyright.


Comments

Popular posts from this blog

2. JSP Scripting elements & Directives

1. JSP Introduction And Life-Cycle

3. JSP Action Tag and Expression Language