Unit V WT
Unit V WT
Introduction – Servlet – Architecture – Lifecycle– Generic Servlet & HTTP Servlet - JSP – Overview –
Objects – scripting – Standard Actions – Directives.
Exercise : implement the web applications using (a) Servlets and (b) JSP
--------------------------------------------------------------------------------------------------------------------
Introduction – Servlet
Servlets are the Java programs that run on the Java-enabled web server or application server.
They are used to handle the request obtained from the webserver, process the request, produce the
response, then send a response back to the webserver.
Lifecycle
The entire life cycle of a Servlet is managed by the Servlet container which uses
the javax.servlet.Servlet interface to understand the Servlet object and manage it. So, before
creating a Servlet object, let’s first understand the life cycle of the Servlet object which is actually
understanding how the Servlet container manages the Servlet object.
Stages of the Servlet Life Cycle: The Servlet life cycle mainly goes through four stages,
Loading a Servlet.
Initializing the Servlet.
Request handling.
Destroying the Servlet.
Let’s look at each of these stages in details:
1. Loading a Servlet: The first stage of the Servlet lifecycle involves loading and initializing the
Servlet by the Servlet container. The Web container or Servlet Container can load the Servlet at
either of the following two stages :
Initializing the context, on configuring the Servlet with a zero or positive integer value.
If the Servlet is not preceding stage, it may delay the loading process until the Web
container determines that this Servlet is needed to service a request.
2. Initializing a Servlet: After the Servlet is instantiated successfully, the Servlet container
initializes the instantiated Servlet object. The container initializes the Servlet object by
invoking the Servlet.init(ServletConfig) method which accepts ServletConfig object
reference as parameter.
The Servlet container invokes the Servlet.init(ServletConfig) method only once,
immediately after the Servlet.init(ServletConfig) object is instantiated successfully. This
method is used to initialize the resources, such as JDBC datasource. Now, if the Servlet fails to
initialize, then it informs the Servlet container by throwing
the ServletException or UnavailableException.
3. Handling request: After initialization, the Servlet instance is ready to serve the client
requests. The Servlet container performs the following operations when the Servlet instance
is located to service a request :
4. Destroying a Servlet: When a Servlet container decides to destroy the Servlet, it performs the
following operations,
It allows all the threads currently running in the service method of the Servlet instance to
complete their jobs and get released.
After currently running threads have completed their jobs, the Servlet container calls
the destroy() method on the Servlet instance.
After the destroy() method is executed, the Servlet container releases all the references of this
Servlet instance so that it becomes eligible for garbage collection.
JSP Overview
Java Server Pages (JSP) is a server-side programming technology that enables the creation of dynamic,
platform-independent method for building Web-based applications. JSP have access to the entire family
of Java APIs, including the JDBC API to access enterprise databases.
JavaServer Pages often serve the same purpose as programs implemented using the Common Gateway
Interface (CGI). But JSP offers several advantages in comparison with the CGI.
Performance is significantly better because JSP allows embedding Dynamic Elements in HTML
Pages itself instead of having separate CGI files.
JSP are always compiled before they are processed by the server unlike CGI/Perl which requires
the server to load an interpreter and the target script each time the page is requested.
JavaServer Pages are built on top of the Java Servlets API, so like Servlets, JSP also has access
to all the powerful Enterprise Java APIs, including JDBC, JNDI, EJB, JAXP, etc.
JSP pages can be used in combination with servlets that handle the business logic, the model
supported by Java servlet template engines.
JSP Objects
These Objects are the Java objects that the JSP Container makes available to the developers in
each page and the developer can call them directly without being explicitly declared. JSP Implicit
Objects are also called pre-defined variables.
Following table lists out the nine Implicit Objects that JSP supports –
request
1
This is the HttpServletRequest object associated with the request.
response
2
This is the HttpServletResponse object associated with the response to the client.
out
3
This is the PrintWriter object used to send output to the client.
session
4
This is the HttpSession object associated with the request.
application
5
This is the ServletContext object associated with the application context.
config
6
This is the ServletConfig object associated with the page.
pageContext
7
This encapsulates use of server-specific features like higher performance JspWriters.
page
8 This is simply a synonym for this, and is used to call the methods defined by the
translated servlet class.
Exception
9
The Exception object allows the exception data to be accessed by designated JSP.
JSP Scripting
In JSP, java code can be written inside the jsp page using the scriptlet tag. Let's see what are the
scripting elements first.
Standard Actions
These actions use constructs in XML syntax to control the behavior of the servlet engine. You
can dynamically insert a file, reuse JavaBeans components, forward the user to another page, or
generate HTML for the Java plugin.
There is only one syntax for the Action element, as it conforms to the XML standard −
<jsp:action_name attribute = "value" />
Action elements are basically predefined functions.
The following table lists out the available JSP actions −
S.No. Syntax & Purpose
jsp:include
1
Includes a file at the time the page is requested.
jsp:useBean
2
Finds or instantiates a JavaBean.
jsp:setProperty
3
Sets the property of a JavaBean.
jsp:getProperty
4
Inserts the property of a JavaBean into the output.
jsp:forward
5
Forwards the requester to a new page.
jsp:plugin
6 Generates browser-specific code that makes an OBJECT or EMBED tag for the Java
plugin.
jsp:element
7
Defines XML elements dynamically.
jsp:attribute
8
Defines dynamically-defined XML element's attribute.
jsp:body
9
Defines dynamically-defined XML element's body.
jsp:text
10
Used to write template text in JSP pages and documents.
JSP Directives.
The jsp directives are messages that tells the web container how to translate a JSP page into the
corresponding servlet.
There are three types of directives:
o page directive
o include directive
o taglib directive
Syntax of JSP Directive
<%@ directive attribute="value" %>
Example:
<html>
<body>
<%@ include file="header.html" %>
Today is: <%= java.util.Calendar.getInstance().getTime() %>
</body>
</html>
Example:
<html>
<body>
<%@ taglib uri="http://www.javatpoint.com/tags" prefix="mytag" %>
<mytag:currentDate/>
</body>
</html>