Sunday, December 24, 2017

How to create servlet?

In order to define a Servlet, implementation of Servlet interface is need to be provided.
This Implementation can be provided in 2 ways:
1- Direct
2- Indirect

1- Direct


2- Indirect
In order to facilitate indirect implementation of Servlet interface, SUN Microsystems provide a helper class named javax.servlet.GenericServlet.
It is an abstract class which implements Servlet interface and defines all its method except service ().


Both approaches are not usually used because it has following limitations:
Servlet interface and its implementation in GenericServlet class are protocol independent in web application. Some protocol (HTTP, HTTPS) used by the client to interact with the web server. Each of these protocols supports different types of request.
Most commonly used protocol is HTTP which supports following 8 types of request.
Types of HTTP request:
1-  get
2-  post
3-  put
4-  delete
5-  head
6-  trace
7-  connect
8-  options

To remove the mismatch between request processing model of Servlet and HTTP protocol, HTTP specific extension of GenericServlet class is provided by SUN Microsystems.
javax.servlet.http package contains http specific interfaces and classes of Servlet API. This packae contains HttpServletRequest and HttpServletResponse interfaces which extends ServletRequest and ServletResponse interfaces respectively.


An HTTP specific helper class is defined in javax.servlet.http package by the name HttpServlet.
HttpServlet class extends GenericServlet and provides methods for handling each type of HTTP request.


Most commonly used method of this class are doGet() and doPost() these are used to process HTTP get and post request respectively.
1- public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException;
2- public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException;


No comments:

Post a Comment

Java Functional Interface

Java Lambda expression Lambda expression came to enable to use functional programming feature in java. It provides the facility t...