0% found this document useful (0 votes)
8 views

Slot 1 - Introduction Java Web Application

ndsfdsfsdfsdfsd

Uploaded by

Phuc Vinh
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)
8 views

Slot 1 - Introduction Java Web Application

ndsfdsfsdfsdfsd

Uploaded by

Phuc Vinh
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/ 41

ĐẠI HỌC FPT CẦN THƠ

Introduction Java Web Application


Objectives

Explain the purpose of different applications

Explain Web applications and their advantages

Describe the architecture and components of Web application

Describe the role of HTTP protocol and its methods used for accessing Web pages

Explain the use of Common Gateway Interface (CGI) language

Explain the different types of components used in developing Web application

Introduction Java Web Application – Huong Hoang Luong 2


Objectives

Explain the advantages and disadvantages of Servlets

Explain the services provided by a Web container

Describe the life cycle and directory structure of a Web application

Explain how to package Web application

Develop a Web application in NetBeans Integrated Development Environment


(IDE)

Introduction Java Web Application – Huong Hoang Luong 33


Introduction

❖ An application is a collection of programs designed to perform a particular


task.
❖ Desktop Application
❑ Runs only in a local machine.

❑ Can neither be viewed nor be controlled by other users.

❑ Example: Microsoft Word.

❑ Figure depicts the desktop application.

Introduction Java Web Application – Huong Hoang Luong 4


Introduction
❖ Networking Application
❑ Runs in a Local Area Network (LAN), Metropolitan Area Network (MAN),

or World Area Network (WAN).


❑ Can be viewed and controlled by users in that particular network only.

❑ Example: Novell Netware.

❑ Figure depicts the networking application.

❖ Web Application
❑ Runs at a remote location.

❑ Can be viewed and controlled by all the users having administrative or

equivalent privilege, throughout the globe, at any instance of time.


❑ Example: Internet mail services, such as www.yahoo.com.

❑ Figure depicts the Web application.

Introduction Java Web Application – Huong Hoang Luong 5


Web Application

❖ Web Application:
❑ Is a software application that runs on a Web server.
❑ Consists of Web pages which can be static and dynamic.
❖ Static Web pages:
❑ Are created using Web technologies such as HyperText Markup Language
(HTML), Cascading Style Sheet (CSS), and JavaScript.
❖ Dynamic Web pages:
❑ Server-side programs are used to achieve dynamic functionalities on Web
pages.
❑ Provide interactivity with the users.

Users accessing the Web pages on the World Wide Web (WWW) are
referred to as Web clients.

Introduction Java Web Application – Huong Hoang Luong 6


Web Application

❖The advantages of Web applications over desktop


applications are as follows:
❑Easier access to information
❑Lower maintenance and deployment costs
❑Platform independence
❑Wider visibility

Introduction Java Web Application – Huong Hoang Luong 7


Web Application Architecture
❖ One-tier architecture
❑ Code related to presentation, business, and data access logic are all

clubbed together.
❑ Figure shows the one-tier architecture.

❖ Two-tier architecture
❑ Code related to data access logic is separated from the other two

components.
❑ Any interaction with data tier will be done through business tier.

❑ Figure shows the two-tier architecture.

Introduction Java Web Application – Huong Hoang Luong 8


Web Application Architecture
❖ Three-tier architecture
❑ Code related to all three components is separated from each other.

❑ Business tier acts as an interface between the data tier and the

presentation tier.
❑ Figure shows the three-tier architecture.

❖ N-tier architecture
❑ Layers are further subdivided for ease of functioning.

❑ Presentation layer is a graphical user interface that displays data to users.

❑ Business layer and application layer are separated reducing the number of

locations implementing the logic.


❑ Figure shows the N-tier architecture.

Introduction Java Web Application – Huong Hoang Luong 9


Web Application Technology - HTML
❖ HTML is a presentation language which enables Web designer to create
visually attractive Web pages. These pages are stored on Web.
❖ HTML allows Web designers to add images, create forms, and embed media
objects on the Web pages. All these are referred as Web resources which are
stored on the Web server along with HTML pages.
❖ Figure shows the Web page designed using HTML.

Introduction Java Web Application – Huong Hoang Luong 10


Web Application Technology - HTTP
❖ The requests and responses sent to a Web application from one computer to
another computer are sent using HTTP.
❖ An HTTP client, such as a Web browser opens a connection and sends a
request message to an HTTP server asking for a resource.
❖ The server, in turn, returns a response message with the requested resource.
Once the resource is delivered, the server closes the connection.
❖ Referred to as a stateless protocol as HTTP does not store any connection
information about the page.
❖ Figure depicts HTTP request and response to a Web application.

Introduction Java Web Application – Huong Hoang Luong 11


HTTP Request
❖ Header information
❑ Specifies the User-Agent along with the Accept header.

❑ The User-Agent element header indicates the browser used by the

client.
❑ The Accept header element provides information on what media types

the client can accept.


❑ After the header, the client sends a blank line indicating the end of
request message.
❑ Figure shows the sample header information sent in the request message.

Introduction Java Web Application – Huong Hoang Luong 12


HTTP Response
❖ The Web application processes the request sent by a client and generates a
response message for the requesting client.
❖ Figure depicts the response message structure.

Its elements are namely, Status line and Header information.


❖ Status line
❑ Indicates status of the requested process.

Introduction Java Web Application – Huong Hoang Luong 13


HTTP Response
❖ Header information
❑ Contains information such as server, last modified date, content-length, and
content-type.
❑ Figure shows the sample message format sent in the response with the HTML
content from the Web server.

Introduction Java Web Application – Huong Hoang Luong 14


HTTP Method
❖ Web applications allow users to enter information using forms and send to
the server for processing.
❖ The data entered into the fields in a form is sent to the Web server through
URL.
❖ The data is processed on the Web server and the appropriate response is
generated which is sent back to the user.
❖ Figure shows an example of request parameters.

Introduction Java Web Application – Huong Hoang Luong 15


HTTP Method
The HTTP request messages commonly use the following HTTP methods for
transmitting request data over the Web:
❖ GET
❑ Informs the server to retrieve information from the request URL.
❑ Information is passed as a sequence of characters that are appended at the end of
the request URL, which forms a query string.
❑ The length of query string is restricted to 240 to 255 characters depending on the
server.
❑ A sample query string constructed while sending user data in the request URL:
http://www.abcBank.co.uk/search?name=john&pass=j7652
❖ HEAD
❑ Functionally same as GET, except that the client uses HEAD method to receive
only the headers of the response and not the message body.
❑ Advantageous when the user want to check characteristics of a resource without
actually downloading the complete resource.

Introduction Java Web Application – Huong Hoang Luong 16


HTTP Method
❖ POST
❑ Used when sending information, such as credit card numbers or

information to be saved in the database.


❑ Data sent is in encrypted format and not visible to the client, and there is

no limit on the amount of data being sent.


❑ The data is passed to the server in the body of the HTTP message, and

hence the request cannot be bookmarked or emailed.


❑ Figure shows the request structure with HTTP POST method.

Introduction Java Web Application – Huong Hoang Luong 17


HTTP Method

❖ PUT
❑ Used to request the server to store the data enclosed in the HTTP

message body at a location provided in the request URL.


❖ DELETE
❑ Used to request the server to delete file at a location specified in the

request URL.
❖ OPTIONS
❑ Can be used to query the server on the methods supported for a

particular resource available on the server.


❖ TRACE
❑ Basically used for debugging and testing the request sent to the server.

❑ It asks the user to echo back the sent request.

❑ Useful when the request sent to the server reaches through the proxies.

Introduction Java Web Application – Huong Hoang Luong 18


Java Web Application
❖ The Web components of Java Web application are as follows:
❑ Servlet: Java classes that dynamically process HTTP requests and

construct responses, a key component in Web development.


❑ JSP: A text document that creates dynamic Web content, usually to display

the content in the Web browser.

❖ Prevalent Server-side Technologies:


❑ Common Gateway Interface (CGI)

❑ Proprietary Web server API (ISAPI)

❑ Server-side JavaScript

❑ Active Server Pages

❑ PHP

❑ Java Servlets

❑ Java Server Pages (JSP)

Introduction Java Web Application – Huong Hoang Luong 19


Common Gateway Interface (CGI)
❖ A set of standards that specify how data is transferred between the Web
server and server-side CGI programs
❖ The data provided by the client in the Web page is sent from HTTP server to
the gateway or CGI program.
❖ The CGI program processes the data and returns the result to the Web server,
which in turn sends it to the client.
❖ Figure depicts the server process for running CGI.

A program that gives dynamic


output, thereby executing in
real time, is written at the
server-side using standards of
CGI.

Introduction Java Web Application – Huong Hoang Luong 20


Common Gateway Interface
❖ When a Web server receives a request that need to access a CGI program, it
creates a new instance or process of the CGI program and then pass the
client data to it.
❖ Figure depicts the working of CGI program.

❖ Disadvantages of CGI:
❑ Reduced efficiency - Number of processes that can be executed by a Web server is
limited to each server.
❑ Reloading Perl interpreter - Each time the server receives a request, the Perl
interpreter needs to be reloaded. This reduces the efficiency of the server.

Introduction Java Web Application – Huong Hoang Luong 21


Servlets
❖ The processing of Servlet is very similar to CGI program, as it also responds
to HTTP request and generates dynamic response.
❖ Servlet:
❑ There is only a single instance of Servlet created on the Web server.
❑ To service multiple clients’ request, the Web server creates multiple threads for
the same Servlet instance.
❑ Each thread handles request received from the client and generates response that
is sent to the Web engine, which in turn sends the response to the client.
❖ Figure shows the working of the Servlet.

Introduction Java Web Application – Huong Hoang Luong 22


Merits of Servlet

❖ Servlets are written using Java language, which makes extensive use of Java
API. The merits of servlets are:
❑ Enhanced efficiency - Servlet is required to be executed only once at the

beginning with the initialization code. Thereafter, it gets auto refreshed


each time a request is sent for execution.
❑ Ease of use - Servlets does not have too many complexities. It is just basic

Java along with HTML implemented within the Java code, which increases
the ease of use.
❑ Powerful - The usage of Java APIs makes the Servlets a powerful server-

side scripting language.


❑ Portable - Java is platform independent and since Servlet uses Java code

for writing scripts, it can be executed in any platform.


❑ Safe and cheap - Usage of Java codes provides high security of data to be

sent and received, thereby maintaining the safety.

Introduction Java Web Application – Huong Hoang Luong 23


Developing Web Application
❖ Java Web application comprises servlets, JSP pages, images, HTML files,
JavaBeans, Applets, and Java classes.
❖ Package all the files associated with Web application into a single Web
archive file (war).
❖ The Java Web applications are deployed on a Java-enabled Web server
such as Tomcat.

❖ Examples of other Java servers containing a Web container for


managing Web components include:

Introduction Java Web Application – Huong Hoang Luong 24


Developing Web Application

❖ Figure shows a Web server containing a Web container


used for processing the HTTP request and HTTP response
for the accessed Servlet or JSP.

❖ Every Java Web server contains a Web container which is


responsible for managing the Web components on the server.
Introduction Java Web Application – Huong Hoang Luong 25
Web Container
❖ Manages execution of Servlets and JSP pages.
❖ Takes request from a Web server and passes it to a servlet for processing.
❖ Manages the servlet life cycle and other services such as security, transaction,
and so on for the Web components.
❖ Referred as Servlet container.
❖ Figure depicts a Web container.

❖ The performance of a servlet depends upon the efficiency of the Web


container.

Introduction Java Web Application – Huong Hoang Luong 26


Web Application Life Cycle

Develop the Web component code.

Develop the Web application deployment descriptor.

Compile the Web application components and helper classes referenced by the
components.

Optionally, package the application into a deployable unit.

Deploy the application into a Web container.

Access a URL which references the Web application.

Introduction Java Web Application – Huong Hoang Luong 27


Web Application Life Cycle

Introduction Java Web Application – Huong Hoang Luong 28


Package Web Application

❖A Web application is composed of Web pages and Web


components which are collectively referred as Web
resources.
❖The Web applications also need to be packaged with
Web resources before the application is deployed on
the Web server.
❖The package file used for a Web application has a
specific set of directory structure:
❑That is packaged in the .war
❑Helps the Web container to ensure proper functionality of the
application.

Introduction Java Web Application – Huong Hoang Luong 29


Web Application Context Root

❖ Each Web application is identified or assigned with a context root.


❖ The context root or context path of the Web application:
❑ Is the base URL used for locating Web pages and Web components such as
Servlet and JSP within the application.
❖ Example:
❑ Suppose the application is deployed on the local Web server.
❑ Then, the URL to access the index.html page will be:

http://localhost:8080/WebApp/page/index.html.
❑ Thus, the context root of the deployed application is the /WebApp.

Introduction Java Web Application – Huong Hoang Luong 30


Web Application Directory Structure
❖ The context root of the Web application contains two main components in the
directory structure:
❑ Static files - All the HTML pages and images comprise the static files.

Images can be collectively stored in an images directory.


❑ WEB-INF - Exists within the context root, cannot be referenced.

❖ Figure shows the directory structure of the .war file.

Introduction Java Web Application – Huong Hoang Luong 31


WEB-INF Directory
The contents of WEB-INF directory are as follows:
❖ classes directory
❑ Servlet classes, JavaBean classes, and any other classes required by the

Web applications are stored in this directory.


❑ Example: If a servlet or JavaBean class is part of a package such as

www.bookstore.com, then the corresponding class file will be placed


in classes/com/bookstore directory.
❖ web.xml
❑ Basically, it is an XML structured file which provides configuration

information for the components of the Web application.


❑ Called as the deployment descriptor of the Web application.

❑ Includes information such as the default pages to show mapping Servlets

with their URLs, and so on.

Introduction Java Web Application – Huong Hoang Luong 32


WEB-INF Directory

❖ lib directory
❑ Library JAR files used by the application are stored within the WEB-

INF/lib directory.
❑ Example: Database driver files.

❖ tags directory
❑ Contains tag files, which provide implementation for custom tags.

❖ tag library descriptor (tld) files


❑ Provide information about the attributes of a custom tag and the class to

be invoked when the tag comes across in a JSP page.


❑ Files have .tld extension.

Introduction Java Web Application – Huong Hoang Luong 33


Development Descriptor

❖ A configuration file which describes how the Web application should be


deployed.
❖ Written using XML with name web.xml and placed under the WEB-INF
folder, WEB-INF/web.xml.
❖ When the Web server receives a request for the application, it uses the
deployment descriptor to map the URL of the request to the Servlet that
handles the request.
❖ Example: To map a URL request to a Servlet, the following settings must be
done in the web.xml file:
❑ Declare the servlet with the <servlet> element.

❑ Define a mapping from a URL path to a Servlet declaration with the


<servlet-mapping> element.

Introduction Java Web Application – Huong Hoang Luong 34


Development Descriptor

Introduction Java Web Application – Huong Hoang Luong 35


Development Descriptor

Introduction Java Web Application – Huong Hoang Luong 36


Development Descriptor

❖ Servlet 3.0 has also introduced a new feature called Web fragments.
❖ Web fragment is the mechanism that:
❑ Include deployment descriptor information which is specific to a library.
❑ Helps to segregate the unrelated information from the main deployment
information.
❑ Is stored in the folder META-INF\web-fragment.xml of the Web
application directory structure.

Introduction Java Web Application – Huong Hoang Luong 37


ĐẠI HỌC FPT CẦN THƠ

Demo
Summary

An application is a collection of programs designed to perform a particular task. Different


types of applications are designed for different purposes.

A Web application is a software application that runs on a Web server. The Web
application basically has three components: the presentation layer, the application
layer, also known as business layer, and the data access layer.

The most common technologies for communication on the Web are HTML and HTTP.
HTML is a presentation language which enables Web designer to create visually
attractive Web pages.

The requests and responses sent to a Web application from one computer to another
computer are sent using HTTP. HTTP does not store any connection information about the
page and hence, it is referred to as a stateless protocol.

Introduction Java Web Application – Huong Hoang Luong 39


Summary

The HTTP request messages uses the HTTP methods for transmitting request data over the Web.

Java Web application comprises servlets, JSP pages, images, HTML files, JavaBeans, Applets, and

Java classes.

The Web applications are packaged in the .war file which allows the content of the Web application are
accessed from the application’s context root.

A deployment descriptor describes how the Web application should be deployed. It is written using XML with
name web.xml and placed under the WEB-INF folder, that is, WEB-INF/web.xml.

Java Web applications are developed in NetBeans IDE which provides fully-integrated

environment for building and executing the Web applications.

Introduction Java Web Application – Huong Hoang Luong 40


ĐẠI HỌC FPT CẦN THƠ

You might also like