Monday, October 3, 2011

servlet API


Servlet API:

To develop servlets we need SERVLET API support. We have 2 Packages for Developing Servlet.
  1. javax.servlet
  2. javax.servlet.http

servlet: 

  this interface provides life cycle methods for our servlets. The life cycle methods are
  1.   init(ServletConfig)
  2.   service(ServletRequest,ServletResponse)
  3.     destroy()

  in our Servlet class we override these methods. Servlet engine calls these methods implicity.servlet interface           has 2 more methods, which are non life cycle methods


  1.               getServletConfig():- it return ServletConfig object
  2.               getServletInfo():- it returns a string that gives servlet information

GenericServlet:-

This is an abstract class. This class implements servlet interface. It defined init(ServletConfig) and destroy() methods of the servlet interface. The third life cycle method service() is not defined in the GenericServlet class. This class also implements the ServletConfig interface. Therefore all the methods of ServletConfig interface can be called directly on the servlet interface itself. GenericServlet class defines one zero argument init method. Parameterized init method defined by the GenericServlet calls this zero argument init method. Therefore servlet developers can override only zero argument method in their servlet classes.

HttpServlet:-

                This is an abstract  class. It is a sub class of GenericServlet class. Servlet developers always defines their servlet class by extending this class. service method of the GenericServlet is implemented in this class. in addition to the public service method ,HttpServlet class has defined the following methods.

  1.                 protected void service(HttpServletRequest, HttpServletResponse)
  2.                 protected void doGet(HttpServletRequest, HttpServletResponse)
  3.                 protected void doPost(HttpServletRequest, HttpServletResponse)
servlet engine calls only public service method, within the public service method protected service method is called. Within the protected service method of the HttpServlet class , either doGet or doPost method is called depending upon thetype of request (GET or  Post) coming from the client. In our servlet class we always overrides either doGet() or doPost() .we never overrides the public service() or protected service().

  
ServletConfig:-

                This is an interface in java.servlet package.  Servlet engine writes a sub class of  ServletConfig interface and creates its instance. But we call it as ServletConfig object only.

                Methods:

  1.  String getInitParameter(String): this method is used to get init param value by supplying param name as input.
  2. ServletContext getServletContext() :-when we will call this method it returns the ServletContext object.              

 ServletContext:-

                This is an interface in java.servlet package.  Servlet engine writes a sub class of  ServletContext interface and creates its instance. But we call it as ServletContext object only. In the servlet programming this object is very widely used. For a servlet this object is directly available. We call a method in servlet to get the ServletContext object.

ServletContext sc=getServletContext();

In the above statement we are calling getServletContext() on the servlet interface. But internally it is called on the ServletConfig object.
            
    Methods:

  1. getInitParameter() :- it takes context param name and returns the corresponding value.
  2. setAttribute() : -stores a data item in context scope with name ,value pair.
  3. getAttribute() :-takes the attribute name and returns the value.
  4.  removeAttribute() :-delete the data item from the context scope.
  5. getRequestDispatcher() :-this method returns the RequestDispacher object.
  6. getServerInfo() :-it returns the web container information as a string .
  7. log() :-used for store the information into web container log files.

HttpServletRequest:

This is an interface in javax.servlet.http package. it as sub interface of ServletRequest interface. Servlet engine writes a sub class of HttpServletRequest interface and creates its instance. But we call it as HttpServletRequest object only.

                Methods:

  1.  getParameter() :- it takes html control name and returns the user input
  2.  setAttribute() : -stores a data item in request scope with name ,value pair.
  3. getAttribute() :-takes the attribute name and returns the value.
  4. removeAttribute() :-delete the data item from the request scope.
  5. getRequestDispatcher() :-this method returns the RequestDispacher object.
  6.  getCookies() :-it returns an array of Cookies coming from the browser.
  7. getSession(() :-returns the HttpSession Object which is unique for the client.
  8. getQueryString() :-Returns the query string that is contained in the request URL after the path.
  9. getHeaderNames() :-Returns an enumeration of all the header names this request contains.
  10. getHeader() :-it takes the header name and returns the header value.

HttpServletResponse:

This is an interface in javax.servlet.http package. it as sub interface of ServletResponse interface. Servlet engine writes a sub class of HttpServletResponse interface and creates its instance. But we call it as HttpServletResponse object only.
          
      Methods:
  1.  getWriter() :-it returns the PrinterWriter object.
  2.  setContentType() :-this method set the MIME type for the response.
  3. getOutputStream() :-returns the OutputStream object.
  4.  addCookie() :-it addsa cookie to the response header.
  5. encodeURL() :- this method is used to URL rewriting .
  6. sendRedirect() :-sends a temporary redirect response to the client.
  7.  sendError() :-sends an error response to the client using error status.
  8. setHeader() :- sets a response header with the given name and value.
  9.  addHeader() :-adds a response header with the given name and value.

RequestDispacher:-
         
       It is an interface. Servlet container writers a sub class for this interface. Servlet engine produces this sub class object when we call getRequestDispacher() on the request or context object. But we say that object as RequestDispacher object only.

                Methods:
  1.   forward()  :- it forwards the control from the servlet to another web resource.
  2.  Include() :-it includes the response of other web resource into the current servlet response.


Directory Structure:
                Root Directory
                                WEB-INF
                                                classes
                                                lib
                                                src
                                                web.xml
                                Resource files

Root directory name should be anything usually it is name of the project/application
classes directory used to keep the java class file(.class files)
lib directory contains the jar file(.jar files)
src or source directory for java files(.java)
web.xml file is call deployment descriptor file
the above files and directories are under “WEB-INF” directory
resource contains the image and html or jsp file …….etc.

Example Project