Tuesday, July 10, 2012

Javax.servlet package

<<Previous                                                                                  Next>>

This package defines several classes and interfaces to develop servlet from the scratch irrespective of any protocol.  There are several interfaces in this package. Those are
01. Servlet: 
  • Every servlet in java should implement Servlet interface either directly or indirectly.
  • This interface defines the most common general methods which are applicable for any servlet object.
  • The life cycle methods of servlet are defined in this interface only.
02. Servlet Request:
  • This interface defines several methods to retrieve client information from request object.
        Example:     getParameter (“”);

03. ServletResponse:
  • This interface defines several methods which are useful for preparation of response.
         Example:     setContentType (“”);

04. ServletConfig:
  • For every servlet web container creates one ServletConfig object to hold its configuration information.  As Servlet can get its configuration information by using config object.     
05. ServletContext:
  • For every web application web container creates one context object to hold application level configuration information.
  • A servlet can get a context object by using getServletContext () of ServletConfig.
Note: - ServletConfig is per Servlet (one servlet) whereas ServletContext is per web application.

06. RequestDispatcher:
  • By using RequestDispatcher object we can dispatch the request to another web component.  
  • This interface defines the following two methods.
1. forward()
2. include()



07.  SingleThreadModel:
  • We can use this interface to provide thread safety to the servlet.  If Servlet class implements SingleThreadModel then at a time.
  • One request can be processed by Servlet object.  The main advantage of this approach is we can overcome data inconsistency problem.  But the main disadvantage if this approach is it increases waiting time of the threads and effects performance of the system.  Hence it is not recommended to use SingleThreadModel.
  • It is deprecated in Servlet 2.3v without any replacement.
  • We can provide thread safety for the Servlet by using synchronized keyword only.
  • SingleThreadModel does not contain any method. It’s a marker interface.
08. Filter
09FilterConfig
10. FilterChain
11. ServletRequestListener
12. ServletRequestAttributeListener
13. ServletContextListener
14. ServletContextAttributeListener

Classes of javax.servlet package:
1. GenericServlet:
  • It implements Servlet interface and provides default implementation for every method except service() method hence, it is an abstract class.
  • We can use GenericServlet as base class to develop protocol independent servlets.
2. ServletOutputStream:
  • We can user ServletOutputStream to send binary data (image files, moving files, audio files) as response.
        Example:   response.getOutputStream ()
3. ServletInputStream:
  • We can use ServletInputStream to rad binary data sent by the client.
        Example:    request.getInputSteam ()
In addition to the above classes javax.servlet package contains the following classes also.
4. ServletRequestWrapper     
5. ServletResponseWrapper
6. ServletRequestEvent 
7. ServletRequestAttributeEvent
8. ServletContextEvent
9. ServletContextAttributeEvent

Exceptions of javax.servlet package
1. ServletException:
  • Servlet can throw this exception whenever it faces any difficulty while processing our request.
2. UnavailableException:
  •   This is child class of Servlet Exception and deprecated.
                Note:  javax.servlet package contains.
                14 interfaces, 9 classes and 2 exceptions.

No comments:

Post a Comment