Pre-assessment Questions

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

J2EE Web

1

Pre-assessment Questions (Contd.)

Identify the method called by the Web container after creating a servlet instance. 3. init() 4. service() 5. start() 6. destroy() Which method is automatically invoked by the Web container when a request arrives? 4. init() 5. service() 6. doPost() 7. doGet()

J2EE Web

2

Pre-assessment Questions (Contd.)

Identify the method that sends user information as query string appended to the URL. 5. GET 6. POST 7. HEAD 8. DELETE

J2EE Web

3

Solution to Pre-assessment Questions

● ● ● ● ●

a. d. a. b. a.

init() Servlets can service multiple client requests. init() service() GET

J2EE Web

4

Objectives
In this lesson, you will learn about:
● ● ●

Classes and interfaces of Servlet API Servlet lifecycle events Deployment descriptor elements

J2EE Web

5

The Servlet API
• •
The Servlet API consists of: javax.servlet package contains:

• • • • • • •

ServletRequest Interface ServletResponse Interface ServletContext Interface

javax.servlet.http package contains: HttpServletRequest Interface HttpServletResponse Interface HfttpSession Interface


J2EE Web 6

The Servlet API (Contd.)

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

J2EE Web

7

The Servlet API (Contd.)

Methods of ServletRequest interface (Contd.): Method public String getRemoteHost() Description Returns a String that specifies the fullqualified name of the computer from which the request is sent. Returns a String that specifies the IP address of the computer from which the request is sent.

public String getRemoteAddr()

J2EE Web

8

The Servlet API (Contd.)

The following table describes the various methods defined in the ServletResponse interface: Method Description Returns an object of the ServletOutputStream class that represents an output stream to send binary data as response. Returns an object of the PrintWriter class that the servlet uses to send character data as response. Sets the MIME type for a servlet response, such as text/plain, image/jpeg and text/html.

public ServletOutputStream getOutputStream() throws IOException public PrintWriter getWriter() throws IOException

public void setContentType(String type)

J2EE Web

9

The Servlet API (Contd.)

The following table describes the various methods defined in the ServletContext interface: Method Description 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. Returns the Object stored in the ServletContext object with the name passed as a parameter.

public void setAttribute(String, Object)

public Object getAttribute(String attrname)

J2EE Web

10

The Servlet API (Contd.)

Methods of ServletContext interface (Contd.): Method Description

public Enumeration getAttributeNames() Returns an Enumeration of String object that contains names of all the context attributes. public String getInitParameter(String Returns the value of the initialization pname) parameter with the name passed as a parameter. public Enumeration getInitParameterNames() Returns an Enumeration of String object that contains names of all the initialization parameters.

J2EE Web

11

The Servlet API (Contd.)

Methods of ServletContext interface (Contd.): Method Description Returns an integer value that specifies the major version of the Servlet API that the Web container supports. If your Web container supports the version 2.4 of the servlet API, this method will return 2. Returns an integer value that specifies the minor version of the servlet API that the Web container supports. If your Web container supports the version 2.4 of the servlet API, this method will return 4.

public int getMajorVersion()

public int getMinorVersion()

J2EE Web Components

12Lesson 1B / Slide 12 of 28

The Servlet API (Contd.)

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

J2EE Web

13

The Servlet API (Contd.)

The following table describes the various methods defined in the HttpServletResponse interface: Method Description Sets the value of header, hname to hvalue. If the header has already been set, the new value overwrites the existing value. 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.

void setHeader(String hname, String hvalue)

void setIntHeader(String hname, int hvalue)

J2EE Web

14

The Servlet API (Contd.)

Methods of HttpServletResponse interface (Contd.): Method void setDateHeader(String hname, long datev) Description Sets the value of the header, hname with a long value datev. The datev represents the number of milliseconds since the midnight of January 1, 1970, GMT.

void addHeader(String hname, String Adds a header, hname with value, hvalue. This hvalue) method adds a new header if the header already exists. void addIntHeader(String hname, int Adds a header, hname with an integer value, hvalue) hvalue.

J2EE Web

15

The Servlet API (Contd.)

Methods of HttpServletResponse interface (Contd.): Method Description Adds a header, hname with value equal to given date, datev. The value of datev must be in milliseconds, which starts since midnight, January 1,1970, GMT.

void addDateHeader(String hname, long datev)

boolean containsHeader(String hname) Returns true if the header, hname has already been set with a specific value and false, if not. void sendRedirect(String url) Redirects a request to the specified URL.

J2EE Web Components

16Lesson 1B / Slide 16 of 28

The Servlet API (Contd.)

The following table describes the various methods defined in the HttpSession Method Description interface: 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. 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. Returns an Enumeration that contains the name of all the objects that are bound as attributes to the session object.

public void setAttribute(String name, Object value) public Object getAttribute(String name)

public Enumeration getAttributeNames()

J2EE Web

17

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 18

Demonstration-Implementing Context Initialization Parameter (Contd.)

• • • •

Solution Create a servlet to retrieve initialization parameters. Specify the initialization parameters. Access the servlet.

J2EE Web

19

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 the in the context of Web application. HTTP Session Events: Relates to changes in the session object of a servlet application.

a

J2EE Web

20

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

21

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

22

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

23

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

24

Understanding Elements of Deployment Descriptor (Contd.)

Various deployment descriptors are:

• • • •

<context-param> <init-param> <mime-mapping> <servlet-mapping>

J2EE Web

25

Summary
• • •
In this lesson, you learned: Java Servlet API consists of two packages: • javax.servlet package

• • • • • • •

javax.servlet.http package ServletRequest interface ServletContext interface ServletResponse interface

The various interfaces of javax.servlet package are:

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

26

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

27

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

28

Sign up to vote on this title
UsefulNot useful