SERVLETS BASICS

servlets basics:

What is web application?
A web application is a server side application, runs on a server and produces the web-pages on to the browser.
Web-applications are 2 types.
1.      Presentation oriented web-applications.
2.      Service oriented web-applications.

1. Presentation oriented web-applications:
Ø  Generally presentation oriented web-applications are created by using static html pages.
Ø  These applications are concentrated only on presentation or display on to the browser.

2. Service oriented web-applications:
Ø  Service oriented applications will concentrate only on providing services to the clients.
Ø  These are implemented using server-side technologies like servlet’s and jsp’s.

A web-application is a collection of both passive and active resources. We call these are web resources.
Passive resource means it do not require server to process the request. These require browser to process the request.
Example: html pages and images both are passive resources.
Servlet’s and jsp’s are active resources they need server to process the requests.

When a request is given for a resource of a web-application to the server, then 1st server receives the request and identifies whether the request is given for passive resource or active resource.
If the request is given for passive resource then server takes care about the request and provides the required page to the browser.

If the client request is given for active resource, then server will hand over the request to another application called web-container. This container will do the remaining work and provides   response back to the server. Now the server will send this response back to the browser/client.

What is a servlet?
Ø  A servlet is a server side web-component, managed by the web-container for the generation of dynamic information.
Ø  Web-container will take care about entire low level activities like identification of  required servlet, load the corresponding servlet class, instantiation, invocation of init(),service() methods.
Ø  A servlet is a platform independent java class runs at server side and increases the functionality of a server.

Sun Micro System released the servlet technology for two types of people.
1.      1.Application writers
2.      2.server writers

Application writers are called programmers or developers and server writers are called vendors.
Servlet API  has 2 packages.
1.      Javax.servlet
2.      Javax.servlet.http

For application writers i.e for programmers sun provided the servlet API.
For server writers sun provided the servlet specification.it contains set of guidelines to be followed while providing implementation for interfaces.
Different ways to create a Servlet:
v  By implementing the servlet interface.
v  By extending the GenericServlet class.(abstract class)
v  By extending HttpServlet class(abstract class)

Servlet interface:
v  It acts as a root interface for entire servlet specification.
v  Every servlet in java compulsory should implement this interface either directly or indirectly.
v  The methods in the servlet interface are
1.      init()
2.      service()
3.      destroy()
4.      getServletConfig()
5.      getServletInfo()

init():
·         This method will be executed by the web-container only once just after instantiating the servlet immediately.
·         The main purpose of this method is to perform initialization.
·         After compilation of init() method only the servlet is in a position to process client request.
Web-container won’t place the servlet into service in the following cases.

1.      The init() method throws ServletException.
2.      If init() method not completed within the time period specified by the  web-container.
In the above cases the web-container makes that servlet object eligible  for garbage collection without calling any other life-cycle methods.

Service() :

Public  void service(ServletRequest request,ServletResponse response) throws ServletException,IOException

·         This method will be executed for every client request.
·         Entire service logic we have to write in this method only.
·         If we are defining servlet by extending the GenericServlet abstract class then compulsory we should provide service() method in our servlet class.
·         If we are creating the servlet class by extending the HttpServlet abstract  class then it is not recommended to override service() method in our class. we have to provide the service logic in either doGet() or doPost() methods.

Destroy():
·         This method will be executed only once just before taking the servlet object  from the out of service.
·         The main purpose of destroy() method is for clean-up activities.
·         While executing  destroy() method if any exception has raised JVM ignores that exception.
·         Whenever web-container calls the destroy method, it will wait until all the threads in the service() method complete their execution.

Note:
from the sevice() method we can call destroy() method explicitly in that case it will be executed just like a normal method call and servlet object cannot be destroyed.

 getSevletConfig():
public ServletConfig getServletConfig();
this method returns ServletConfig object and by using that object  servlet object get its all information.
getServletInfo():
this method returns information about the servlet like  purpose, author, copy right information.etc

SERVLET LIFE-CYCLE:
·         For our servlet web-container creates the object, in order  to create the object for our class the class must be visible to the web-container so we must create the class with public modifier.
Phases in life-cycle:
1.      Not available
2.      Instantiation
3.      Initialization
4.      Service
5.      Destroy

1.Not available:
·         For our  servlet class objects will be created by the web-container.
·         Web-container is a part of a server
·         Before creating the object for a servlet ,the servlet in a position called the not available.
·         By default the container creates an object to our servlet class, whenever the first request is given to  it.
·         We can also tell to the container, create the object to our servlet class before first request come to it. i.e at the time of server start-up.
2.Instantiation & 3.Initialization:
·         Whenever the object of servlet is created then it is moved from the stage not available to instantiation.
·         After instantiated ,the web-container calls the constructor of the servlet class and then init() method.
·         By using the init() method web-container initializes the servlet object by using init() method of the servlet class.
4.Service:
·         After initialization done, the web-container calls the service() method of the servlet object for providing the services for the given client request.
·         For every request given by client  web-container calls the service() method.  
5.Destroy:
·         Whenever the server is shout-down or servlet is removed from the server, then it’s object will be destroyed.
·         In order to de initializes the resources web-container calls destroy() method.
Simple understanding of servlet lifecycle:
1.      Loading the servlet class.
2.      Servlet Instantiation.
3.      Execution of init() method.
4.      Execution of service() method.
5.      Execution of destroy() method.
All of the above 5 tasks are done by the web-container.
v  Except the service() method init() and destroy() methods are one time activities.
v  For every servlet web-container creates ServletConfig object just before calling init() method 
v  For every request web-container creates the ServletRequest and ServletResponse object s just  before invoking the service() method.
v  For every web-application web-container creates the ServletContext object at the time of application deployment.

GenericServlet:
v  GenericServlet implements the Servlet interface and provides the implementation for every method except service() method. Hence GenericServlet is declared as abstract class.
v   
v   GenericServlet implements ServletConfig, Servlet and Serializable interfaces.
v  If you create the servlet class by extending GenericServlet class then you must override the service() method.
public class abstract GenericServlet implements Servlet,ServletConfig,Serializable
{
private transient ServletConfig config;
public  void init(ServletConfig config)throws ServletException
{
this.config=config;
init();
}
Public void init() throws ServletException
{
}
Public servletConfig getServletInfo()
{
return config;
}
}
GenericServlet contains 2 init(ServletConfig) methods  one is for web-container purpose And other init() method for  programmer purpose.

HttpServlet:
HttpServlet acts as base class for developing HttpServlets.

 For every http method corresponding doxxx() method is there.
HttpServlet contains 2 service() methods.

1. public void service(ServletRequest request,ServletResponse response) throws ServletException
2. protected void service(ServletRequest request,ServletResponse response) throws                                                                                                                                                          ServletException,IOException


LifeCycle of HttpServlet:

1. when ever we sending the form ,the browser prepares the http request object and send its to the server.
2.server checks  whether the request is for static or dynamic information.
3.if the request ius for dynamic information  then webserver forwards the request to the web-container.
4.web-container identifies the corresponding servlet by usnig web.xml
5.web-container checks whether the servlet object is already available or not,if not web-container loads the       servlet class followed by instantiation and execution of init() method.
6.then web-container invokes public service() methos.if the service() method is available it will execute otherwise the HttpServlet  service() method will be executed. whic is implemented as follows

public void service(ServletRequest request,ServletResponse response) throws                                                                                                                                                                    ServletException,IOException
{
         HttpServletRequest=(HttpServletRequest)request;;
           HttpServletResponse=(HttpServletResponse)response;
           service(request,response);
}
7.web-container checks whether protected service() method available or not,if not HttpServlet service() will be executed like this
protected void service(  HttpServletRequest request,HttpServletResponse response) throws                                                                                                                                              ServletException,IOException
{
String method=request.getMethod();
if(method=="GET")
{
doGet(request,response);
}
else if(method=="POST")
{
doPost(request,response);
}

if(method=="HEAD")
{
doHead(request,response);
}
'
'
'
'
else
provide 501 status response saying invalid http method
}
8.from service() method web-container identifies the request method and invokes the corresponding doxxx() method.
9.
web-container checks whether this doxxx() method is available or not if notit will be execute the HttpServlet class doxxx() method which will having error information..

methods flow by the container:
 public service()------->protected service()-------->doxxx()


Search This Blog

All the rights are reserved to this blog is belongs to me only.. Powered by Blogger.