You are on page 1of 38

Java and Web Design

Unit 1

Unit 1

Introduction to Web and Java

Structure
1.1

Introduction
Objectives

1.2

Overview of the World Wide Web

1.3

Overview of Web Browsers


Components of Web Browsers
Features of Web Browsers
The URL and DNS

1.4

Java: A Web Programming Language

1.5

Java-Based Web Technologies


Java Servlet
JSPs

1.6

Web Architecture Models


Model-1 Architecture
Model-2 Architecture

1.7

The MVC Architecture


Model
View
Controller

1.8

Summary

1.9

Glossary

1.10

Terminal Questions

1.11

Answers

Sikkim Manipal University

B2016

Page No. 1

Java and Web Design

1.1

Unit 1

Introduction

The Internet has impacted our life in numerous ways. It has changed the
way we work, communicate, network, educate ourselves and others, make
financial transactions, and much more. It offers many possibilities and is an
empowering tool.
This experience of getting online is facilitated by the World Wide Web
(www), commonly known as the web. The web is a structure of interlinked
documents called web pages accessed through the Internet. Web pages
contain images, text, videos, and many other multimedia elements. The
multiple multimedia elements that the web offers are enabled by web
applications. Web applications are programs that run on more than one
computer and communicate through a network or server.
Virtually every type of web application is developed and delivered by using
Java. Java is a programming language that allows the programmers to
develop high-performance applications such as mobile applications, web
applications, and enterprise applications. As a programming language, Java
is the global standard for developing and delivering web applications. Java
programs can be embedded in web pages and distributed to users. This
facilitates automatic distribution and installation of applications on the
computer.
In this unit, you will learn about the web, Java and the role and importance
of Java in web designing and development. First, you will get an overview of
the web. Next, you will be introduced to Java as a programming language.
Further, you will learn about Java based web technologies such as Servlets
and Java Server Pages or JSPs. Towards the end of the unit, you will learn
about web architecture models.
Objectives
After completing this unit, you will be able to:

define the World Wide Web

describe web browsers

explain Java as a programming language

Sikkim Manipal University

B2016

Page No. 2

Java and Web Design

Unit 1

describe Java-based web technologies

discuss Web architecture models

1.2

Overview of the World Wide Web

The World Wide Web or the web is an information sharing model for
accessing information on the Internet. The web and internet are not the
same; they are separate but related. The Internet is a network. It enables
the user of any computer connected to the Internet to retrieve information
from other computers connected to the Internet, provided the user has
permission to do so.
With the passage of time, the Internet has gradually evolved into its present
form. In 1969, Advanced Research Projects Agency (ARPA) of the U.S.
government realised that in the interest of a proper research and
development in the country, research scientists at one university must be
able to communicate with the scientists at other universities. This realisation
on part of the government led to the development of a network called the
Advanced Research Projects Agency Network (ARPANET). ARPANET
allowed the research scientists and scholars from different universities to
communicate with each other. One of the benefits of ARPANET was that
the network could function properly even if a portion of it was destroyed.
ARPANET finally evolved into the Internet, which is a public, cooperative,
and self-sustaining network, allowing people to communicate worldwide.
The web was developed by Tim Berners-Lee of the European Organization
for Nuclear Research (CERN) in Switzerland in 1989. Initially, the web used
networked hypertext documents to allow effective communication among
the members worldwide. Then, the hypertext documents over the web
began to include graphics, video, and sound. Due to the effective and
proper communication provided by the web, its use has spread globally in
an amazingly shorter period of time. The significance of Berners-Lee's
invention lies in the fact that it integrated hypertext to the Internet and
developed the following three technologies:
1.

Universal
Document

Sikkim Manipal University

A system of globally unique identifiers for web


resources, which later came to be known as

B2016

Page No. 3

Java and Web Design

Identifier (UDI)

Unit 1

Uniform Resource Locator (URL) and Uniform


Resource Identifier (URI)

2.

HyperTextMar The markup language to create the web pages


kup Language displayed on the web browsers
(HTML)

3.

HyperText
Transfer
Protocol
(HTTP)

A protocol to define the way messages are


formatted and transmitted, and the actions that
web servers and browsers should take in
response to various commands

The web was a remarkable improvement over other contemporary hypertext


systems. With the advent of the web, it became possible to link to another
resource without action by the owner of that resource as the web required
only unidirectional links rather than bidirectional ones. Though it significantly
reduced the challenges regarding implementation of web servers and
browsers, but it caused the chronic link rot problem. As a non-proprietary
system, the World Wide Web, unlike predecessors such as HyperCard,
made it possible to develop servers and clients independently. In addition, it
also allowed adding of extensions without licensing restrictions.
NOTE:
In French, CERN is known as Centre Europen pour la Recherche
Nuclaire.
Self Assessment Questions:
1.

Which of the following statements is not correct about ARPANET?


a. It allowed the research scientists from different universities to
communicate with each other
b. It could function properly even if a portion of it was destroyed
c. It was developed by CERN, Switzerland
d. It finally evolved into the Internet

2.

When was the World Wide Web born?

Sikkim Manipal University

B2016

Page No. 4

Java and Web Design

Unit 1

a. 1969
b. 1979
c. 1989
d. 1999
3.

Which of the following agencies developed the World Wide Web?


a. ARPA
b. NASA
c. ISRO
d. CERN

4.

Which of the following statements are correct about the World Wide
Web?
a. It requires unidirectional links
b. It requires bidirectional links
c. It marries hypertext to the Internet
d. It is non-proprietary

Activity 1:
Make a comparative list of features of the World Wide Web and some of
its predecessors.
Objective of the activity: To enhance your understanding of the superior
features of the World Wide Web
This activity should not take you more than 10 minutes to complete.

1.3

Overview of Web Browsers

A web browser is the application software that allows users to access and
navigate the World Wide Web. A web browser allows the users to make
request to the web server for a particular task, which is then processed by

Sikkim Manipal University

B2016

Page No. 5

Java and Web Design

Unit 1

the server and the desired result is sent to the web browser. The types of
web browsers can be classified under the following categories:
Graphical: Such web browsers allow you to access text, images, audio,
and video present in documents over the web. Examples of such web
browsers are Internet Explorer, Firefox, Apple Safari, Google Chrome and
Netscape. These web browsers are available for Windows, Mac, Linux and
other operating systems. Usually, navigation of web pages or links in the
Graphical web browsers is done by using a mouse pointer. Although you
can install a graphical web browser on your computer, yet some of these
browsers are automatically installed during the installation of the operating
system. For example, Internet Explorer is a part of the Windows operating
system, and can also be downloaded and installed using the
http://www.microsoft.com/ URL. Firefox is the web browser that can be
downloaded from the http://www.mozilla.org/products/firefox/ URL and
Safari is available at the http://www.apple.com/safari/ URL. Fig. 1.1 shows
different graphical web browsers:

Fig. 1.1: Different Graphical Web Browsers

Text: Such web browsers allow you to access the documents over the web
in text-only mode. Navigation in the text type web browser is done by
highlighting emphasised words using the up and down arrow keys, and then
pressing the ENTER key to open the link. Lynx is a good example of a text
web browser.
1.3.1

Components of Web Browsers

All web browsers have certain components common to them.

Sikkim Manipal University

B2016

Page No. 6

Java and Web Design

Unit 1

Fig. 1.2 shows the components of a web browser:


Title Bar Address Bar

Refresh button

Back
button

Navigation Toolbar

Home button

Workspace

Scroll
Status bar

Bars

Fig. 1.2: Components of a Web Browser

Lets examine each component of a web browser, as displayed in Fig. 1.2.


Address bar: It is a wide box at the top of the browser window that spans
across the screen. You type in web address or URL in the address bar to
visit the target website.
Title bar: It is located at the very top of the browser window. The title bar
displays the title of a web page currently being viewed.
Navigation toolbar: It is underneath the title bar. It contains frequently used
shortcut tools, such as Back, Home, and Refresh buttons, among others.
You can navigate the web pages through the navigation toolbar.
Workspace: It is the display window where you read the content on an
open browser.
Scroll bars: Scroll bars let you use your mouse for pulling up, down, or
sideways a web page, if it is longer than the screen space.
Status bar: It is a tiny box at the bottom of the browser window. It displays
important information, such as pages load status, URL of the web address,
etc.

Sikkim Manipal University

B2016

Page No. 7

Java and Web Design

1.3.2

Unit 1

Features of Web Browsers

Now, let us discuss some of the essential features of web browsers.


Fig. 1.3 shows the features of web browsers:

Easy to Use

Component
Rich

Caching
Features
of Web
browsers
Search

Bookmark
Complete
Internet
Package

Fig. 1.3: Features of Web Browsers

Lets discuss each feature in detail.


Easy to use: The most important feature of a web browser is ease of use.
The web browser connects to web pages on various sites throughout
Internet. It enables the user to follow the hyperlinks on the web and type in a
URL for it to follow.
Complete Internet package: Some web browsers offer a complete Internet
package capable of handling text, images as well as the hyperlinks to digital
videos, or other types of information. They display web pages that contain
animated or interactive items. They also provide the facility to save a web
page in a file on your computer, print a web page on your computer and
send the contents of a web page through e-mail to others over the Internet.
Component rich: Web browsers have components like e-Mail client,
newsgroup client, an HTML composer, telnet client, ftp client, etc. They also
Sikkim Manipal University

B2016

Page No. 8

Java and Web Design

Unit 1

have a number of commands readily available through menus, icons, and


buttons. It includes an easy way to get online help as well as built in links to
other resources on the web that can provide you help or answer your
questions.
Bookmark: Web browsers provide a way to save links to the sites you have
visited on the web so that you can revisit them in future..
Search: One of the main features of a browser is that they are able to
search the information on the current page as well as search the web itself.
Caching: Another related feature of a web browser is caching. Browsers
keep caches of the pages you visit so that they do not have to download
them again if you want to revisit them. Reloading a page from the cache is
much quicker that downloading it again from the original source.
Lets now learn how to access a web page available on the web. It is the
URL and Domain Name System (DNS) that facilitate accessing and
Retrieving Documents on the web.
1.3.3

The URL and DNS

URL stands for Uniform Resource Locator, which defines the unique
Internet address of a file stored on a host computer connected to the
Internet. Every Internet resource possesses a unique URL, irrespective of
the fact that whether or not it can be accessed by a user. You can access a
web page or any other resource available on the Internet by providing the
appropriate URL of the resource in the web browsers; for instance, the URL
to access the web page of Google is http://www.google.com. A specified
URL helps access the appropriate resource from the specific directory
available on the host computer. This resource is then displayed on the
monitor of the clients machine. The process of providing a unique name to
a resource or domains is known as DNS.
The DNS is a worldwide system of servers that contains pointers to the
websites. DNS provides alphanumeric addresses (URL) that can be easily
remembered by the users. The concept of alphanumeric addresses has
eased the problems of users as now they do not have to remember the
numeric Internet Protocol (IP) address of the hosts. The alphanumeric
addresses are automatically translated into the specific IP address and the
desired file stored at that IP address is retrieved.

Sikkim Manipal University

B2016

Page No. 9

Java and Web Design

Unit 1

For example, the URL of the website of the U.S. House of Representatives
(http://www.house.gov/house/House_Calendar.shtml) represents addresses
hosted in domains in the United States.
Fig. 1.4 explains the components of this URL:

Fig. 1.4: Components of a URL

Some commonly used top-level domains (TLDs) are defined in Table 1.1:
Table 1.1: Commonly Used TLDs

.com

Represents the top-level domain for commercial entities

.edu

Specifies the top-level domain for four-year, accredited


colleges and universities

.net

Represents the top-level domain for networking


organizations, such as Internet Service Providers (ISP)

.org

Denotes the top-level domain for miscellaneous


organizations, which may include non-profit groups

In November 2000,some new domain names were approved by the Internet


Corporation for Assigned Names and Numbers (ICANN), namely, .biz,
museum, .info, .pro (for professionals) .name (for individuals), .aero (for the
aerospace industry), and .coop (for cooperatives).
Apart from the above mentioned domain names, some domain names are
used to identify and locate files stored on hosts all over the world. These
domain names are called two-letter Internet country codes. The
International Standards Organization has standardised them as ISO 3166.
Some country domains are listed in Table 1.2:
Table 1.2: Country Domains

in

India

ch

China

de

Germany

jp

Japan

Sikkim Manipal University

B2016

Page No. 10

Java and Web Design

Unit 1

uk

United Kingdom

ca

Canada

Lets now learn the concept of Hypertext Transfer Protocol (HTTP) that is
used to transfer data over the web.
Overview of HTTP
The data on the web is transmitted by using some specific protocols. The
main protocol used for the web is HTTP, which defines how the computers
on the web or Internet exchange data. The HTTP defines how the web
browser and the web server can communicate with each other, while the
TCP and IP protocols perform the actual task of moving bits and bytes over
the network. An HTTP conversation consists of a handful of commands from
the client and a stream of data from the server.
Fig. 1.5 shows the flow of data exchange on the Internet:
When a client makes request to a web server using a web browser, the browser
contacts and sends the user request to the server.

Then, the web server processes the request and delivers the response generated in
the form of a document to the client.

The web browser at the clients end displays the received document or response.

The server also informs the web browser about the kind of document. It specifies
whether the document is an HTML file, a PDF file, or a ZIP file.

The document is displayed in the appropriate program in accordance with the type
of the document received by the web browser. For example, if the PDF file is
returned by the web server in the form of the response, then the file is displayed
with the help of the Adobe Reader program installed on the clients machine.

Fig. 1.5: Flow of Data Exchange on the Internet


Sikkim Manipal University

B2016

Page No. 11

Java and Web Design

Unit 1

NOTE:
A software program that retrieves document from the web server is
referred to as a client in network terminology and as a user agent in web
terminology. Also, the computer responding to the clients request is
known as server and not the computer.
Lets now understand the concept of cookies that are meant for
remembering user information.
Cookies
A cookie is a text file that is stored on the clients computer by a web server.
Cookies can be retrieved later by the web server whenever the client makes
the request again to the server. A unique ID number may be generated for
each visitor by a website that can be stored by it on every clients machine
using a cookie file.
If you are using the Microsoft Internet Explorer for browsing the web, all the
cookies files get stored on your machine in the cookies folder. You can find
this folder in the Documents directory of the current user. When you
navigate to the Cookies directory on your machine you would find many text
files (Cookies file).
Cookie files contain information in the form of name-value pairs. On the
basis of the name and value pair, you can easily identify the website that
placed the cookie file on your machine. When you access a website, say,
www.google.com,it places a cookie file on your machine. The following code
snippet shows the content of the cookie file for the www.google.com
website:
User ID

2e88c198291a0dea7421210

www.google.com/

In the preceding code snippet, you can notice a single name-value pair,
which specifies the value of the User ID (2e88c198291a0dea7421210)
when the user has visited the www.google.com website.
NOTE:
It is possible that several other values may get stored in the file after the
name and value pair in the cookie file. These are housekeeping
Sikkim Manipal University

B2016

Page No. 12

Java and Web Design

Unit 1

information for the web browser. For example, the yahoo.com website
stores a bit more information on clients machine. When you open the
cookie file that yahoo has created on the machine, the following text is
available:
session-id-time

9067880093 yahoo.com

session-9067880093vctr3timeslog.yahoo.com/ x-main

Self Assessment Questions:


5.

Which of the following is not a graphical web browser?


a. Internet Explorer
b. Lynx
c. Firefox
d. Chrome

6.

Which component of a web browser allows you to type in URL?


a. Title bar
b. Navigation toolbar
c. Address bar
d. Status bar

7.

Which of the following is not a feature of web browsers?


a. Can delete cookies automatically
b. Is easy to use
c. Can properly display and handle web pages
d. Can save a web page in a file on a computer

8.

What is the process of providing a unique name to a resource or


domains is called?
a. Domain Name System (DNS)
b. Uniform Resource Locator (URL)
c. Internet Protocol (IP) address

Sikkim Manipal University

B2016

Page No. 13

Java and Web Design

Unit 1

d. Top-level domain name


9.

What is HTTP?
a. Second-level domain name
b. Protocol
c. File name
d. Top-level domain name

Activity 2:
Make a list of the desirable features of your dream web browser.
Objective of the activity: To enhance your understanding of the features
of web browsers
This activity should not take you more than 10 minutes to complete.

1.4

Java: A Web Programming Language

In 1995, James Gosling at Sun Microsystems introduced the Java


programming language. As a programming language, Java is the global
standard for developing and delivering web applications. The syntax used in
Java is more or less similar to the syntax used in C and C++. The Java
applications are first compiled to byte code, which can then be executed on
any Java Virtual Machine (JVM).Therefore, JVM is a prerequisite to use
Java and Java based technologies. Java Servlet and JavaServer Pages are
the two important technologies of Java used to create the web applications.
Both these technologies can be combined to develop content publishing
systems supporting separation of presentation and business logic of web
applications. Both Servlets and JSPs use Java Database Connectivity
(JDBC) for handling database operations in web applications.
While building the server-side web applications using Java, the presentation
and the business logics are separated. This separation provides the lessexperienced web designers with an easier and faster way to generate the
web pages with dynamic content. Generally, the separation process applies
to the situation where the content of business and presentation logic
changes frequently.
Sikkim Manipal University

B2016

Page No. 14

Java and Web Design

Unit 1

Earlier, web applications created using Java faced the maintenance


problems. Using the Model/View/Controller (MVC) paradigm for building
user interfaces has solved this problem. In MVC, the back-end system is the
Model, the templates for creating the look and feel of the response is the
View, and the code that combines the Model and View all together is the
Controller.
In the following sections, we will explore Java based-web technologies.
Self Assessment Questions:
10. What are Java applications first compiled to?
a. C
b. C++
c. Byte code
d. JVM
11. What is a prerequisite to use Java and Java based technologies?
a. Java servlet
b. Java Virtual Machine (JVM)
c. Java Server Pages (JSP)
d. Java Database Connectivity (JDBC)
12. Which of the following is used for handling database operations in
web applications?
a. Java Server Pages (JSP)
b. Java Virtual Machine (JVM)
c. Java servlet
d. Java Database Connectivity (JDBC)
Activity 3:
Find out the maintenance problems that the Java based web applications
faced earlier.

Sikkim Manipal University

B2016

Page No. 15

Java and Web Design

Unit 1

Objective of the activity: To enhance your understanding of Java as a


web programming language
This activity should not take you more than 10 minutes to complete.

1.5

Java-Based Web Technologies

Java Servlet and JSPs are the two frequently used Java based web
technologies that allow you to develop web applications. Both Java Servlet
and JSPs use Java Database Connectivity (JDBC) for handling database
operations in web applications.
In this section, we briefly introduce the following Java based web
technologies:

Java Servlet

JSP

1.5.1

Java Servlet

A Servlet is a Java program module that processes and answers client


requests. A Servlet runs on the server and acts as an interface between the
client and the server. Java Servlet provides high level, component-based,
platform-independent and server-independent standards for developing web
applications in Java. To appreciate the significance of Java Servlet in
developing web application, we first need to understand the challenges that
the developers were facing.
When we use a web browser to access simple, static HTML pages, the
browser sends requests to a web server. The web server uses HTTP to
send back the requested web page stored on the server. These web pages
are static; they exist in a constant state. However, with changing
technologies and requirements, we now need to design a web server to
process the data given by the client and display dynamic content to the
client. To add this dynamic behaviour, we need to develop applications that
can:

Run and integrate with the web server

Process client requests

Sikkim Manipal University

B2016

Page No. 16

Java and Web Design

Unit 1

Generate the response content dynamically

To meet these requirements, we have some proprietary solutions in the


form of Application Programming Interfaces (APIs) from various web server
providers, which allow us to build programs that can run with web servers.
However, this approach to build server-side applications causes the
following problems:

The application becomes vendor dependent.

Migrating the application from one server to another becomes difficult.

While developing the application, we have to decide the server on which


the application has to be deployed, and consequently, we have to learn
the vendor specific API to develop the application.

As a solution for the above stated problems, the Common Gateway


Interface (CGI) came up. The CGI is an open standard abstraction between
HTTP adapter (web server) and an external application written by using any
of the programming languages, such as C or C++. Some of the benefits of
CGI include:
Simplicity: It is easy to understand and implement.
Open standards: It allows deploying the CGI application on different web
servers.
Architecture independent: It does not depend on any particular web
server architecture.
Language independent: It can be written in any language.
Process isolation: It allows running the CGI applications outside the web
server process. Therefore, if there is any bug in the CGI applications, it will
not crash the web server process.
The CGI approach has its own limitations. In this approach, the server
creates and destroys the process for each request. It results in an increased
workload on the server and a longer time in processing the requests. This,
in turn, reduces the availability of the web applications. This approach works
well only for few clients, but as the number of clients increase, the web
server using the CGI applications is not able to serve the clients. Therefore,
Sikkim Manipal University

B2016

Page No. 17

Java and Web Design

Unit 1

CGI is fraught with problems of low performance, poor efficiency, and


stateless support.
Java Servlet came as an alternate to CGI, by providing high level,
component-based,
platform-independent,
and
server-independent
standards for developing web applications in Java. Java Servlet technology
provides a simple, vendor independent mechanism for extending the
functionality of web servers.
Servlets possesses many advantages, which are as follows:

Servlets are faster than CGI.

Servlets use a standard vendor-independent API that is supported by


numerous web servers.

Servlets provide a high level API to programs.

Servlets provide declarative security management support that allows us


to easily build and modify the security logic for server-side extensions.

Java Servlet enhances the web server functionality. Servlets function


such as applets that run on the server. These are portable, platform and
web server independent means to deliver dynamic content for a client.

Servlet is a Java class that extends the javax.Servlet.GenericServlet


class. This allows Servlets to function on any platform having a JVM and
a web server that support them.

Servlets can communicate directly with existing enterprise resources by


using generic APIs, such as JDBC. Consequently, application
development becomes simple and easy.

Servlets are extensible; by using them, developers can extend the


functionalities of a web application similar to any Java application.

Servlets possesses a better performance than the CGI scripts for two
reasons. First is that once a Servlet is loaded into memory, it may be
called any number of times without the need of extra hardware as the
Servlet can run on a single lightweight thread. Another reason is that a
Servlet maintains a pool of connections to databases or other necessary
Java objects. This saves time required for processing requests.

Sikkim Manipal University

B2016

Page No. 18

Java and Web Design

Unit 1

Servlets eliminate the complexity of retrieving parameters from an HTTP


request, as Servlets can directly access the parameters as objects.

Servlets provide the mechanism to maintain the session data throughout


a web application using the APIs. Maintenance of the users session
data is required in some applications, such as online book store where it
is important to keep track of user order of different books in order to
calculate the total bill. HTTP is a stateless protocol and does not
maintain the state of user requests. This limitation of HTTP is overcome
by the use of Servlet in the web applications.

Servlets are accessible with wide range of service APIs available under
the Java platform; which allows us to solve complex problems by using a
simple API.

Next, lets understand the concept of JSPs, used for creating web
applications in Java.
1.5.2

JSPs

JSPs are files that contain HTML and Java code. JSPs have overcome the
shortcomings of Java Servlets. We learnt that Java Servlet is used to
develop server side extensions for presenting dynamic content to clients.
However, while using Java Servlet technology for creating dynamic views,
we can observe the numerous usage of the out.println statements.
System.out.println statement is used to print the arguments passed to it or
display the output. These output statements make the Java Servlet code
very complex and difficult to understand. Most of the code lines in a Servlet
contain static content presentation statements (in the out.println statements)
and contain very less Java logic, which leads to the following issues:

We cannot take the advantage of the Integrated Development


Environments (IDEs) to design HTML views, which increases the
development time and cost of developing an application.

We are not able to utilise HTML designers services to prepare the


presentation view; instead we need to depend on Java programmers to
prepare the views.

Sikkim Manipal University

B2016

Page No. 19

Java and Web Design

Unit 1

To solve these problems, we need to include additional instructions for


processing dynamic content in the HTML pages. Therefore, a page that
includes some tag based additional instructions has to be parsed and
resolved into a standard server side extension (or Servlet) that are
understood by the web container. These additional instructions to
process dynamic content have the following advantages:

They can be easily used by the web designers.

Their format is understood by the IDEs that is used to design the


HTML views.

This requirement of tag based instructions has necessitated the introduction


of JSPs. The JSP technology clearly differentiates the application logic and
content. As a result, content providers are not required to have the
knowledge of the Java technology to update or maintain the content. They
just require designing interfaces with the help of the JavaBeans components
and custom tags supplied by web application developers. Similarly, web
application developers do not need to be experts in user interface design to
build web applications. Apart from separating the presentation and
application content, the JSP technology provides all the benefits offered by
Java Servlet.
Lets now examine how a web container processes a JSP page to handle
an incoming user request. Fig.1.6 shows how the requests are processed
by a JSP page:

Fig. 1.6: Request Processing by JSP page


Sikkim Manipal University

B2016

Page No. 20

Java and Web Design

Unit 1

After understanding the request processing, lets understand the lifecycle of


a JSP page. A web container processes and manages JSP pages through a
well-defined lifecycle that defines how a JSP page is translated, compiled,
loaded and initialised. The JSP lifecycle also defines how to handle and
destroy user requests.
Fig. 1.7 shows the five phases of the JSP lifecycle:

Fig. 1.7: Five Phases of the JSP Lifecycle

Lets discuss each phase in detail.


Page translation: This phase allows the Servlet container to translate the
JSP document into an equivalent Java code, that is, a Servlet.
Compilation: In this phase, the Servlet container compiles the Servlet into
the Java byte code. After compilation, the class file is generated. Then, the
container can decide to discard or retain the Java source code. In general,
most of the containers discard the generated Java source code by default,
unless the source code needs to be retained for debugging.
Loading and initialisation: This phase allows the web container to load the
generated and compiled Servlet and create an object of the Servlet. If this
phase is performed successfully, the web container puts the Servlet object
into the active state, that is, makes it available and ready to handle
requests.
Request handling: This phase allows handling of client requests. In this
phase, the container creates the ServletRequest and ServletResponse
types of objects. If the client uses HTTP to send requests, the web container
creates the HttpServletRequest and HttpServletResponse types of objects.
The request object represents the request (the requested data), and can be
Sikkim Manipal University

B2016

Page No. 21

Java and Web Design

Unit 1

used to retrieve client information. The response object can be used by the
Servlet to generate the response. The Servlet then invokes the
_jspService() method on the Servlet object and passes the created request
and response objects to the _jspService() method.
Destroying: This phase allows the Servlet container to destroy the JSP
pages Servlet instance or end the services provided by the Servlet
instance. In this phase, the Servlet container:

Allows all the threads currently in the service method of the Servlet
instance to complete their jobs and makes this instance unavailable for
new requests

Calls the destroy() method on the Servlet instance after the current
threads have completed their jobs

Releases all the references of the Servlet instance and makes it


available for garbage collection after the destroy method is executed

Till now we have discussed the various technologies used for creating web
applications. Next, we will discuss the architecture of web applications.
Self Assessment Questions:
13. Which of the following is an external application responsible to
process client requests and generate dynamic content?
a. API
b. CGI
c. Web server
d. JSP
14. Which of the following is an alternate to CGI?
a. API
b. Java servlet
c. JSP
e. Web server
15. Which of the following are advantages of a Java servlet?

Sikkim Manipal University

B2016

Page No. 22

Java and Web Design

Unit 1

a. Servlets are faster than CGI


b. Servlets provide a high level API to programs
c. Java servlet enhances the web server functionality
d. Most servlet code lines contain very less java logic
16. What necessitated the introduction of JSP?
a. Requirement of tag based instructions
b. High development time and cost of developing an application
under Java servlet
c. Inability to use html designers services to prepare the
presentation view under Java servlet
d. Requirement of integrating the application logic and content
17. Which phase of the JSP lifecycle allows the servlet container to end
the services provided by the servlet instance?
a. Page translation
b. Request handling
c. Destroying
d. Loading and initialisation
Activity 4:
Write a note on the use of Java Database Connectivity for handling
database operations in web applications.
Objective of the activity: To enhance your understanding of Java based
web technologies
This activity should not take you more than 15 minutes to complete.

1.6

Web Architecture Models

Earlier, different programmers used to implement the same functionality in


an application in different ways. Therefore, the logic and execution flow of
an application used to vary from one programmer to another. This led to the
problem of understanding the execution flow of the application, which was
Sikkim Manipal University

B2016

Page No. 23

Java and Web Design

Unit 1

resolved with the introduction of development models. These models


provide a standard way of flow control in an application and describe the
technologies used in different parts of the application. A development model
facilitates the design process by separating the code according to the
functions performed by different parts of an application.
For web applications in Java, two types of development models, Model-1
Architecture and Model-2 Architecture, are used. These models are
classified on the basis of different approaches used to develop web
applications.
Lets learn about each model in detail.
1.6.1

Model-1 Architecture

The Model-1 architecture was the first development model used to develop
web applications. This model uses JSPs to design applications, which is
responsible for all the activities and functionalities provided by the
application. Applications using the Model-1 architecture contain a number of
JSP pages, with each page providing different functionalities and views to
different users.
Fig. 1.8 shows the Model-1 architecture:

Fig. 1.8: Model-1 Architecture

In the Model-1 architecture, web applications are developed by mixing the


business and presentation logic. In this model, JSP pages receive HTTP
requests, which are then transferred to the data layer by JavaBeans. After
the requests are serviced, JSPs send HTTP responses back to the client. A
JSP page not only contains display elements, but also retrieves HTTP
parameters, calls the business logic, and handles the HTTP session.

Sikkim Manipal University

B2016

Page No. 24

Java and Web Design

Unit 1

Web applications implementing this type of architecture contain a series of


JSP pages, where a user needs to navigate from one page to another. The
Model-1 architecture is page-centric and only suitable for small web
applications. Despite its simple structure and easy to learn features, the
Model-1 architecture was not successful for designing large projects
because:

It leads to inflexibility and difficulty in maintaining applications. A single


change in one page may cause changes in other pages, leading to
unexpected results.

It involves the developer at both the page development and the business
logic implementation stages. Moreover, there is no scope for the splitting
of work between the web page designer and the business logic
developer.

It enhances the intricacy of a program with the enhancement in the size


of the JSP page; therefore, it is not easy to find the flow of control and
debug the program.

It increases the effort required for maintaining JSP pages in an


application.

Lets now understand the second type of web application architecture,


Model-2.
1.6.2

Model-2 Architecture

The Model-2 architecture was targeted at overcoming the disadvantages of


Model-1 and helping developers to design more powerful web applications.
Fig. 1.9 shows the Model-2 architecture:

Fig. 1.9: Model-2 Architecture


Sikkim Manipal University

B2016

Page No. 25

Java and Web Design

Unit 1

In the Model-2 architecture, the JSP and Java Servlet technologies were
used together to develop a web application. Servlets handle the control flow
while JSPs handle HTML page creation. In due course, the approach of
using JSPs and Servlets together evolved as the Model-2 architecture. In
this type of design model, the presentation logic is separated from the
business logic.
So far, Model-2 is the most successful development model used to develop
web applications. It not only overcomes the limitations of the Model-1
architecture, but also provides new features that have their own
advantages. Some of these advantages are as follows:

This model allows the use of reusable software components to design


business logic.

It offers great flexibility to the presentation logic, which can be modified


without affecting the business logic.

It allows each software component to perform a different task, making it


easy to design an application by simply embedding these components in
the application.

The Model-2 architecture resembles the classical Model/View/Controller


(MVC) architecture. We will discuss the MVC architecture in the next
section.
Self Assessment Questions:
18. Which of the following statements are correct about web architecture
models?
a. They provide a standard way of flow control in an application
b. They describe the technologies used in different parts of the
application
c. They facilitate the design process
d. They integrate the codes according to the functions performed by
different parts of an application

Sikkim Manipal University

B2016

Page No. 26

Java and Web Design

Unit 1

19. How many types of development models are used for web
applications in Java?
a. One
b. Two
c. Three
d. Four
20. Which of the following statements are correct about the Model 1
architecture?
a. It uses JSP to design applications
b. It develops web applications by mixing the business and
presentation logic
c. It develops web applications by separating the business and
presentation logic
d. It is only suitable for small web applications
21. Which of the following is not a shortcoming of the Model-1
architecture?
a. It leads to inflexibility and difficulty in maintaining applications
b. It is suitable for large web applications
c. It is difficult to trace the flow of control and debug the program
22. Which of the following is not a feature of the Model-2 architecture?
a. It uses the JSP and Java servlet technologies together
b. It uses reusable software components to design business logic
c. It offers great flexibility to the presentation logic
d. It allows each software component to perform similar task
Activity 5:
Make a comparative list of the advantages and disadvantages of the two
models of web architecture.

Sikkim Manipal University

B2016

Page No. 27

Java and Web Design

Unit 1

Objective of the activity: To enhance your understanding of web


architecture models
This activity should not take you more than 10 minutes to complete.

1.7

The MVC Architecture

The MVC architecture allows separation of the business logic, data, and the
presentation logic. A web application can ensure a coherent interaction with
users by allowing the three components of MVC--Model, View, and
Controller--to communicate with each other. An application usually provides
different sets of information to its users. The content of a web application is
presented to users on different web pages in different formats. Different
users may require multiple types of interfaces, such as HyperTextMarkup
Language (HTML) for web customer, a Wireless Markup Language (WML)
for wireless customers, Java for administrators and XML-based web service
for suppliers. Therefore, in an application, we may need to create multiple
views for a single model.
When developing a small application, it sounds logical to interweave data
access, business logic, presentation logic, and control logic together.
However, for a large enterprise-level application, interweaving makes code
files more complex and it becomes difficult to manage files while
implementing changes in future. This causes the duplication of data access
and business logic code in the application at various points. As a result, the
implementation, testing, and maintenance efforts are also duplicated.
However, duplication can be reduced to save time and resources by
separating presentation logic, business logic, and control logic as different
components, which can easily be reused. This can be accomplished with
the Model-View-Controller (MVC) design pattern, which separates data
access, business logic, and presentation logic. The MVC model also
enables the creation of a number of views accessing the same enterpriselevel application model.

Sikkim Manipal University

B2016

Page No. 28

Java and Web Design

Unit 1

Fig. 1.10 shows the interaction among the various MVC components:

Fig. 1.10: Interaction among the MVC Architecture Components

The figure shows that View retrieves the data of an application by using the
query methods of the Model. Whenever a user sends a request, it passes
through the Controller. The Controller intercepts the request from the View
and passes it to the Model for appropriate action. In addition, the Controller,
which is a Servlet, stores all the business logic. A View, normally a JSP
page, contains all the code for presentation. A Model is implemented by
using a pure Java or bean class. The different components in the
application, such as Controller, Model, and View, are reusable and make
applications highly scalable. Lets closely examine each component of the
MVC architecture.
1.7.1

Model

The Model displays the data on which an application is based. In a web


application, JavaBeans hold the data needed by a web application to
process user queries. Events sent to the Controller serve the basis for all
modifications to the data. The Model represents the data and the business
logic of the application. It is not associated with the presentation logic of the
application. The Model does not provide the specification for data access
logic. It performs its interfacing with other components by using a set of
public methods.
An application may have many states that need to be stored somewhere.
The Model of the application encapsulates all these states. It provides all
the functionalities and features of the application. The application behaves
Sikkim Manipal University

B2016

Page No. 29

Java and Web Design

Unit 1

according to the business logic of the application implemented by the


Model. Any change in the state of the Model is notified immediately, and this
change is reflected in all Views.
Lets now examine the View component of MVC architecture.
1.7.2

View

The View provides the Graphical User Interface (GUI) for Model. A user
interacts with the application through the View, which displays the
information based on the Model and allows the user to alter data. The Model
can have multiple Views. The information provided by the Model has
different meanings for different users and is interpreted by them in different
ways. The View is responsible for displaying the information to users in a
form that is understandable to them.
The Model manages the database, the content of which can be changed
and updated frequently. Any change in the database must be reflected to
the user as soon as possible. Therefore, the View communicates with the
Model to get information, if any change takes place in the database. Users
who want to modify the content of Model do not communicate with it directly.
Instead, they communicate with the Model through View, which
communicates with the Controller regarding the user input. The Controller
then makes the required changes in the Model and also notifies the other
associated Views.
Lets now examine the Controller component of MVC architecture.
1.7.3

Controller

The Controller controls all the Views associated with a Model. When a user
interacts with View and tries to update the Model, the Controller invokes
various methods to update the Model. The Controller of an application also
controls the data flow and transfers the requests between Model and View.
The behaviour of a web application is determined by the behaviour of its
various MVC components. The interaction among the various components
is managed by the Controller, which, by controlling the flow among these
components, determines the way the application performs the intended
tasks.
Sikkim Manipal University

B2016

Page No. 30

Java and Web Design

Unit 1

Lets understand how the Controller performs its role when a user needs to
change the data stored in a Model. If the user wishes to change this data,
the user sends a request to the Controller, which consults the Model to
update all the Views. The following steps are followed to change the data:
1.

The user sends a request through an interface provided by the View,


which passes the request to the Controller.

2.

The Controller receives the input request.

3.

The Controller processes the request according to the Controller logic,


and if access to the Model is not required, the process moves to step.

4.

The Model is accessed and modified, if required. It then needs to notify


all the associated Views regarding the modification.

5.

The View presents a user interface according to the modified or original


Model.

6.

The View remains idle after the current interaction and waits for the
next interaction to begin.

This process is repeated again for every new request.


Self Assessment Questions:
23. What does the MVC architecture do?
a. It separates the core business model functionality of the web
application from the presentation and control logic of the
interface-specific application.
b. It separates data access, business logic, and presentation logic,
and enables the creation of a number of Views accessing the
same aforementioned enterprise-level application Model.
c. It separates data (Model) and user interface (View) from each
other
d. It ensures a coherent interaction with users

Sikkim Manipal University

B2016

Page No. 31

Java and Web Design

Unit 1

24. What is not a component of the MVC model?


a. Model
b. View
c. Client
d. Controller
25. Which of the following is a Servlet in the MVC model?
a. Model
b. View
c. Client
d. Controller
26. Which of the following is a JSP page in the MVC model?
a. Model
b. View
c. Client
d. Controller
27. What is implemented by using a pure Java or bean class?
a. Model
b. View
c. Client
d. Controller
28. In the MVC architecture, what displays the data on which an
application is based?
a. Model
b. View
c. Client
d. Controller

Sikkim Manipal University

B2016

Page No. 32

Java and Web Design

Unit 1

29. In the MVC architecture, what provides the Graphical User Interface
(GUI) for Model?
a. Model
b. View
c. Client
d. Controller
30. To which component of the MVC architecture does the user send a
request if he/she wishes to change data?
a. Model
b. View
c. Client
d. Controller
Activity 6:
Choose any web application, examine its web architecture and write a
note on it.
Objective of the activity: To enhance your understanding of web
architecture models
This activity should not take you more than 15 minutes to complete.

1.8

Summary

In this unit, you have learnt that:

All the public web sites connected to the Internet worldwide are referred
to as the World Wide Web. It is a system of connected
hypertext documents that can be retrieved using the Internet.

A web browser is the application software that allows users to access


and navigate the World Wide Web by making request to the web server
for a particular task, which is then processed by the server and the
desired result is sent to the web browser.

Sikkim Manipal University

B2016

Page No. 33

Java and Web Design

Unit 1

URL stands for Uniform Resource Locator, which defines the unique
Internet address of a file stored on a host computer connected to the
Internet.

The DNS is a worldwide system of servers that contains pointers to the


websites. It provides alphanumeric addresses (URL) that can be easily
remembered by the users.

HTTP is the main protocol used for transmitting data on the web. It
defines how the computers on the web or Internet, specifically the server
and client, exchange data.

Java is a programming language that is completely independent from


the physical platform on the level of translated code. It is the global
standard for creating and providing every type of web application.

Java Servlet and JSPs) are the two frequently used Java based web
technologies that allow you to develop web applications. Both Java
Servlet and JSP use Java Database Connectivity (JDBC) for handling
database operations in web applications.

Web architecture models provide a standard way of flow control in an


application and describe the technologies used in different parts of the
application.

Web applications in Java are developed using two development models,


Model-1 Architecture and Model-2 Architecture.

MVC is another web architecture model. With the MVC architecture, it is


possible to have multiple views of the same data model of the web
application. Under MVC architecture, data (Model) and user interface
(View) are separated from each other so that any change in the user
interfaces does not affect the data in the database, and vice versa.

1.9

Glossary

Application Programming Interfaces (APIs): APIs allow us to build


programs that can run with web servers. They state the way by which
software modules should communicate with each other.

Sikkim Manipal University

B2016

Page No. 34

Java and Web Design

Unit 1

Common Gateway Interface (CGI): CGI is a standard method that is used


by web server software to delegate web content generation to executable
files that are created using a scripting language.
Domain Name System (DNS): The DNS is a worldwide system of servers
that contains pointers to the websites. DNS provides alphanumeric
addresses (URL) that can be easily remembered by the users.
HyperCard: HyperCard was an effective hypermedia system that preceded
the World Wide Web.
Hypertext Markup Language (HTML): HTML is the markup language for
designing web pages/content and displaying them in a web browser.
Internet Protocol (IP) Address: An IP address is a unique 32 bit logically
assigned binary number of four octets, which is represented in decimal
notation, designated to a particular device.
Integrated Development Environment (IDE): The IDE provides the tools
and environment needed to develop and deploy different types of
applications for developers.
Java Virtual Machine (JVM): JVM is a prerequisite to use Java and Java
based technologies. It is a virtual machine that is capable of executing Java
byte code.
The Hypertext Transfer Protocol (HTTP): HTTP is an application protocol
that states the way messages are formatted and conveyed, and the
activities that web servers and browsers should perform in response to
several commands.

1.10 Terminal Questions


1.

How was Berners-Lee's breakthrough a landmark in the evolution of the


World Wide Web?

2.

Examine the evolution of the URL and the Domain Name System. How
have they improved the access to Internet?

3.

Examine the role of protocol in web browsing with special reference to


HTTP.

Sikkim Manipal University

B2016

Page No. 35

Java and Web Design

Unit 1

4.

Is Java really better for web applications than other programming


languages? How?

5.

Critically compare Java Servlet and Java Server Page as web


technologies.

6.

Explain the process of request processing by JSP?

7.

Explain the JSP lifecycle. Underscore its role in handling and destroying
user requests.

8.

Examine the importance of web architecture models with reference to


Model-1 and Model-2 architectures.

9.

Is MVC architecture an improvement over Model-1 and Model-2


architectures? How?

1.11 Answers
Self Assessment Questions
1.

c.

It was developed by CERN, Switzerland.

2.

c.

1989

3.

d.

CERN

4.

a.

It requires unidirectional links, c. It marries hypertext to the Internet,


and d. It is non-proprietary

5.

b.

Lynx

6.

c.

Address bar

7.

a.

Can delete cookies automatically

8.

a.

Domain Name System (DNS)

9.

b.

Protocol

10. c.

Byte code

11. b.

Java Virtual Machine (JVM)

12. d.

Java Database Connectivity (JDBC)

13. b.

CGI

14. b.

Java servlet

Sikkim Manipal University

B2016

Page No. 36

Java and Web Design

15. a.

Unit 1

Servlets are faster than CGI,

b.

Servlets provide a high level API to programs, and

c.

Java servlet enhances the web server functionality

16. a.

Requirement of tag based instructions,

b.

High development time and cost of developing an application under


Java servlet, and

c.

Inability to use html designers services to prepare thepresentation


view under Java servlet

17. c.

Destroying

18. a.

They provide a standard way of flow control in an application,

b.

They describe the technologies used in different parts of the


application, and

c.

They facilitate the design process

19. b.

Two

20. a.

It leads to inflexibility and difficulty in maintaining applications

b.

It is suitable for large web applications, and

c.

It increases the effort required for maintaining JSP pages in an


application.

21. b.

It is suitable for large web applications.

22. d.

It allows each software component to perform similar task.

23. b.

It separates data access, business logic, and presentation logic,


and enables the creation of a number of Views accessing the
same aforementioned enterprise-level application Model.

24. c.

Client

25. d.

Controller

26. b.

View

27. a.

Model

28. a.

Model

Sikkim Manipal University

B2016

Page No. 37

Java and Web Design

29. b.

View

30. d.

Controller

Unit 1

Terminal Questions
1.

Refer to section 1.2 Overview of World Wide Web, which deals with the
evolution of the World Wide Web.

2.

Refer to sub section 1.3.3 of section 1.3 Overview of the Web


Browsers, which deals with the evolution of the URL and the Domain
Name System.

3.

Refer to sub section 1.3.3.1 of section 1.3 Overview of the Web


Browsers, which deals with HTTP and the role of protocol in web
browsing.

4.

Refer to section 1.4 Java: A Web Programming Language, which deals


with Java as a web programming language.

5.

Refer to section 1.5 Java-Based Web Technologies, which deals with


Java Servlet and Java Server Page as web technologies.

6.

Refer to sub section 1.5.2 JSPs of section 1.5 Java-Based Web


Technologies, which deals with request processing by JSP.

7.

Refer to sub section 1.5.2 JSPs of section 1.5 Java-Based Web


Technologies, which deals with the JSP lifecycle.

8.

Refer to section 1.6 Web Architecture Models, which deals with Model1 and Model-2 architectures.

9.

Refer to section 1.7 The MVC Architecture, which deals with the MVC
architecture.

References and Suggested Readings

Nino, J., & Hosch, F. A. (2001). An Introduction to Programming and


Object Oriented Design Using Java. New York: John Wiley & Sons.

E-References

http://www.oracle.com/technetwork/articles/javase/webapps-1138794.html

http://www.vogella.com/articles/JavaWebTerminology/article.html

http://www.w3.org/

Sikkim Manipal University

B2016

Page No. 38

You might also like