Você está na página 1de 28

Pre-assessment Questions

1. Which method is invoked only once in the life cycle of a servlet?


a. init()
b. doPost()
c. service()
d. doGet()

2. Identify the correct feature of servlets.


a. Servlets are client-side components that generates dynamic
contents.
b. Servlets by default can service only one client request at a time.
c. Servlets are platform dependent.
d. Servlets can service multiple client requests. Les
son
1B
/
Sli
de
1
J2EE Web Components of
28
Pre-assessment Questions (Contd.)
3. Identify the method called by the Web container after creating a servlet
instance.
a. init()
b. service()
c. start()
d. destroy()

4. Which method is automatically invoked by the Web container when a


request arrives?
a. init()
b. service()
c. doPost()
d. doGet()

J2EE Web Components Lesson


1B /
Slide 2
of 28
Pre-assessment Questions (Contd.)
5. Identify the method that sends user information as query string
appended to the URL.
a. GET
b. POST
c. HEAD
d. DELETE

J2EE Web Components Lesson


1B /
Slide 3
of 28
Solution to Pre-assessment Questions
1. a. init()
2. d. Servlets can service multiple client requests.
3. a. init()
4. b. service()
5. a. GET

J2EE Web Components Lesson


1B /
Slide 4
of 28
Objectives
In this lesson, you will learn about:

• Classes and interfaces of Servlet API


• Servlet lifecycle events
• Deployment descriptor elements

J2EE Web Components Lesson


1B /
Slide 5
of 28
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 Components Lesson


1B /
Slide 6
of 28
The Servlet API (Contd.)
• The following table describes the various methods defined in the
ServletRequest interface:
Method Description

public String getParameter(String Returns a String object that specifies the


paramName) value of a particular request parameter.

public String[] Returns an array of String objects that


getParameterValues(String contains all the values of the request
paramName) parameter.

public Enumeration Returns an Enumeration containing all


getParameterNames() the parameter names as String objects
that a servlet request contains.

J2EE Web Components Lesson


1B /
Slide 7
of 28
The Servlet API (Contd.)
• Methods of ServletRequest interface (Contd.):

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.

J2EE Web Components Lesson


1B /
Slide 8
of 28
The Servlet API (Contd.)
• The following table describes the various methods defined in the
ServletResponse interface:
Method Description

public ServletOutputStream Returns an object of the


getOutputStream() throws ServletOutputStream class that
IOException represents an output stream to send
binary data as response.
public PrintWriter getWriter() Returns an object of the PrintWriter
throws IOException class that the servlet uses to send
character data as response.

public void setContentType(String Sets the MIME type for a servlet response,
type) such as text/plain, image/jpeg and
text/html.

J2EE Web Components Lesson


1B /
Slide 9
of 28
The Servlet API (Contd.)
• The following table describes the various methods defined in the
ServletContext interface:

Method Description

public void setAttribute(String, Binds the object with a name and


Object) 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 Returns the Object stored in the


attrname) ServletContext object with the
name passed as a parameter.

J2EE Web Components Lesson


1B /
Slide 10
of 28
The Servlet API (Contd.)
• Methods of ServletContext interface (Contd.):

Method Description

public Enumeration Returns an Enumeration of String object


getAttributeNames() that contains names of all the context
attributes.

public String Returns the value of the initialization


getInitParameter(String pname) parameter with the name passed as a
parameter.

public Enumeration Returns an Enumeration of String object


getInitParameterNames() that contains names of all the initialization
Les
parameters. son
1B
/
Sli
de
11
J2EE Web Components of
28
The Servlet API (Contd.)
• Methods of ServletContext interface (Contd.):

Method Description
public int getMajorVersion() 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.
public int getMinorVersion() 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.

J2EE Web Components


Lesson 1B / Slide 12 of 28
The Servlet API (Contd.)
• The following table describes the various methods defined in the
HttpServletRequest interface:

Method Description

public String getHeader(String Returns the value of the request header


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

Method Description

void setHeader(String hname, Sets the value of header, hname to


String hvalue) hvalue. If the header has already been
set, the new value overwrites the
existing value.

void setIntHeader(String hname, Sets the value of the header, hname to


int hvalue) the int value, hvalue. If the header has
already been set, the new value
overwrites the existing value. Les
son
1B
/
Sli
de
14
J2EE Web Components of
28
The Servlet API (Contd.)
• Methods of HttpServletResponse interface (Contd.):

Method Description
void setDateHeader(String hname, Sets the value of the header, hname with a
long datev) long value datev. The datev represents the
number of milliseconds since the midnight of
January 1, 1970, GMT.
void addHeader(String hname, Adds a header, hname with value, hvalue.
String hvalue) This method adds a new header if the header
already exists.
void addIntHeader(String hname, Adds a header, hname with an integer value,
int hvalue) hvalue. Les
son
1B
/
Sli
de
15
J2EE Web Components of
28
The Servlet API (Contd.)
• Methods of HttpServletResponse interface (Contd.):

Method Description

void addDateHeader(String hname, Adds a header, hname with value


long datev) equal to given date, datev. The value of
datev must be in milliseconds, which starts
since midnight, January 1,1970, GMT.

boolean containsHeader(String Returns true if the header, hname has


hname) 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


Lesson 1B / Slide 16 of 28
The Servlet API (Contd.)
• The following table describes the various methods defined in the
HttpSession interface:
Method Description

public void Binds the object with a name and stores the
setAttribute(String name, name/value pair as an attribute of the
Object value) HttpSession object. If an attribute already exists,
then this method replaces the existing attribute.
public Object Retrieves the String object specified in the
getAttribute(String name) parameter, from the session object. If no object is
found for the specified attribute, then the
getAttribute() method returns null.
Les
public Enumeration Returns an Enumeration that contains the name of son
getAttributeNames() all the objects that are bound as attributes to the 1B
session object. /
Sli
de
17
J2EE Web Components of
28
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.

Les
son
1B
/
Sli
de
18
J2EE Web Components of
28
Demonstration-Implementing Context
Initialization Parameter (Contd.)
• Solution

1. Create a servlet to retrieve initialization parameters.


2. Specify the initialization parameters.
3. Access the servlet.

Les
son
1B
/
Sli
de
19
J2EE Web Components of
28
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
a Web application.
• HTTP Session Events: Relates to changes in the session object of a
servlet application.

Les
son
1B
/
Sli
de
20
J2EE Web Components of
28
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
Les
son
1B
/
Sli
de
21
J2EE Web Components of
28
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.

Les
son
1B
/
Sli
de
22
J2EE Web Components of
28
Demonstration-Implementing Servlet
Life Cycle Events (Contd.)
• Solution

1. Create a servlet that adds an attribute to context object.


2. Create a servlet that logs the time of all the events generated
by first servlet to the server.log file.
3. Deploy the two servlets. While deploying, register the second
servlet to listen to the events generated by first servlet.
4. Run the program.

Les
son
1B
/
Sli
de
23
J2EE Web Components of
28
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
Les
son
1B
/
Sli
de
24
J2EE Web Components of
28
Understanding Elements of
Deployment Descriptor (Contd.)
• Various deployment descriptors are:

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

Les
son
1B
/
Sli
de
25
J2EE Web Components of
28
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. Les
son
• You can set a context attribute for a servlet application by using the 1B
various methods of ServletContext interface. /
Sli
de
26
J2EE Web Components of
28
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: Les
son
• Servlet request events 1B
/
Sli
de
27
J2EE Web Components of
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>, Les
son
<session-config>, and <listener>. 1B
/
Sli
de
28
J2EE Web Components of
28

Você também pode gostar