Chapter 2 - SaigonTech
Download
Report
Transcript Chapter 2 - SaigonTech
Chapter 2
An Overview of
Servlet and JSP
Technology
Contents
1. Building Web Pages Dynamically
2. Understanding the Role of
Servlets
3. Understanding the Role of JSPs
4. Servlet Jobs
5. Comparing Servlet and JSP
1. Building Web Pages Dynamically
The Web page is
based on data sent
by the client
The Web page is
derived from data
that changes
frequently
2. Understanding the Role of Servlets
2.1. Servlets
2.2. Problem: Displaying Even Numbers
2.1. Servlets
Java programs that run on Web or
application servers
The role of web middleware
Acting as a middle layer between
requests coming from Web browsers
or other HTTP clients and databases
or applications on the HTTP server
2.2. Problem: Displaying Even Numbers
Develop a servlet that shows all even
numbers from 1 to 100 as shown below:
Solution
Project name: EvenNumbers
Package name: evennumbers
2.2.1. Developing the Servlet Class
2.2.2. Deploying the Servlet to the Web
Server
2.2.1. Developing the Servlet Class
2.2.2. Deploying the Servlet to the Web Server
Save EvenNumbersServlet.class to
TOMCAT_DIR/webapps/ROOT/WEBINF/classes/evennumbers/
Declaring EvenNumbersServlet in web.xml
3. Understanding the Role of JSPs
3.1. JSPs
3.2. Problem: Displaying Even Numbers
3.1. The role of JSP
HTML pages with Java code embedded inside
Simplifying the creation and maintenance of
the HTML
3.2. Problem: Displaying Even Numbers
Develop a JSP that shows all even numbers
from 1 to 100 as shown below:
Solution
Save EvenNumbers.jsp to
TOMCAT_DIR/webapps/ROOT
4. Servlet Jobs
Read the explicit data sent by the client.
Read the implicit HTTP request data sent by
the browser.
Generate the results.
Send the explicit data (i.e., the document) to
the client.
Send the implicit HTTP response data.
5. Comparing Servlet and JSP
Servlet
JSP
Java programs
with HTML
embedded inside
of them
Mostly like a
regular Java class
The servlets get
compiled
HTML pages with Java
code embedded inside
of them
Run at request
time
Run at request time
Mostly like a normal
HTML page
JSP pages get translated
into servlets
Using Servlet and JSP
technology
JSP is focused on simplifying the creation and
maintenance of the HTML
Servlets are best at invoking the business logic and
performing complicated operations
Servlets are best for tasks oriented toward
processing, whereas JSP is best for tasks oriented
toward presentation
For still others, neither servlets alone nor JSP alone is
best, and a combination of the two (see Chapter 15,
"Integrating Servlets and JSP: The Model View
Controller (MVC) Architecture") is best.
References
1. Core Servlets And Java Server Pages,
Volume 1, 2nd Edition, Chapter 2
Practice Exercises
1.
2.
3.
Create a JSP page that shows all of characters from
‘A’ to ‘Z’
Create a JSP page that show all prime numbers
from 1 to 100.
Create a Servlet page that show all perfect
numbers from 1 to 1000. (a perfect number is a
positive integer that is equal to the sum of its
proper positive divisors, that is, the sum of its
positive divisors excluding the number itself. For
example: 6 = 1 + 2 + 3, 28 = 1 + 2 + 4 + 7 + 14)