Upload
suresh
View
242
Download
0
Embed Size (px)
Citation preview
8/8/2019 Java Server Pages(JSP)
1/28
J2EE Web 1
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.
8/8/2019 Java Server Pages(JSP)
2/28
J2EE Web 2
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()
8/8/2019 Java Server Pages(JSP)
3/28
J2EE Web 3
Pre-assessment Questions (Contd.)
Identify the method that sends user information as query string appended to theURL.
5. GET
6. POST
7. HEAD
8. DELETE
8/8/2019 Java Server Pages(JSP)
4/28
J2EE Web 4
Solution to Pre-assessment Questions
a. init() d. Servlets can service multiple client requests. a. init() b. service() a. GET
8/8/2019 Java Server Pages(JSP)
5/28
J2EE Web 5
Objectives
In this lesson, you will learn about:
Classes and interfaces of Servlet API Servlet lifecycle events Deployment descriptor elements
8/8/2019 Java Server Pages(JSP)
6/28
J2EE Web 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
8/8/2019 Java Server Pages(JSP)
7/28J2EE Web 7
The Servlet API (Contd.)
The following table describes the various methods defined in theServletRequest interface:
Method Description
public String getParameter(String
paramName)Returns a String object that specifies thevalue 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.
8/8/2019 Java Server Pages(JSP)
8/28J2EE Web 8
The Servlet API (Contd.)
Methods ofServletRequest 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 IPaddress of the computer from which the
request is sent.
8/8/2019 Java Server Pages(JSP)
9/28
J2EE Web 9
The Servlet API (Contd.)
The following table describes the various methods defined in theServletResponse interface:Method Description
public ServletOutputStream
getOutputStream() throws
IOException
Returns an object of theServletOutputStream class that
represents an output stream to sendbinary data as response.
public PrintWriter getWriter()
throws IOException
Returns an object of the PrintWriter
class that the servlet uses to sendcharacter 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.
8/8/2019 Java Server Pages(JSP)
10/28
J2EE Web 10
The Servlet API (Contd.)
The following table describes the various methods defined in theServletContext interface:
Method Description
public void setAttribute(String,
Object)Binds the object with a name andstores the name/value pair as an
attribute of the ServletContext
object. If an attribute alreadyexists, then this method replaces
the existing attribute.
public Object getAttribute(String
attrname)Returns the Object stored in theServletContext object with the
name passed as a parameter.
8/8/2019 Java Server Pages(JSP)
11/28
J2EE Web 11
The Servlet API (Contd.)
Methods ofServletContext 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
pname)Returns the value of the initialization
parameter with the name passed as aparameter.
public Enumeration
getInitParameterNames()Returns an Enumeration of String object thatcontains names of all the initialization
parameters.
8/8/2019 Java Server Pages(JSP)
12/28
Method Description
public int getMajorVersion()
Returns an integer value that specifies the major
version of the Servlet API that the Webcontainer 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 Webcontainer supports. If your Web container
supports the version 2.4 of the servlet API,this method will return 4.
12Lesson 1B / Slide12 of 28
J2EE Web Components
The Servlet API (Contd.)
Methods ofServletContext interface (Contd.):
8/8/2019 Java Server Pages(JSP)
13/28
J2EE Web 13
The Servlet API (Contd.)
The following table describes the various methods defined in theHttpServletRequest interface:
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 Enumerationof String objects
public Enumeration
getHeaderNames()Returns the names of all the requestheaders that a servlet can access as an
Enumeration of String objects.
8/8/2019 Java Server Pages(JSP)
14/28
J2EE Web 14
The Servlet API (Contd.)
The following table describes the various methods defined in theHttpServletResponse interface:
Method Description
void setHeader(String hname,
String hvalue)Sets the value of header, hname tohvalue. If the header has already been
set, the new value overwrites theexisting value.
void setIntHeader(String hname,
int hvalue) Sets the value of the header, hname tothe int value, hvalue. If the header has
already been set, the new value
overwrites the existing value.
8/8/2019 Java Server Pages(JSP)
15/28
J2EE Web 15
The Servlet API (Contd.)
Methods ofHttpServletResponse interface (Contd.):
Method Description
void setDateHeader(String hname,
long datev)Sets the value of the header, hname with a
long value datev. The datev represents thenumber of milliseconds since the midnight ofJanuary 1, 1970, GMT.
void addHeader(String hname, String
hvalue)Adds a header, hname with value, hvalue. This
method adds a new header if the header
already exists.void addIntHeader(String hname, int
hvalue)Adds a header, hname with an integer value,hvalue.
8/8/2019 Java Server Pages(JSP)
16/28
Method Description
void addDateHeader(String hname,
long datev)Adds a header, hname with valueequal to given date, datev. The value of
datev must be in milliseconds, which starts
since midnight, January 1,1970, GMT.
boolean containsHeader(String hname) Returns true if the header, hname has
already been set with a specific value andfalse, if not.
void sendRedirect(String url) Redirects a request to the specified URL.
16Lesson 1B / Slide16 of 28
J2EE Web Components
The Servlet API (Contd.)
Methods ofHttpServletResponse interface (Contd.):
8/8/2019 Java Server Pages(JSP)
17/28
J2EE Web 17
The Servlet API (Contd.)
The following table describes the various methods defined in the HttpSession
interface: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 theHttpSession 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 isfound for the specified attribute, then thegetAttribute() 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.
8/8/2019 Java Server Pages(JSP)
18/28
J2EE Web 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 displayan 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.
8/8/2019 Java Server Pages(JSP)
19/28
J2EE Web 19
Demonstration-Implementing Context
Initialization Parameter (Contd.)
Solution
Create a servlet to retrieve initialization parameters.
Specify the initialization parameters.
Access the servlet.
8/8/2019 Java Server Pages(JSP)
20/28
J2EE Web 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 aservlet application.
8/8/2019 Java Server Pages(JSP)
21/28
J2EE Web 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
8/8/2019 Java Server Pages(JSP)
22/28
J2EE Web 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 contextobject. 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.
8/8/2019 Java Server Pages(JSP)
23/28
J2EE Web 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.
8/8/2019 Java Server Pages(JSP)
24/28
J2EE Web 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
8/8/2019 Java Server Pages(JSP)
25/28
J2EE Web 25
Understanding Elements of
Deployment Descriptor (Contd.)
Various deployment descriptors are:
8/8/2019 Java Server Pages(JSP)
26/28
J2EE Web 26
Summary
In this lesson, you learned:
Java Servlet API consists of two packages:
javax.servlet package
javax.servlet.http package
The various interfaces ofjavax.servlet package are:
ServletRequest interface
ServletContext interface
ServletResponse interface
You can retrieve the request headers by using the various methods of theServletRequest interface.
You can send response to the client request by using the various methods ofServletResponse interface.
You can set a context attribute for a servlet application by using the various
methods ofServletContext interface.
8/8/2019 Java Server Pages(JSP)
27/28
J2EE Web 27
Summary (Contd.)
The various interfaces ofjavax.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
8/8/2019 Java Server Pages(JSP)
28/28
J2EE Web 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, , , ,
, and .