1 / 28

Pre-assessment Questions Which method is invoked only once in the life cycle of a servlet?

Learn about the Servlet API, its classes, interfaces, and methods. Understand the servlet lifecycle events and deployment descriptor elements.

Download Presentation

Pre-assessment Questions Which method is invoked only once in the life cycle of a servlet?

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Pre-assessment Questions • Which method is invoked only once in the life cycle of a servlet? • init() • doPost() • service() • doGet() • Identify the correct feature of servlets. • Servlets are client-side components that generates dynamic contents. • Servlets by default can service only one client request at a time. • Servlets are platform dependent. • Servlets can service multiple client requests. J2EE Web Components

  2. Pre-assessment Questions (Contd.) • Identify the method called by the Web container after creating a servlet instance. • init() • service() • start() • destroy() • Which method is automatically invoked by the Web container when a request arrives? • init() • service() • doPost() • doGet() J2EE Web Components

  3. Pre-assessment Questions (Contd.) • Identify the method that sends user information as query string appended to the URL. • GET • POST • HEAD • DELETE J2EE Web Components

  4. Solution to Pre-assessment Questions • a. init() • d. Servlets can service multiple client requests. • a. init() • b. service() • a. GET J2EE Web Components

  5. Objectives • In this lesson, you will learn about: • Classes and interfaces of Servlet API • Servlet lifecycle events • Deployment descriptor elements J2EE Web Components

  6. The Servlet API • The Servlet API consists of: • javax.servlet package contains: • ServletRequest Interface • ServletResponseInterface • ServletContextInterface • javax.servlet.http package contains: • HttpServletRequest Interface • HttpServletResponse Interface • HfttpSession Interface J2EE Web Components

  7. Method Description public String getParameter(String paramName) Returns a String object that specifies the value of a particular request parameter. public String[] getParameterValues(String paramName) Returns an array of String objects that contains all the values of the request parameter. public Enumeration getParameterNames() Returns an Enumeration containing all the parameter names as String objects that a servlet request contains. • The Servlet API (Contd.) • The following table describes the various methods defined in the ServletRequest interface: J2EE Web Components

  8. Method Description public String getRemoteHost() Returns a String that specifies the full-qualified name of the computer from which the request is sent. public String getRemoteAddr() Returns a String that specifies the IP address of the computer from which the request is sent. • The Servlet API (Contd.) • Methods of ServletRequest interface (Contd.): J2EE Web Components

  9. Method Description public ServletOutputStream getOutputStream() throws IOException Returns an object of the ServletOutputStream class that represents an output stream to send binary data as response. public PrintWriter getWriter() throws IOException Returns an object of the PrintWriter class that the servlet uses to send character data as response. public void setContentType(String type) Sets the MIME type for a servlet response, such as text/plain, image/jpeg and text/html. • The Servlet API (Contd.) • The following table describes the various methods defined in the ServletResponse interface: J2EE Web Components

  10. Method Description public void setAttribute(String, Object) Binds the object with a name and stores the name/value pair as an attribute of the ServletContext object. If an attribute already exists, then this method replaces the existing attribute. public Object getAttribute(String attrname) Returns the Object stored in the ServletContext object with the name passed as a parameter. • The Servlet API (Contd.) • The following table describes the various methods defined in the ServletContext interface: J2EE Web Components

  11. The Servlet API (Contd.) • Methods of ServletContext interface (Contd.): J2EE Web Components

  12. The Servlet API (Contd.) • Methods of ServletContext interface (Contd.): J2EE Web Components

  13. Method Description public String getHeader(String fieldname) Returns the value of the request header field such as Cache-Control and Accept-Language specified in parameter. public Enumeration getHeaders(String sname) Returns all the values associated with a specific request header as an Enumeration of String objects public Enumeration getHeaderNames() Returns the names of all the request headers that a servlet can access as an Enumeration of String objects. • The Servlet API (Contd.) • The following table describes the various methods defined in the HttpServletRequest interface: J2EE Web Components

  14. Method Description void setHeader(String hname, String hvalue) Sets the value of header, hname to hvalue. If the header has already been set, the new value overwrites the existing value. void setIntHeader(String hname, int hvalue) Sets the value of the header, hname to the int value, hvalue. If the header has already been set, the new value overwrites the existing value. • The Servlet API (Contd.) • The following table describes the various methods defined in the HttpServletResponse interface: J2EE Web Components

  15. The Servlet API (Contd.) • Methods of HttpServletResponse interface (Contd.): J2EE Web Components

  16. The Servlet API (Contd.) • Methods of HttpServletResponse interface (Contd.): J2EE Web Components

  17. Method Description public void setAttribute(String name, Object value) Binds the object with a name and stores the name/value pair as an attribute of the HttpSession object. If an attribute already exists, then this method replaces the existing attribute. public Object getAttribute(String name) Retrieves the String object specified in the parameter, from the session object. If no object is found for the specified attribute, then the getAttribute() method returns null. public Enumeration getAttributeNames() Returns an Enumeration that contains the name of all the objects that are bound as attributes to the session object. • The Servlet API (Contd.) • The following table describes the various methods defined in the HttpSession interface: J2EE Web Components

  18. Demonstration-Implementing Context Initialization Parameter • Problem Statement • Smart Softwares wants to develop a Web application that will use servlets to provide the information about the company. The servlets, which are created for handling the presentation logic, needs to display an email-id where an end user can send feedbacks and suggestions. The company wants to avoid changing the application code, whenever the email-id of the company changes. J2EE Web Components

  19. Demonstration-Implementing Context Initialization Parameter (Contd.) • Solution • Create a servlet to retrieve initialization parameters. • Specify the initialization parameters. • Access the servlet. J2EE Web Components

  20. Servlet Life Cycle API • Types of Events generated during the life cycle of a servlet are: • Servlet Request Events: Relates to changes in the request objects associated with a Web application. • Servlet Context Events: Relates to changes in thein the context of a Web application. • HTTP Session Events: Relates to changes in the session object of a servlet application. J2EE Web Components

  21. Servlet Life Cycle API (Contd.) • Handling of Servlet Life Cycle Events • The classes that receive notification about the servlet life cycle events are known as event listeners. • Various event listener interfaces are: • ServletRequestListener Interface • ServletRequestAttributeListener Interface • ServletContextListener interface • ServletContextAttributeListener Interface • HttpSessionListener Interface • HttpSessionAttributeListener Interface • HttpSessionActivationListener Interface J2EE Web Components

  22. Demonstration-Implementing Servlet Life Cycle Events • Problem Statement • David Wong, the administrator at Smart Software Developers want to create an application that will log the time when request and context objects are initialized and when attribute is added to the context object. At the same time, the application should also log the time when the attribute is removed from the context object and request and context objects are destroyed. J2EE Web Components

  23. Demonstration-Implementing Servlet Life Cycle Events (Contd.) • Solution • Create a servlet that adds an attribute to context object. • Create a servlet that logs the time of all the events generated by first servlet to the server.log file. • Deploy the two servlets. While deploying, register the second servlet to listen to the events generated by first servlet. • Run the program. J2EE Web Components

  24. Understanding Elements of Deployment Descriptor • A deployment descriptor is an XML file with the name web.xml that contains configuration information of a Web application. • Each Web application contains one web.xml file. • The elements of the web.xml file enable you to specify: • Initialization parameter for the servlet • MIME type for different files • Listener classes • Mapping a URL pattern to a servlet J2EE Web Components

  25. Understanding Elements of Deployment Descriptor (Contd.) • Various deployment descriptors are: • <context-param> • <init-param> • <mime-mapping> • <servlet-mapping> J2EE Web Components

  26. Summary • In this lesson, you learned: • Java Servlet API consists of two packages: • javax.servlet package • javax.servlet.http package • The various interfaces of javax.servlet package are: • ServletRequest interface • ServletContext interface • ServletResponse interface • You can retrieve the request headers by using the various methods of the ServletRequest interface. • You can send response to the client request by using the various methods of ServletResponse interface. • You can set a context attribute for a servlet application by using the various methods of ServletContext interface. J2EE Web Components

  27. Summary (Contd.) • The various interfaces of javax.servlet.http interface are: • HttpServletRequest interface • HttpServletResponse interface • HttpSession interface • The HttpServletRequest interface provides various methods for retrieving request parameters sent using HTTP. • The HttpServletResponse interface provides various methods to handle response and status codes for servlets using HTTP. • The HttpSession interface provides various methods to maintain the state of an end user across a Web application. • You can initialize servlet applications by using the initialization parameters. • The servlet life cycle events generated within the Web container are: • Servlet request events J2EE Web Components

  28. Summary (Contd.) • Servlet context events • Servlet session events • The various interfaces for handling servlet life cycle events are: • ServletRequestListener interface • ServletRequestAttributeListener interface • ServletContextListener interface • ServletContextAttributeListener interface • HttpSessionListener interface • HttpSessionAttributeListener interface • HttpSessionActivationListener interface • The various elements of the Web application deployment descriptor are <context-param>, <init-param>, <mime-mapping>, <servlet-mapping>, <session-config>, and <listener>. J2EE Web Components

More Related