Documente Academic
Documente Profesional
Documente Cultură
(MCA-305)
UNIT - I
© Bharati Vidyapeeth’s Institute of Computer Applications and Management, New Delhi-63, by Dr. Sunil Pratap Singh Unit 1
Java, J2SE, J2EE and J2ME
• “Java” refers to both, a language and a platform.
©
© Bharati Vidyapeeth’s
Vidyapeeth’s Institute
Institute of
of Computer
Computer Applications
Applications and
and Management,
Management, New
New Delhi-63,
Delhi-63, by Dr. Sunil Pratap Singh
Singh Unit
Unit1‹#›
1
Java Programming Language Platforms
• Java Platform, Standard Edition (Java SE)
• Java Platform, Enterprise Edition (Java EE)
• Java Platform, Micro Edition (Java ME)
©
© Bharati Vidyapeeth’s
Vidyapeeth’s Institute
Institute of
of Computer
Computer Applications
Applications and
and Management,
Management, New
New Delhi-63,
Delhi-63, by Dr. Sunil Pratap Singh
Singh Unit
Unit1‹#›
1
Enterprise Edition (Java EE)
• The Java EE platform is built on top of the Java SE platform.
• It uses many components of SE, as well as, has many new features of
it’s own like Servlets, JavaBeans, etc., adding a whole new
functionalities to the language.
• It uses HTML, CSS, JavaScript etc., so as to create web pages and web
services.
• It is one of the most widely accepted web development standard.
©
© Bharati Vidyapeeth’s
Vidyapeeth’s Institute
Institute of
of Computer
Computer Applications
Applications and
and Management,
Management, New
New Delhi-63,
Delhi-63, by Dr. Sunil Pratap Singh
Singh Unit
Unit1‹#›
1
Micro Edition (Java ME)
• The Java ME platform provides an API and a small-footprint JVM for
running Java programming language applications on small devices, like
mobile phones, set top boxes, etc.
• The API is a subset of the Java SE API, along with special class libraries
useful for small device application development.
• Most of the apps, developed for the phones (prior to smartphones era),
were built on J2ME platform .
Old Nokia phones, which used Symbian OS, used this technology.
©
© Bharati Vidyapeeth’s
Vidyapeeth’s Institute
Institute of
of Computer
Computer Applications
Applications and
and Management,
Management, New
New Delhi-63,
Delhi-63, by Dr. Sunil Pratap Singh
Singh Unit
Unit1‹#›
1
JavaFX
• JavaFX is another edition of Java technology, which is now merged with
SE 8.
• It replaces Swings (in SE), with itself as the standard GUI library.
©
© Bharati Vidyapeeth’s
Vidyapeeth’s Institute
Institute of
of Computer
Computer Applications
Applications and
and Management,
Management, New
New Delhi-63,
Delhi-63, by Dr. Sunil Pratap Singh
Singh Unit
Unit1‹#›
1
Developing Enterprise Applications
• The Java EE platform is designed to help developers create
large-scale,
multi-tiered,
scalable,
reliable, and
secure network applications
©
© Bharati Vidyapeeth’s
Vidyapeeth’s Institute
Institute of
of Computer
Computer Applications
Applications and
and Management,
Management, New
New Delhi-63,
Delhi-63, by Dr. Sunil Pratap Singh
Singh Unit
Unit1‹#›
1
Multitiered Application Model
• The Java EE platform uses a distributed multi-tiered application model
for enterprise applications.
• In a multi-tiered application, the functionality of the application is
separated into isolated functional areas, called tiers.
• Typically, multi-tiered applications have
a client tier,
a middle tier, and
a data tier (often called the enterprise information systems tier)
©
© Bharati Vidyapeeth’s
Vidyapeeth’s Institute
Institute of
of Computer
Computer Applications
Applications and
and Management,
Management, New
New Delhi-63,
Delhi-63, by Dr. Sunil Pratap Singh
Singh Unit
Unit1‹#›
1
Multitiered Application Model
©
© Bharati Vidyapeeth’s
Vidyapeeth’s Institute
Institute of
of Computer
Computer Applications
Applications and
and Management,
Management, New
New Delhi-63,
Delhi-63, by Dr. Sunil Pratap Singh
Singh Unit
Unit1‹#›
1
Client Tier
• The client tier consists of application clients that access a Java EE server
and that are usually located on a different machine from the server.
• The server processes the requests and returns a response back to the
client.
©
© Bharati Vidyapeeth’s
Vidyapeeth’s Institute
Institute of
of Computer
Computer Applications
Applications and
and Management,
Management, New
New Delhi-63,
Delhi-63, by Dr. Sunil Pratap Singh
Singh Unit
Unit1‹#›
1
Client Tier: Web Clients
• A web client consists of two parts:
2. a web browser, which renders the pages received from the server
©
© Bharati Vidyapeeth’s
Vidyapeeth’s Institute
Institute of
of Computer
Computer Applications
Applications and
and Management,
Management, New
New Delhi-63,
Delhi-63, by Dr. Sunil Pratap Singh
Singh Unit
Unit1‹#›
1
Client Tier: Application Clients
• An application client runs on a client machine and provides a way for
users to handle tasks that require a richer user interface than can be
provided by a markup language.
• It typically has a GUI created from the Swing or the AWT API.
• Many different types of applications can be Java EE clients, and they are
not always, or even often Java applications.
©
© Bharati Vidyapeeth’s
Vidyapeeth’s Institute
Institute of
of Computer
Computer Applications
Applications and
and Management,
Management, New
New Delhi-63,
Delhi-63, by Dr. Sunil Pratap Singh
Singh Unit
Unit1‹#›
1
Client Tier: Web and Application Clients
©
© Bharati Vidyapeeth’s
Vidyapeeth’s Institute
Institute of
of Computer
Computer Applications
Applications and
and Management,
Management, New
New Delhi-63,
Delhi-63, by Dr. Sunil Pratap Singh
Singh Unit
Unit1‹#›
1
Web Tier
• The web tier consists of components that handle the interaction
between clients and the business tier.
• Its primary tasks are the following:
dynamically generate content in various formats for the client
collect input from users of the client interface and return appropriate
results from the components in the business tier
©
© Bharati Vidyapeeth’s
Vidyapeeth’s Institute
Institute of
of Computer
Computer Applications
Applications and
and Management,
Management, New
New Delhi-63,
Delhi-63, by Dr. Sunil Pratap Singh
Singh Unit
Unit1‹#›
1
Web Components
• Servlets
Servlets are Java programming language classes that dynamically process
requests and construct responses
• JavaServer Pages (JSP)
JSP pages are text-based documents that execute as servlets and define
how dynamic content can be added to static pages, such as HTML pages
• JavaServer Faces
JavaServer Faces technology builds on servlets and JSP technology and
provides a user interface component framework for web applications.
©
© Bharati Vidyapeeth’s
Vidyapeeth’s Institute
Institute of
of Computer
Computer Applications
Applications and
and Management,
Management, New
New Delhi-63,
Delhi-63, by Dr. Sunil Pratap Singh
Singh Unit
Unit1‹#›
1
Web Tier and Java EE Applications
©
© Bharati Vidyapeeth’s
Vidyapeeth’s Institute
Institute of
of Computer
Computer Applications
Applications and
and Management,
Management, New
New Delhi-63,
Delhi-63, by Dr. Sunil Pratap Singh
Singh Unit
Unit1‹#›
1
Business Tier
• The business tier consists of components that provide the business
logic for an application.
©
© Bharati Vidyapeeth’s
Vidyapeeth’s Institute
Institute of
of Computer
Computer Applications
Applications and
and Management,
Management, New
New Delhi-63,
Delhi-63, by Dr. Sunil Pratap Singh
Singh Unit
Unit1‹#›
1
Business Components
• Enterprise JavaBeans
Business code, which is logic that solves or meets the needs of a particular
business domain such as banking, retail, or finance, is handled by
enterprise beans running in the business tier.
An API for accessing data in underlying data stores and mapping that data
to Java programming language objects.
©
© Bharati Vidyapeeth’s
Vidyapeeth’s Institute
Institute of
of Computer
Computer Applications
Applications and
and Management,
Management, New
New Delhi-63,
Delhi-63, by Dr. Sunil Pratap Singh
Singh Unit
Unit1‹#›
1
Business Tier
©
© Bharati Vidyapeeth’s
Vidyapeeth’s Institute
Institute of
of Computer
Computer Applications
Applications and
and Management,
Management, New
New Delhi-63,
Delhi-63, by Dr. Sunil Pratap Singh
Singh Unit
Unit1‹#›
1
Enterprise Information Systems Tier
• The enterprise information systems (EIS) tier consists of database
servers and other legacy data sources.
©
© Bharati Vidyapeeth’s
Vidyapeeth’s Institute
Institute of
of Computer
Computer Applications
Applications and
and Management,
Management, New
New Delhi-63,
Delhi-63, by Dr. Sunil Pratap Singh
Singh Unit
Unit1‹#›
1
Characteristics of n-tier Architecture
• Deployment costs are low
It is much cheaper to deploy and configure software in a controlled server-side
environment than to deploy software on thousands of end user terminals.
• Scalability
The key benefit of three-tier is improved scalability since the application servers
can be deployed on many machines. Also, the database no longer requires a
connection from every client; it only requires connections from a smaller number
of application servers.
©
© Bharati Vidyapeeth’s
Vidyapeeth’s Institute
Institute of
of Computer
Computer Applications
Applications and
and Management,
Management, New
New Delhi-63,
Delhi-63, by Dr. Sunil Pratap Singh
Singh Unit
Unit1‹#›
1
Characteristics of n-tier Architecture
• Business logic migration costs are low
Changing the business logic layer may not necessitate recompiling and
redeploying the client tier
• Maintenance costs are high
You are deploying in three or more physically separate tiers. Software
installation costs, software upgrade costs, redeployment costs, and
administration costs increase significantly
• Improved Security
Security is improved since it can be implemented at multiple levels (not
just the database)
©
© Bharati Vidyapeeth’s
Vidyapeeth’s Institute
Institute of
of Computer
Computer Applications
Applications and
and Management,
Management, New
New Delhi-63,
Delhi-63, by Dr. Sunil Pratap Singh
Singh Unit
Unit1‹#›
1
Characteristics of n-tier Architecture
• Improved Availability
Mission-critical applications can make use of redundant application servers
and redundant database servers. With redundant servers, it is possible to
architect an application so that it can recover from network or server
failures
©
© Bharati Vidyapeeth’s
Vidyapeeth’s Institute
Institute of
of Computer
Computer Applications
Applications and
and Management,
Management, New
New Delhi-63,
Delhi-63, by Dr. Sunil Pratap Singh
Singh Unit
Unit1‹#›
1
MVC
©
© Bharati Vidyapeeth’s
Vidyapeeth’s Institute
Institute of
of Computer
Computer Applications
Applications and
and Management,
Management, New
New Delhi-63,
Delhi-63, by Dr. Sunil Pratap Singh
Singh Unit
Unit1‹#›
1
What is Servlet?
• Servlet can be described in many ways, depending on the context:
Servlet is a class that extend the capabilities of the servers and respond to
the incoming request. It can respond to any type of requests
©
© Bharati Vidyapeeth’s
Vidyapeeth’s Institute
Institute of
of Computer
Computer Applications
Applications and
and Management,
Management, New
New Delhi-63,
Delhi-63, by Dr. Sunil Pratap Singh
Singh Unit
Unit1‹#›
1
Advantages of Servlets
• In order to understand the advantages of servlets, consider a request
for a static web page:
A user enters a Uniform Resource Locator (URL) into a browser.
The HTTP header in the response indicates the type of the content.
The Multipurpose Internet Mail Extensions (MIME) are used for this purpose.
For example, ordinary ASCII text has a MIME type of text/plain. The Hypertext
Markup Language (HTML) source code of a web page has a MIME type of
text/html.
©
© Bharati Vidyapeeth’s
Vidyapeeth’s Institute
Institute of
of Computer
Computer Applications
Applications and
and Management,
Management, New
New Delhi-63,
Delhi-63, by Dr. Sunil Pratap Singh
Singh Unit
Unit1‹#›
1
Advantages of Servlets
• Now consider a request for dynamic web page:
(Assume that an online store uses a database to store information about its
business. This would include items for sale, prices, availability, orders, and so forth. It
wishes to make this information accessible to customers via web pages. The
contents of those web pages must be dynamically generated to reflect the latest
information in the database.)
In the early days of the Web, a server could dynamically construct a page by
creating a separate process to handle each client request.
The process would open connections to one or more databases in order to obtain
the necessary information.
It communicated with the web server via an interface known as the Common
Gateway Interface (CGI).
CGI allowed the separate process to read data from the HTTP request and write
data to the HTTP response.
A variety of different languages were used to build CGI programs. These included
C, C++, and Perl.
©
© Bharati Vidyapeeth’s
Vidyapeeth’s Institute
Institute of
of Computer
Computer Applications
Applications and
and Management,
Management, New
New Delhi-63,
Delhi-63, by Dr. Sunil Pratap Singh
Singh Unit
Unit1‹#›
1
Problems with CGI / Advantages of Servlets
• CGI suffered serious performance problems:
It was expensive in terms of processor and memory resources to create a
separate process for each client request
For each request, it starts a process and Web server is limited to start
processes.
It was also expensive to open and close database connections for each
client request
If number of clients increases, it takes more time for sending response.
In addition, the CGI programs were not platform-independent.
©
© Bharati Vidyapeeth’s
Vidyapeeth’s Institute
Institute of
of Computer
Computer Applications
Applications and
and Management,
Management, New
New Delhi-63,
Delhi-63, by Dr. Sunil Pratap Singh
Singh Unit
Unit1‹#›
1
Advantages of Servlets
• Servlets offer several advantages in comparison with CGI:
Performance is significantly better because it creates a thread for each request
not process
Servlets are platform-independent because they are written in Java
The Java security manager on the server enforces a set of restrictions to protect
the resources on a server machine
The full functionality of the Java class libraries is available to a servlet. It can
communicate with applets, databases, or other software via the sockets and RMI
mechanisms
©
© Bharati Vidyapeeth’s
Vidyapeeth’s Institute
Institute of
of Computer
Computer Applications
Applications and
and Management,
Management, New
New Delhi-63,
Delhi-63, by Dr. Sunil Pratap Singh
Singh Unit
Unit1‹#›
1
Life Cycle of a Servlet
• Consider a typical user scenario to understand life cycle of a servlet:
Second, this HTTP request is received by the web server. The server maps
this request to a particular servlet. The servlet is dynamically retrieved and
loaded into the address space of the server.
Third, the server invokes the init() method of the servlet. This method is
invoked only when the servlet is first loaded into memory. It is possible to
pass initialization parameters to the servlet so it may configure itself.
©
© Bharati Vidyapeeth’s
Vidyapeeth’s Institute
Institute of
of Computer
Computer Applications
Applications and
and Management,
Management, New
New Delhi-63,
Delhi-63, by Dr. Sunil Pratap Singh
Singh Unit
Unit1‹#›
1
Life Cycle of a Servlet
Fourth, the server invokes the service() method of the servlet. This method is
called to process the HTTP request. It is possible for the servlet to read data that
has been provided in the HTTP request. It may also formulate an HTTP response
for the client. The servlet remains in the server’s address space and is available to
process any other HTTP requests received from clients. The service() method is
called for each HTTP request.
Finally, the server may decide to unload the servlet from its memory. The
algorithms by which this determination is made are specific to each server. The
server calls the destroy() method to relinquish any resources such as file handles
that are allocated for the servlet. The memory allocated for the servlet and its
objects can then be garbage collected.
©
© Bharati Vidyapeeth’s
Vidyapeeth’s Institute
Institute of
of Computer
Computer Applications
Applications and
and Management,
Management, New
New Delhi-63,
Delhi-63, by Dr. Sunil Pratap Singh
Singh Unit
Unit1‹#›
1
HTTP (Hyper Text Transfer Protocol)
• The Hypertext Transfer Protocol (HTTP) is application-level protocol
used to establish communication between client and server.
©
© Bharati Vidyapeeth’s
Vidyapeeth’s Institute
Institute of
of Computer
Computer Applications
Applications and
and Management,
Management, New
New Delhi-63,
Delhi-63, by Dr. Sunil Pratap Singh
Singh Unit
Unit1‹#›
1
Characteristics of HTTP
• It is the protocol that allows web servers and browsers to exchange
data over the web.
• It is a request response protocol.
• It uses the reliable TCP connections on port 80.
• It is stateless (each request is considered as the new request).
©
© Bharati Vidyapeeth’s
Vidyapeeth’s Institute
Institute of
of Computer
Computer Applications
Applications and
and Management,
Management, New
New Delhi-63,
Delhi-63, by Dr. Sunil Pratap Singh
Singh Unit
Unit1‹#›
1
HTTP Requests
• The request sent by the computer to a web server that contains all
sorts of potentially interesting information is known as HTTP request.
• The HTTP client sends the request to the server in the form of request
message which includes following main information:
Analysis of source IP address, proxy and port
• POST
In case of post request, large amount of data can be sent because data is sent in body
Post request is secured because data is not exposed in URL bar
Post request cannot be bookmarked
Post request is non-idempotent
Post request is less efficient and used less than get
©
© Bharati Vidyapeeth’s
Vidyapeeth’s Institute
Institute of
of Computer
Computer Applications
Applications and
and Management,
Management, New
New Delhi-63,
Delhi-63, by Dr. Sunil Pratap Singh
Singh Unit
Unit1‹#›
1
A Simple Servlet
import java.io.*;
import javax.servlet.*;
public class HelloServlet extends GenericServlet
{
public void service(ServletRequest request,
ServletResponse response) throws ServletException,
IOException
{
response.setContentType("text/html");
PrintWriter pw = response.getWriter();
pw.println("<B>Hello!");
pw.close();
}
}
©
© Bharati Vidyapeeth’s
Vidyapeeth’s Institute
Institute of
of Computer
Computer Applications
Applications and
and Management,
Management, New
New Delhi-63,
Delhi-63, by Dr. Sunil Pratap Singh
Singh Unit
Unit1‹#›
1
How a Servlet Works
1. User sends request for a servlet by clicking a link that has URL to a
servlet.
©
© Bharati Vidyapeeth’s
Vidyapeeth’s Institute
Institute of
of Computer
Computer Applications
Applications and
and Management,
Management, New
New Delhi-63,
Delhi-63, by Dr. Sunil Pratap Singh
Singh Unit
Unit1‹#›
1
How a Servlet Works
2. The container finds the servlet using deployment descriptor and
creates two objects:
HttpServletRequest
HttpServletResponse
©
© Bharati Vidyapeeth’s
Vidyapeeth’s Institute
Institute of
of Computer
Computer Applications
Applications and
and Management,
Management, New
New Delhi-63,
Delhi-63, by Dr. Sunil Pratap Singh
Singh Unit
Unit1‹#›
1
How a Servlet Works
3. Then the container creates or allocates a thread for that request and
calls the Servlet's service() method and passes the request,
response objects as arguments.
©
© Bharati Vidyapeeth’s
Vidyapeeth’s Institute
Institute of
of Computer
Computer Applications
Applications and
and Management,
Management, New
New Delhi-63,
Delhi-63, by Dr. Sunil Pratap Singh
Singh Unit
Unit1‹#›
1
How a Servlet Works
4. The service() method, then decides which servlet method, doGet() or doPost()
to call, based on HTTP request method (GET, POST, etc.) sent by the client.
©
© Bharati Vidyapeeth’s
Vidyapeeth’s Institute
Institute of
of Computer
Computer Applications
Applications and
and Management,
Management, New
New Delhi-63,
Delhi-63, by Dr. Sunil Pratap Singh
Singh Unit
Unit1‹#›
1
How a Servlet Works
5. Then the Servlet uses response object to write the response back to the
client.
©
© Bharati Vidyapeeth’s
Vidyapeeth’s Institute
Institute of
of Computer
Computer Applications
Applications and
and Management,
Management, New
New Delhi-63,
Delhi-63, by Dr. Sunil Pratap Singh
Singh Unit
Unit1‹#›
1
How a Servlet Works
6. After the service() method is completed the thread dies. And the
request and response objects are ready for garbage collection.
©
© Bharati Vidyapeeth’s
Vidyapeeth’s Institute
Institute of
of Computer
Computer Applications
Applications and
and Management,
Management, New
New Delhi-63,
Delhi-63, by Dr. Sunil Pratap Singh
Singh Unit
Unit1‹#›
1
Servlet API
• Servlet API consists of two important packages that encapsulates all the
important classes and interface, namely:
javax.servlet
javax.servlet.http
©
© Bharati Vidyapeeth’s
Vidyapeeth’s Institute
Institute of
of Computer
Computer Applications
Applications and
and Management,
Management, New
New Delhi-63,
Delhi-63, by Dr. Sunil Pratap Singh
Singh Unit
Unit1‹#›
1
Servlet API: Interfaces of javax.servlet
• Main interfaces of javax.servlet package
Servlet – Declares life cycle methods {init(), service(), and destroy()} for a servlet
RequestDispatcher - Defines an object that receives requests from the client and
sends them to any resource (such as a servlet, HTML file, or JSP file) on the server
©
© Bharati Vidyapeeth’s
Vidyapeeth’s Institute
Institute of
of Computer
Computer Applications
Applications and
and Management,
Management, New
New Delhi-63,
Delhi-63, by Dr. Sunil Pratap Singh
Singh Unit
Unit1‹#›
1
Servlet API: Classes of javax.servlet
• Main classes of javax.servlet package
GenericServlet (Abstract Class)
java.lang.Object
∟javax.servlet.GenericServlet
ServletException
java.lang.Object
∟java.lang.Throwable
∟java.lang.Exception
∟javax.servlet.ServletException
©
© Bharati Vidyapeeth’s
Vidyapeeth’s Institute
Institute of
of Computer
Computer Applications
Applications and
and Management,
Management, New
New Delhi-63,
Delhi-63, by Dr. Sunil Pratap Singh
Singh Unit
Unit1‹#›
1
Servlet Interface
• Servlet Interface
Servlet Interface provides five methods. Out of these five methods, three
methods are servlet life cycle methods and rest two are non life cycle methods.
Methods:
void init(ServletConfig)
void service(ServletRequest, ServletResponse) Servlet Life Cycle Methods
(called by servlet container )
void destroy()
ServletConfig getServletConfig() - Returns a ServletConfig object, which contains
initialization and startup parameters for this servlet.
String getServletInfo() - Returns information about the servlet, such as author, version,
and copyright.
• Note: A servlet container (web container) is a component of web server that interacts with Java servlets. It
provides run time environment for Java applications. It performs many operations including life cycle
management and multithreaded.
©
© Bharati Vidyapeeth’s
Vidyapeeth’s Institute
Institute of
of Computer
Computer Applications
Applications and
and Management,
Management, New
New Delhi-63,
Delhi-63, by Dr. Sunil Pratap Singh
Singh Unit
Unit1‹#›
1
ServletRequest Interface
• Main methods of ServletRequest interface
©
© Bharati Vidyapeeth’s
Vidyapeeth’s Institute
Institute of
of Computer
Computer Applications
Applications and
and Management,
Management, New
New Delhi-63,
Delhi-63, by Dr. Sunil Pratap Singh
Singh Unit
Unit1‹#›
1
ServletResponse Interface
• Main methods of ServletResponse interface
©
© Bharati Vidyapeeth’s
Vidyapeeth’s Institute
Institute of
of Computer
Computer Applications
Applications and
and Management,
Management, New
New Delhi-63,
Delhi-63, by Dr. Sunil Pratap Singh
Singh Unit
Unit1‹#›
1
ServletDispatcher Interface
• Main methods of ServletDispatcher interface
©
© Bharati Vidyapeeth’s
Vidyapeeth’s Institute
Institute of
of Computer
Computer Applications
Applications and
and Management,
Management, New
New Delhi-63,
Delhi-63, by Dr. Sunil Pratap Singh
Singh Unit
Unit1‹#›
1
Working of forward() Method
©
© Bharati Vidyapeeth’s
Vidyapeeth’s Institute
Institute of
of Computer
Computer Applications
Applications and
and Management,
Management, New
New Delhi-63,
Delhi-63, by Dr. Sunil Pratap Singh
Singh Unit
Unit1‹#›
1
Working of include() Method
©
© Bharati Vidyapeeth’s
Vidyapeeth’s Institute
Institute of
of Computer
Computer Applications
Applications and
and Management,
Management, New
New Delhi-63,
Delhi-63, by Dr. Sunil Pratap Singh
Singh Unit
Unit1‹#›
1
Use of forward() and include() Methods
©
© Bharati Vidyapeeth’s
Vidyapeeth’s Institute
Institute of
of Computer
Computer Applications
Applications and
and Management,
Management, New
New Delhi-63,
Delhi-63, by Dr. Sunil Pratap Singh
Singh Unit
Unit1‹#›
1
ServletConfig Interface
• When Web Container initializes a servlet, it creates a ServletConfig object for
servlet.
• ServletConfig object is used to pass information to a servlet during
initialization by getting configuration information from web.xml.
©
© Bharati Vidyapeeth’s
Vidyapeeth’s Institute
Institute of
of Computer
Computer Applications
Applications and
and Management,
Management, New
New Delhi-63,
Delhi-63, by Dr. Sunil Pratap Singh
Singh Unit
Unit1‹#›
1
Example of ServletConfig Program
Name of Parameter
Value of Parameter
©
© Bharati Vidyapeeth’s
Vidyapeeth’s Institute
Institute of
of Computer
Computer Applications
Applications and
and Management,
Management, New
New Delhi-63,
Delhi-63, by Dr. Sunil Pratap Singh
Singh Unit
Unit1‹#›
1
ServletContext Interface
• For every Web Application a ServletContext object is created by the Web
Container.
• ServletContext object is used to get configuration information from web.xml
which will be available to any servlet or JSPs that are part of the Application.
• Main methods of ServletContext interface
String getInitParameter(String) - Returns a String containing the value of the named
context-wide initialization parameter, or null if the parameter does not exist.
void addAttribute(String, Object) - Binds an object to a given attribute name in this servlet
context.
void removeAttribute(String) - Removes the attribute with the given name from the servlet
context.
ServletContext getContext(String uriPath) - Returns a ServletContext object that
corresponds to a specified URL on the server.
String getMimeType (String file) - Returns the MIME type of the specified file, or null if
the MIME type is not known.
©
© Bharati Vidyapeeth’s
Vidyapeeth’s Institute
Institute of
of Computer
Computer Applications
Applications and
and Management,
Management, New
New Delhi-63,
Delhi-63, by Dr. Sunil Pratap Singh
Singh Unit
Unit1‹#›
1
Example of ServletContext Program
<context-param> is for whole application so it is put inside the <web-app>
tag but outside any <servlet>tag
Value of Parameter
Name of Parameter
©
© Bharati Vidyapeeth’s
Vidyapeeth’s Institute
Institute of
of Computer
Computer Applications
Applications and
and Management,
Management, New
New Delhi-63,
Delhi-63, by Dr. Sunil Pratap Singh
Singh Unit
Unit1‹#›
1
ServletContext vs. ServletConfig
©
© Bharati Vidyapeeth’s
Vidyapeeth’s Institute
Institute of
of Computer
Computer Applications
Applications and
and Management,
Management, New
New Delhi-63,
Delhi-63, by Dr. Sunil Pratap Singh
Singh Unit
Unit1‹#›
1
GenericServlet Class
• Main methods of GenericServlet class
void init(ServletConfig) - Called by the servlet container to indicate to a servlet
that the servlet is being placed into service.
void destroy() - Called by the servlet container to indicate to a servlet that the
servlet is being taken out of service.
©
© Bharati Vidyapeeth’s
Vidyapeeth’s Institute
Institute of
of Computer
Computer Applications
Applications and
and Management,
Management, New
New Delhi-63,
Delhi-63, by Dr. Sunil Pratap Singh
Singh Unit
Unit1‹#›
1
Servlet API: Interfaces and Classes of javax.servlet.http
©
© Bharati Vidyapeeth’s
Vidyapeeth’s Institute
Institute of
of Computer
Computer Applications
Applications and
and Management,
Management, New
New Delhi-63,
Delhi-63, by Dr. Sunil Pratap Singh
Singh Unit
Unit1‹#›
1
Methods of HttpServletRequest Interface
ServletRequest
∟HttpServletRequest
• Main methods of HttpServletRequest interface
Cookie [] getCookies() - Returns an array containing all of the Cookie objects
the client sent with this request.
String getMethod() - Returns the name of the HTTP method with which this
request was made, for example, GET, POST, etc.
©
© Bharati Vidyapeeth’s
Vidyapeeth’s Institute
Institute of
of Computer
Computer Applications
Applications and
and Management,
Management, New
New Delhi-63,
Delhi-63, by Dr. Sunil Pratap Singh
Singh Unit
Unit1‹#›
1
Methods of HttpServletResponse Interface
ServletResponse
∟HttpServletResponse
• Main methods of HttpServletResponse interface
void addCookie(Cookie ck) - Adds the specified cookie to the response.
void addHeader(String name, String value) - Adds a response header with the
given name and value.
void sendError(int sc) - Sends an error response to the client using the
specified status code.
©
© Bharati Vidyapeeth’s
Vidyapeeth’s Institute
Institute of
of Computer
Computer Applications
Applications and
and Management,
Management, New
New Delhi-63,
Delhi-63, by Dr. Sunil Pratap Singh
Singh Unit
Unit1‹#›
1
Methods of HttpServlet Class
Object
∟GenericServlet
∟HttpServlet (Abstract Class) implements Servlet, ServletConfig
©
© Bharati Vidyapeeth’s
Vidyapeeth’s Institute
Institute of
of Computer
Computer Applications
Applications and
and Management,
Management, New
New Delhi-63,
Delhi-63, by Dr. Sunil Pratap Singh
Singh Unit
Unit1‹#›
1
Session Management
• Managing Session in Servlet
Session is a conversional state between client and server and it can
consists of multiple request and response between client and server.
As HTTP is a stateless protocol, all requests and responses are
independent.
Sometimes we need to keep track of client's activity across multiple
requests.
For eg., when a user logs into a website, not matter on which web page he visits after
logging in, his credentials will be with the server, until he logs out.
HttpSession
URL Rewriting
©
© Bharati Vidyapeeth’s
Vidyapeeth’s Institute
Institute of
of Computer
Computer Applications
Applications and
and Management,
Management, New
New Delhi-63,
Delhi-63, by Dr. Sunil Pratap Singh
Singh Unit
Unit1‹#›
1
Working of Session
• Whenever a user starts using application, a unique identification information
about him/her can be saved in an object which is available throughout the
application, until its destroyed.
©
© Bharati Vidyapeeth’s
Vidyapeeth’s Institute
Institute of
of Computer
Computer Applications
Applications and
and Management,
Management, New
New Delhi-63,
Delhi-63, by Dr. Sunil Pratap Singh
Singh Unit
Unit1‹#›
1
Using Cookies for Session Management
• Cookies are small pieces of information that are sent in response from the
web server to the client.
• Cookies are the simplest technique used for storing client state.
• Cookies are stored on client's computer. They have a lifespan and are
destroyed by the client browser at the end of that lifespan.
• Cookies are created using Cookie class present in Servlet API.
• Cookies are added to response object using the addCookie() method. This
method sends cookie information over the HTTP response stream.
• getCookies() method is used to access the cookies that are added to response
object.
Using Cookies for storing client state has one shortcoming though, if the client has turned
off Cookie saving settings in his browser then, client state can never be saved.
©
© Bharati Vidyapeeth’s
Vidyapeeth’s Institute
Institute of
of Computer
Computer Applications
Applications and
and Management,
Management, New
New Delhi-63,
Delhi-63, by Dr. Sunil Pratap Singh
Singh Unit
Unit1‹#›
1
Using Cookies for Session Management
• Creating a new Cookie (Example)
response.addCookie(ck);
©
© Bharati Vidyapeeth’s
Vidyapeeth’s Institute
Institute of
of Computer
Computer Applications
Applications and
and Management,
Management, New
New Delhi-63,
Delhi-63, by Dr. Sunil Pratap Singh
Singh Unit
Unit1‹#›
1
Program of Cookie
©
© Bharati Vidyapeeth’s
Vidyapeeth’s Institute
Institute of
of Computer
Computer Applications
Applications and
and Management,
Management, New
New Delhi-63,
Delhi-63, by Dr. Sunil Pratap Singh
Singh Unit
Unit1‹#›
1
Program of Cookie
©
© Bharati Vidyapeeth’s
Vidyapeeth’s Institute
Institute of
of Computer
Computer Applications
Applications and
and Management,
Management, New
New Delhi-63,
Delhi-63, by Dr. Sunil Pratap Singh
Singh Unit
Unit1‹#›
1
Program of Cookie
©
© Bharati Vidyapeeth’s
Vidyapeeth’s Institute
Institute of
of Computer
Computer Applications
Applications and
and Management,
Management, New
New Delhi-63,
Delhi-63, by Dr. Sunil Pratap Singh
Singh Unit
Unit1‹#›
1
Using HttpSession for Session Management
©
© Bharati Vidyapeeth’s
Vidyapeeth’s Institute
Institute of
of Computer
Computer Applications
Applications and
and Management,
Management, New
New Delhi-63,
Delhi-63, by Dr. Sunil Pratap Singh
Singh Unit
Unit1‹#›
1
Working of HttpSession
• On client's first request, the Web Container generates a unique session ID and
gives it back to the client with response. This is a temporary session created
by web container.
• The client sends back the session ID with each request. Making it easier for
the web container to identify where the request is coming from.
• The Web Container uses this ID, finds the matching session with the ID and
associates the session with the request.
©
© Bharati Vidyapeeth’s
Vidyapeeth’s Institute
Institute of
of Computer
Computer Applications
Applications and
and Management,
Management, New
New Delhi-63,
Delhi-63, by Dr. Sunil Pratap Singh
Singh Unit
Unit1‹#›
1
Using HttpSession for Session Management
• Creating a new session:-
HttpSession session = request.getSession();
getSession() returns a session . If the session already exist, it returns the
existig else creates a new session.
HttpSession session = request.getSession(true);
getSession(true) always returns a new session.
©
© Bharati Vidyapeeth’s
Vidyapeeth’s Institute
Institute of
of Computer
Computer Applications
Applications and
and Management,
Management, New
New Delhi-63,
Delhi-63, by Dr. Sunil Pratap Singh
Singh Unit
Unit1‹#›
1
Program of HttpSession
©
© Bharati Vidyapeeth’s
Vidyapeeth’s Institute
Institute of
of Computer
Computer Applications
Applications and
and Management,
Management, New
New Delhi-63,
Delhi-63, by Dr. Sunil Pratap Singh
Singh Unit
Unit1‹#›
1
Program of HttpSession
©
© Bharati Vidyapeeth’s
Vidyapeeth’s Institute
Institute of
of Computer
Computer Applications
Applications and
and Management,
Management, New
New Delhi-63,
Delhi-63, by Dr. Sunil Pratap Singh
Singh Unit
Unit1‹#›
1
Program of HttpSession
©
© Bharati Vidyapeeth’s
Vidyapeeth’s Institute
Institute of
of Computer
Computer Applications
Applications and
and Management,
Management, New
New Delhi-63,
Delhi-63, by Dr. Sunil Pratap Singh
Singh Unit
Unit1‹#›
1
Using URL Rewriting for Session Management
©
© Bharati Vidyapeeth’s
Vidyapeeth’s Institute
Institute of
of Computer
Computer Applications
Applications and
and Management,
Management, New
New Delhi-63,
Delhi-63, by Dr. Sunil Pratap Singh
Singh Unit
Unit1‹#›
1
Program of URL Rewriting
©
© Bharati Vidyapeeth’s
Vidyapeeth’s Institute
Institute of
of Computer
Computer Applications
Applications and
and Management,
Management, New
New Delhi-63,
Delhi-63, by Dr. Sunil Pratap Singh
Singh Unit
Unit1‹#›
1
Using Hidden Form Field for Session Management
• In case of hidden form field, a hidden field is used to store client state.
• In this case user information is stored in hidden field value and retrieved
from another servlet.
• Advantages:
Does not have to depend on browser whether the cookie is disabled or not.
Inserting a simple HTML Input field of type hidden is required. Hence, its easier to
implement.
• Disadvantages:
©
© Bharati Vidyapeeth’s
Vidyapeeth’s Institute
Institute of
of Computer
Computer Applications
Applications and
and Management,
Management, New
New Delhi-63,
Delhi-63, by Dr. Sunil Pratap Singh
Singh Unit
Unit1‹#›
1
Hidden Form Field: Working
©
© Bharati Vidyapeeth’s
Vidyapeeth’s Institute
Institute of
of Computer
Computer Applications
Applications and
and Management,
Management, New
New Delhi-63,
Delhi-63, by Dr. Sunil Pratap Singh
Singh Unit
Unit1‹#›
1
Hidden Form Field: Example
protected void doPost(HttpServletRequest request, HttpServletResponse
response) throws ServletException, IOException
{
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
©
© Bharati Vidyapeeth’s
Vidyapeeth’s Institute
Institute of
of Computer
Computer Applications
Applications and
and Management,
Management, New
New Delhi-63,
Delhi-63, by Dr. Sunil Pratap Singh
Singh Unit
Unit1‹#›
1
Bibliography
• https://docs.oracle.com
• http://www.studytonight.com
• https://www.javatpoint.com
• https://way2java.com
• “Inside Servlets” by Dustin R. Callaway
• “Enterprise Java Computing: Applications and Architectures” by Govind Sesadari
©
© Bharati Vidyapeeth’s
Vidyapeeth’s Institute
Institute of
of Computer
Computer Applications
Applications and
and Management,
Management, New
New Delhi-63,
Delhi-63, by Dr. Sunil Pratap Singh
Singh Unit
Unit1‹#›
1