0% found this document useful (0 votes)
44 views26 pages

Unit - 4 JSP AND SERVLET

The document provides an introduction to Java Servlets, detailing their life cycle, development options, and the Servlet API, which includes essential interfaces and classes. It explains how servlets extend web server functionality and handle HTTP requests and responses, including the use of cookies and session tracking. Additionally, it covers the creation and compilation of servlets, as well as examples of handling HTTP GET and POST requests.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
44 views26 pages

Unit - 4 JSP AND SERVLET

The document provides an introduction to Java Servlets, detailing their life cycle, development options, and the Servlet API, which includes essential interfaces and classes. It explains how servlets extend web server functionality and handle HTTP requests and responses, including the use of cookies and session tracking. Additionally, it covers the creation and compilation of servlets, as well as examples of handling HTTP GET and POST requests.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 26

1

Unit-4

Introducing Servlets

1 Background
2 The Life Cycle of a Servlet
Servlet Development Options
Using Tomcat
3 A Simple Servlet
3.a Create and Compile the Servlet Source Code
3.b Start Tomcat
3.c Start a Web Browser and Request the Servlet
4 The Servlet API
5 The javax.servlet Package
A The Servlet Interface
B The ServletConfig Interface
C The ServletContext Interface
D The ServletRequest Interface
E The ServletResponse Interface
F The GenericServlet Class
G The ServletInputStream Class
H The ServletOutputStream Class
I The Servlet Exception Classes
6 Reading Servlet Parameters
7 The javax.servlet.http Package
A The HttpServletRequest Interface
B The HttpServletResponse Interface
C The HttpSession Interface
D The Cookie Class
E The HttpServlet Class
8 Handling HTTP Requests and Responses
a Handling HTTP GET Requests
b Handling HTTP POST Requests
9 Using Cookies
10 Session Tracking
11 Introduction to JSP
a Using JSP
b Comparing JSP with Servlet
c Java Web Frameworks
https://ctal-advancejava.blogspot.com/
2

Servlets are small programs that execute on the server side of a Web connection. Just as applets dynamically
extend the functionality of a Web browser, servlets dynamically extend the functionality of a Web server.

A servlet is a Java programming language class used to extend the capabilities of servers that host applications
accessed via a request-response programming model. Although servlets can respond to any type of request, they
are commonly used to extend the applications hosted by Web servers. For such applications, Java Servlet
technology defines HTTP-specific servlet classes.The javax.servlet and javax.servlet.http packages provide
interfaces and classes for writing servlets. All servlets must implement the Servlet interface, which defines life-
cycle methods.

The Life Cycle of a Servlet


Three methods are central to the life cycle of a servlet. These are init( ), service( ), and destroy( ). They are implemented
by every servlet and are invoked at specific times by the server. Let us consider a typical user scenario to understand
when these methods are called.

First, when a user enters a Uniform Resource Locator (URL) to a Web browser. The browser then generates an HTTP
request for this URL. This request is then sent to the appropriate server.

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.

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 server calls the destroy( ) method to
relinquish any resources such as file handles that are allocated for the servlet. Important data may be saved to a
persistent store. The memory allocated for the servlet and its objects can then be garbage collected.

4. The Servlet API


Two packages contain the classes and interfaces that are required to build servlets. These are javax.servlet and
javax.servlet.http. They constitute the Servlet API.These packages are not part of the Java core packages. Instead, they
are standard extensions. Therefore, they are not included in the Java Software Development Kit. You must download
Tomcat or Glass Fish server to obtain their functionality.

The javax.servlet Package


The javax.servlet package contains a number of interfaces and classes that establish the framework in which servlets
operate.
Er. Sital Pd. Mandal https://ctal-advancejava.blogspot.com/
3
The following table summarizes the core interfaces that are provided in this package. The most significant of these is
Servlet. All servlets must implement this interface or extend a class that implements the interface.
The ServletRequest and ServletResponse interfaces are also very important.

Interface Description
Servlet Declares life cycle methods for a
servlet.
ServletConfig Allows servlets to get initialization
parameters.
ServletContext Enables servlets to log events and
access information about their
environment.
ServletRequest Used to read data from a client
request.
ServletResponse Used to write data to a client
response.

The following table summarizes the core classes that are provided in the javax.servlet package.

Class Description
GenericServlet Implements the Servlet and
ServletConfig interfaces.
ServletInputStream Provides an input stream for reading
requests from a client.
ServletOutputStream Provides an output stream for writing
responses to a client.
ServletException Indicates a servlet error occurred.
UnavailableException Indicates a servlet is unavailable.

The Servlet Interface


All servlets must implement the Servlet interface. It declares the init( ), service( ), and destroy( ) methods that are
called by the server during the life cycle of a servlet. The methods defined by Servlet are shown below:

Er. Sital Pd. Mandal https://ctal-advancejava.blogspot.com/


4

The ServletConfig Interface


The ServletConfig interface allows a servlet to obtain configuration data when it
is loaded. The methods declared by this interface are summarized here:

The ServletContext Interface


The ServletContext interface enables servlets to obtain information about their
environment. Several of its methods are summarized in Table 38-2.

The ServletRequest Interface


The ServletRequest interface enables a servlet to obtain information about a
client request. Several of its methods are summarized in Table 38-3.

Er. Sital Pd. Mandal https://ctal-advancejava.blogspot.com/


5

Table 38-3 Various Methods Defined by ServletRequest

The ServletResponse Interface


The ServletResponse interface enables a servlet to formulate a response for a
client. Several of its methods are summarized in Table 31-4.

Er. Sital Pd. Mandal https://ctal-advancejava.blogspot.com/


6

The GenericServlet Class


The GenericServlet class provides implementations of the basic life cycle methods for a servlet.
GenericServlet implements the Servlet and ServletConfig interfaces. In addition, a method to
append a string to the server log file is available. The signatures of this method are shown here:
void log(String s)
void log(String s, Throwable e)
Here, s is the string to be appended to the log, and e is an exception that occurred.

The ServletInputStream Class


The ServletInputStream class extends InputStream. It is implemented by the
servlet container and provides an input stream that a servlet developer can use
to read the data from a client request. In addition to the input methods inherited
from InputStream, a method is provided to read bytes from the stream. It is
shown here:

int readLine(byte[ ] buffer, int offset, int size) throws IOException

Here, buffer is the array into which size bytes are placed starting at offset. The
method returns the actual number of bytes read or –1 if an end-of-stream
condition is encountered.

The ServletOutputStream Class


The ServletOutputStream class extends OutputStream. It is implemented by
the servlet container and provides an output stream that a servlet developer can
use toEr.write
Sital Pd. Mandal https://ctal-advancejava.blogspot.com/
data to a client response. In addition to the output methods provided
7
by OutputStream, it also defines the print( ) and println( ) methods, which
output data to the stream.

The Servlet Exception Classes


javax.servlet defines two exceptions. The first is ServletException, which
indicates that a servlet problem has occurred. The second is
UnavailableException, which extends ServletException. It indicates that a
servlet is unavailable.

Reading Servlet Parameters

The ServletRequest class includes methods that allow to read the names and values of parameters that are included in
a client request. We will develop a servlet that illustrates their use. The example contains two files. A Web page is
defined in index.jsp and a servlet is defined in PostParametersServlet.java. The HTML source code for index.jsp is
shown in the following listing. It defines a table that contains two labels and two text fields. One of the labels is
Employee and the other is Phone. There is also a submit button. Notice that the action parameter of the form tag
specifies a URL. The URL identifies the servlet to process the HTTP POST request.

//index.jsp

<html>
<body>
<center>
<form name="Form1"
method="post"
action="http://localhost:8080/servlets-examples/
servlet/PostParametersServlet">
<table>
<tr>
<td><B>Employee</td>
<td><input type=textbox name="e" size="25" value=""></td>
</tr>
<tr>
<td><B>Phone</td>
<td><input type=textbox name="p" size="25" value=""></td>
</tr>
</table>
<input type=submit value="Submit">
</body>
</html>

Er. Sital Pd. Mandal https://ctal-advancejava.blogspot.com/


8

//PostParametersServlet.java
import java.io.*;
import java.util.*;
import javax.servlet.*;
public class PostParametersServlet
extends GenericServlet {
public void service(ServletRequest request,
ServletResponse response)
throws ServletException, IOException {
// Get print writer.
PrintWriter pw = response.getWriter();
// Get enumeration of parameter names.
Enumeration e = request.getParameterNames();
// Display parameter names and values.
while(e.hasMoreElements()) {
String pname = (String)e.nextElement();
pw.print(pname + " = ");
String pvalue = request.getParameter(pname);
pw.println(pvalue);
}
pw.close();
}
}

output
e = navin
p = 9841

The javax.servlet.http Package

The javax.servlet.http package contains a number of interfaces and classes that are commonly used by servlet
developers. You will see that its functionality makes it easy to build servlets that work with HTTP requests and
responses.

The following table summarizes the core interfaces that are provided in this package:

Er. Sital Pd. Mandal https://ctal-advancejava.blogspot.com/


9
The following table summarizes the core classes that are provided in this package. The most important of these is
HttpServlet. Servlet developers typically extend this class in order to process HTTP requests.

The HttpServletRequest Interface


The HttpServletRequest interface is implemented by the server. It enables a servlet to obtain information about a client
request. Several of its methods are shown in Table below.

Er. Sital Pd. Mandal https://ctal-advancejava.blogspot.com/


10

The HttpServletResponse Interface


The HttpServletResponse interface is implemented by the server. It enables a servlet to formulate an HTTP response to a
client. Several constants are defined. These correspond to the different status codes that can be assigned to an HTTP
response. For example, SC_OK indicates that the HTTP request succeeded and SC_NOT_FOUND indicates that the
requested resource is not available. Several methods of this interface are summarized in Table below.

The Cookie Class

• The Cookie class encapsulates a cookie.


• A cookie is stored on a client and contains state information.
• Cookies are valuable for tracking user activities.

For example, assume that a user visits an online store. A cookie can save the user’s name, address, and other
information. The user does not need to enter this data each time he or she visits the store.

A servlet can write a cookie to a user’s machine via the addCookie( ) method of the HttpServletResponse interface.

The data for that cookie is then included in the header of the HTTP response that is sent to the browser.

The names and values of cookies are stored on the user’s machine. Some of the information that is saved for each
Er. Sitalthe
cookie includes Pd.following:
Mandal https://ctal-advancejava.blogspot.com/
11
o The name of the cookie
o The value of the cookie
o The expiration date of the cookie
o The domain and path of the cookie

The expiration date determines when this cookie is deleted from the user’s machine. If an expiration date is not
explicitly assigned to a cookie, it is deleted when the current browser session ends. Otherwise, the cookie is saved in a
file on the user’s machine.

The domain and path of the cookie determine when it is included in the header of an HTTP request. If the user enters a
URL whose domain and path match these values, the cookie is then supplied to the Web server. Otherwise, it is not.
The methods of the Cookie class are summarized in Table below:

The HttpServlet Class


The HttpServlet class extends GenericServlet. It is commonly used when developing servlets that receive and process
HTTP requests. The methods of the HttpServlet class are summarized in Table below.

Er. Sital Pd. Mandal https://ctal-advancejava.blogspot.com/


12

TABLE 31-9 The Methods Defined by HttpServlet

Handling HTTP Requests and Responses

The HttpServlet class provides specialized methods that handle the various types of HTTP requests. A servlet
developer typically overrides one of these methods. These methods are doDelete( ), doGet( ), doHead( ),
doOptions( ), doPost( ), doPut( ), and doTrace( ).

Er. Sital Pd. Mandal https://ctal-advancejava.blogspot.com/


13
Handling HTTP GET Requests
The servlet is invoked when a form on a web page is submitted. The example contains two files.

A web page is defined in ColorGet.html.

A servlet is defined in ColorGetServlet.java.

Compile the servlet. Next, copy it to the appropriate directory, and update the web.xml file. Then, perform
these steps to test this example:
1. Start Tomcat, if it is not already running.
2. Display the web page in a browser.
3. Select a color.
4. Submit the web page.

Assume that the user selects the red option and submits the form. The URL sent from the browser to the server
is The characters
Er. Sital Pd.toMandal
the right of the question mark are known as the query string.
https://ctal-advancejava.blogspot.com/
14

Handling HTTP POST Requests


The servlet is invoked when a form on a web page is submitted. The example contains two files. A web page is
defined in ColorPost.html.

A servlet is defined in ColorPostServlet.java.

Er. Sital Pd. Mandal https://ctal-advancejava.blogspot.com/


15
Using Cookies
The example contains three files as summarized here:

AddCookie.html

AddCookieServlet.java

Er. Sital Pd. Mandal https://ctal-advancejava.blogspot.com/


16
GetCookiesServlet.java

Compile the servlets. Next, copy them to the appropriate directory, and update the web.xml file. Then, perform
these steps to test this example:

1. Start Tomcat, if it is not already running.


2. Display AddCookie.html in a browser.
3. Enter a value for MyCookie.
4. Submit the web page.

Next, request the following URL via the browser:

Er. Sital Pd. Mandal https://ctal-advancejava.blogspot.com/


17

Session Tracking
A session can be created via the getSession( ) method of HttpServletRequest. An HttpSession object is
returned. This object can store a set of bindings that associate names with objects.

The setAttribute( ), getAttribute( ), getAttributeNames( ), and removeAttribute( ) methods of HttpSession


manage these bindings. Session state is shared by all servlets that are associated with a client.

The following servlet illustrates how to use session state:

That object is a Date object that encapsulates the date and time when this page was last accessed. A Date object
encapsulating the current date and time is then created. The setAttribute( ) method is called to bind the name
"date" to this object.

When you first request this servlet, the browser displays one line with the current date and time information. On
subsequent invocations, two lines are displayed. The first line shows the date and time when the servlet was last
accessed. The second line shows the current date and time.

Er. Sital Pd. Mandal https://ctal-advancejava.blogspot.com/


18

11 Introduction to JSP
• It stands for Java Server Pages.
• It is a server side technology.
• It is used for creating web application.
• It is used to create dynamic web content.
• In this JSP tags are used to insert JAVA code into HTML pages.
• It is an advanced version of Servlet Technology.
• It is a Web based technology helps us to create dynamic and platform independent web
pages.
• In this, Java code can be inserted in HTML/ XML pages or both.
• JSP is first converted into servlet by JSP container before processing the client’s request.

Comparing JSP with Servlet

Sr. No. Key Servlet JSP

Implementation Servlet is developed on Java JSP is primarily written in HTML


1 language. language although Java code
could also be written.

MVC In contrast to MVC we can state On the other hand, JSP plays
servlet as a controller which the role of view to render the
2
receives the request process and response returned by the
send back the response. servlet.

Request type Servlets can accept and process all JSP on the other hand is
3 type of protocol requests. compatible with HTTP request
only.

Session In Servlet by default session On the other hand in JSP


4 Management management is not enabled, the session management is
user has to enable it explicitly. automatically enabled.

Performance Servlet is faster than JSP. JSP is slower than Servlet


because first the translation of
5
JSP to java code is taking place
and then compiles.

Modification Modification in Servlet is a time- On the other hands JSP


reflected consuming task because it includes modification is fast as just need
6 reloading, recompiling and restarting to click the refresh button and
the server as we made any change code change would get
in our code to get reflected. reflected.

Er. Sital Pd. Mandal https://ctal-advancejava.blogspot.com/


19
JSP pages are more advantageous than Servlet:

• They are easy to maintain.


• No recompilation or redeployment is required.
• JSP has access to entire API of JAVA .
• JSP are extended version of Servlet.

Features of JSP
• Coding in JSP is easy :- As it is just adding JAVA code to HTML/XML.
• Reduction in the length of Code :- In JSP we use action tags, custom tags etc.
• Connection to Database is easier :-It is easier to connect website to database and allows to
read or write data easily to the database.
• Make Interactive websites :- In this we can create dynamic web pages which helps user to
interact in real time environment.
• Portable, Powerful, flexible and easy to maintain :- as these are browser and server
independent.
• No Redeployment and No Re-Compilation :- It is dynamic, secure and platform
independent so no need to re-compilation.
• Extension to Servlet :- as it has all features of servlets, implicit objects and custom tags

Why Use JSP?

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.
Finally, JSP is an integral part of Java EE, a complete platform for enterprise class applications. This
means that JSP can play a part in the simplest applications to the most complex and demanding.

Er. Sital Pd. Mandal https://ctal-advancejava.blogspot.com/


20
Using JSP
JSP syntax
Syntax available in JSP are following:
1. Declaration Tag :-It is used to declare variables.
Syntax:-
<%! Dec var %>
Example:-
<%! int var=10; %>

2. Java Scriplets :- It allows us to add any number of JAVA code, variables and expressions.
Syntax:-
<% java code %>

3. JSP Expression :- It evaluates and convert the expression to a string.


Syntax:-
<%= expression %>

Example:-
<% num1 = num1+num2 %>

4. JAVA Comments :- It contains the text that is added for information which has to be ignored.
Syntax:-
<% -- JSP Comments %>

Process of Execution
Steps for Execution of JSP are following:-
• Create html page from where request will be sent to server eg try.html.
• To handle to request of user next is to create .jsp file Eg. new.jsp
• Create project folder structure.
• Create XML file eg my.xml.
• Create WAR file.
• Start Tomcat
• Run Application

Er. Sital Pd. Mandal https://ctal-advancejava.blogspot.com/


21
Your First JSP

Let’s start learning JSP with a simple JSP.

<%-- JSP comment --%>


<HTML>
<HEAD>
<TITLE>MESSAGE</TITLE>
</HEAD>
<BODY>
<%out.print("Hello, Sample JSP code");%>
</BODY>
</HTML>

The above JSP generates the following output:


Hello, Sample JSP code.

Your Second JSP

HTML>
<BODY>
Hello BeginnersBook Readers!
Current time is: <%= new java.util.Date() %>
</BODY>
</HTML>

The Directory structure of JSP

The directory structure of JSP page is same as Servlet. We contain the JSP page outside
the WEB-INF folder or in any directory.

Er. Sital Pd. Mandal https://ctal-advancejava.blogspot.com/


22
Architecture of a JSP Application
1) Model1 Architecture: In this Model, JSP plays a key role and it is responsible for of processing
the request made by client.

2) Model2 Architecture: In this Model, Servlet plays a major role and it is responsible for processing
the client’s(web browser) request.

Java Web Frameworks


What is Framework in Java

Java Framework is the body or platform of pre-written codes used by Java developers
to develop Java applications or web applications. In other words, Java Framework is a
collection of predefined classes and functions that is used to process input, manage
hardware devices interacts with system software. It acts like a skeleton that helps the
developer to develop an application by writing their own code.

What is a framework?

Framework are the bodies that contains the pre-written codes (classes and functions) in
which we can add our code to overcome the problem. We can also say that frameworks
use programmer's code because the framework is in control of the programmer. We can
use the framework by calling its methods, inheritance, and supplying "callbacks",
listeners, or Pd.
Er. Sital other implementations
Mandal of the Observer pattern.
https://ctal-advancejava.blogspot.com/
23
Popular Java Frameworks

Some of the most popular Java frameworks are:

o Spring
o Hibernate
o Grails
o Play
o JavaServer Faces (JSF)
o Google Web Toolkit (GWT)
o Quarkus

Spring

It is a light-weighted, powerful Java application development framework. It is used


for JEE. Its other modules are Spring Security, Spring MVC, Spring Batch, Spring
ORM, Spring Boot and Spring Cloud etc.

Advantages

o Loose coupling
o Lightweight
o Fast Development
o Powerful abstraction
o Easy to test

Hibernate

Hibernate is ORM (Object-Relation Mapping) framework that allows us establish


communication between Java programming language and the RDBMS.

Advantages

o Portability, productivity, maintainability.


o Open source framework.
o It avoids repetitive code from the JDBC API.

Grails

It is a dynamic framework created by using the Groovy programming language. It is an


OOPs language. Its purpose is to enhance the productivity. The syntax of Grails is
Er. Sital Pd. Mandal https://ctal-advancejava.blogspot.com/
24
matched with Java and the code is compiled to JVM. It also works with Java, JEE, Spring
and Hibernate.

Advantages

o It uses Groovy programming standard instead of Java programming standard


because Groovy is similar to Java.
o Its object mapping feature is easy to use.
o It provides the facility of reusing the code (in the form of plugin) between different
Grail applications.
o Provides flexible profiles.

Play

It is a unique Java framework because it does not follow JEE standards. It follows MVC
architecture pattern. It is used when we want to develop highly scalable Java
application. Using Play framework, we can develop lightweight and web-friendly Java
application for both mobile and desktop.

Advantages

o No configuration is required.
o It enhances the developer productivity and target the RESTful architectures.
o It offers hot code reload and error message in the browser.
o It also supports popular IDEs.

JavaServer Faces

It stands for JavaServer Faces. It is a component-based UI framework developed by


Oracle that is used to build user interfaces for Java-based applications. It follows MVC
design pattern. The application developed using JSF has an architecture that defines a
distinction

Advantages

o It is an important part of JEE.


o Provides rich libraries.

Google Web Toolkit (GWT)

It is an open-source framework that allows developers to write client-side Java code.


With the help of GWT, we can rapidly develop complex browse application. The
advantages to use GWT is that we can easily develop and debug Ajax application. the
productEr. of Google,
Sital such as Google
Pd. Mandal AdSense, Blogger are developed using GWT.
https://ctal-advancejava.blogspot.com/
25
Advantages

o It employs reusability for web application development


o We can use Google API to develop application.
o It provides functionality such as, internationalization, UI abstraction, and history
management.

Quarkus

It is a modern, full-stack, and Kubernetes-native Java framework. It offers small


memory footprint and reduced boot time. It works well if the infrastructure is cloud-
native. It optimizes Java specifically for Kubernetes and enables it to become an
effective platform for serverless, cloud, and Kubernetes environments.

Advantages

o It is used in cloud, containers, and serverless environments.


o It supports microservices architecture and development.
o The developers are free to choose own development model.
o It is compatible with popular frameworks like, Eclipse, Spring Dependency
Injection, and Hibernate.

Advantages of Java Frameworks

The advantages of the Java Frameworks are as follows:

o Security: It is the most important advantage of the Java framework. If we found


any security loop hole or vulnerability in an application, we can directly move to
the official website of the framework to fix the security related issues.
o Support: The widely used framework provides large forums or groups where we
can put our problem for the solution. It also provides the documentation of the
framework that helps us to understand the working of the framework.
o Efficiency: If we are doing a task without using the framework, it may take time
to complete. But if we are doing the same work by using the framework, we can
complete that task in easy and fast way. Therefore, using the Java framework
development become faster, easier and effective. It also saves time and efforts.
o Expenses: Another advantage of using the framework is to reduce the cost of the
application. Because the maintenance cost of the framework is low.

Er. Sital Pd. Mandal https://ctal-advancejava.blogspot.com/


26
Examples of Frameworks in Java

In Java, Collection is an example of the framework. It reduces the programming efforts


because it provides useful data structure and algorithms. It is referred to as library that
do not provides inversion of control.

Another example of framework is, Swing and AWT classes. Swing is a GUI based
framework used to develop windows-based application. It contains a large number of
interfaces. There is inversion of control because of listeners.

Framework vs. Library

Library Framework

Library is the collection of frequently Framework is the collection of libraries.


used, pre-compiled classes.

It is a set of reusable functions used by It is a piece of code that dictates the


computer programs. architecture of your project and aids in
programs.

You are in full control when you call a The code never calls into a framework,
method from a library and the control is instead the framework calls you.
then returned.

It is in corporate seamlessly into It cannot be seamlessly incorporated into


existing projects to add functionality an existing project. Instead it can be used
that you can access using an API. when a new project is started.

They are important in program for They provide a standard way to build and
linking and binding process. deploy applications.

Libraries do no employ an inverted flow Framework employs an inverted flow of


of control between itself and its clients. control between itself and its clients.

Example: jQuery is a JavaScript library Example: Angular JS is a JavaScript-


that simplifies DOM manipulation. based framework for dynamic web
applications.

Reference:

https://www3.ntu.edu.sg/home/ehchua/programming/java/JSPByExample.html
Er. Sital Pd. Mandal https://ctal-advancejava.blogspot.com/

You might also like