Sunday, June 18, 2017

Servlet Interface

The Servlet interface specified s contract between the servlet container and the servlet.  Servlet container calls the init method of the servlet one time during start up.  When a request comes in, servlet engine calls the service method of the servlet to process it.

Servlet engine will also pass in a ServletConfig object to the servlet application's init method.  ServletConfig contains initialization parameters. ServletConfig also provide a SrvletContext object. The ServleCcontext contains global data accessible by all classes in the servlet. In a distributed environment, each servlet instance can access the same ServletContext data.

Servlet also defines two objects - ServletRequest and ServletResponse that used by service method.  ServletRequest defines methods to retrieve parameters submitted in a request.  For example, "id" is the parameter which the method returns as string value.

http:::.../servlet?id=1

ServletResponse simplifies the returning of response to the client. It defines methods to let the application to write the response to a certain stream and let servlet container to handle the rest.

To implement a serlvet, one create a class implement the Servlet interface or extend a class that implemented the Servlet interface.

No comments: