An Introduction to Control Structures

Download Report

Transcript An Introduction to Control Structures

Building Web Applications
Chapter 13
Java Programming: Advanced Topics
1
Objectives
• Review the way the Web works and the
non-Java technology that participates in
Web applications
• Learn how J2EE packages Web
applications
• Program dynamic Web content in servlets
• Become familiar with the Servlet API
• Provide continuity as the user navigates
through your Web application
Java Programming: Advanced Topics
2
Objectives (Cont.)
• Learn how to generate dynamic Web
content in JavaServer Pages
• Design Web applications based on
servlets and JavaServer Pages
• Use the JavaServer Page tags and
Servlet API
• Apply design patterns and frameworks to
Web applications
• Discuss design issues related to Web
applications
Java Programming: Advanced Topics
3
The Technology of the Web
• Web apps are composed of Web and Java
technologies
• A Web app requires a Web server and an
application server on the server side and a Web
browser on the client side.
• Servlets and JavaServer Pages (JSPs) are the
components that form the bridge between Web
pages and Java application code
Java Programming: Advanced Topics
4
Web Relationships
Java Programming: Advanced Topics
5
The HTTP and HTTPS Protocols
• The browser communicates with a Web server
HyperText Transfer Protocol (HTTP) or
HyperText Transfer Protocol Secure (HTTPS)
• In a Web application, the content of HTML or
XHTML pages can be dynamically created at
runtime by Java components that run on the
server side
• A Web container provides the context within
which servlets and JSPs run
Java Programming: Advanced Topics
6
HTTP Request Methods
Java Programming: Advanced Topics
7
J2EE Web Application Packaging
• To be deployed on an application server, a J2EE
Web app must be packaged in a Web archive
(war file) that packaged in an enterprise
application archive (ear file)
• The ear and war files contain deployment
descriptors
– Deployment descriptors: XML files that describe your
Web app for the application server and request runtime services from the application server
Java Programming: Advanced Topics
8
Servlets
• A servlet is a server-side Java program that runs
in response to an HTTP request
• The role of a servlet is to accept requests from
the client, invoke the appropriate application
logic to fulfill the request, and return the results
to the client
• Each servlet is an entry point into a Web app or
an enterprise application running a J2EEcompliant application server
Java Programming: Advanced Topics
9
How a Web App Processes HTTP
Requests
Java Programming: Advanced Topics
10
Named and Anonymous Servlets
• Named servlet: when a servlet is assigned a
name in the deployment descriptor
• Anonymous servlet: a servlet not listed in the
Web deployment descriptor
• To protect your servlets from unauthorized use,
make all servlets named servlets, disable
anonymous servlets, and apply security settings
to your Web app
Java Programming: Advanced Topics
11
The Servlet API
• The Servlet API is included in two
packages:
– javax.servlet
– javax.servlet.http
• Servlet classes extend
javax.servlet.http.HttpServlet
Java Programming: Advanced Topics
12
Common Types in the Servlet API
Java Programming: Advanced Topics
13
Common Types in the Servlet API
(Cont.)
Java Programming: Advanced Topics
14
A Simple Servlet
Java Programming: Advanced Topics
15
The User Experience: Building a
Web App with Continuity
• The HTTP protocol is stateless, so every
request and response is a complete,
independent transaction
• The Servlet API provides a simple
mechanism to save information on the
server side or on the client side to provide
some continuity between browser sessions
Java Programming: Advanced Topics
16
Storing Data in HTTP Sessions
• A session is a place to store state information
for a specific client, so that the information is
available to different servlets
• You can access or create a session for a
client by calling the
HttpServletRequest.getSession method
• An HttpSession object contains a collection
of name-value pairs
Java Programming: Advanced Topics
17
Places to Store State Data on the
Server Side
• A servlet can store data in and retrieve it from
four different scopes:
– Data stored in the session scope is specific to one
client.
– Data stored in the application scope is global to the
Web app.
– The lifetime of data stored in the request scope is the
duration of one HTTP request-response cycle.
– Data stored in the page scope is accessible only in
the current page
Java Programming: Advanced Topics
18
Providing Continuity with Cookies
• You can also use Cookie objects to store data
on the client side from one browser session to
the next
• Cookies: data objects stored on the client side of
a Web app and passed back and forth between
the Web browser and the server
• URL rewriting appends the cookie data to the
URL
Java Programming: Advanced Topics
19
JavaServer Pages
• A JSP is Web resource with embedded
Java code
• A JSP takes the form of an HML or XHMTL
document that includes ordinary HTML
tags and some additional JSP-specific
tags
• The application server converts the entire
page to HTML and resolves all dynamic
content before sending the page to the
client browser
Java Programming: Advanced Topics
20
Implementing MVC with Servlets
and JSPs
• The MVC design pattern:
– The view layer consists of static HTML pages and
JSP documents
– The controller layer consists of servlets that receive
HTTP requests
– The model layer consists of classes and other
components that perform the core functionality of the
application
Java Programming: Advanced Topics
21
Implementing MVC with Servlets
and JSPs
Java Programming: Advanced Topics
22
JSP Tags and API
• JSPs that conform to HTML or XHTML syntax
have four types of tags:
– Java directives are used to import packages and set
other parameters of the page as a whole
– Declarations define methods and variables used by
Java code in the JSP
– Java expressions are evaluated, and its string
representations of the value are inserted into the
resulting HTML
– Scriptlets contain one or more complete Java
statements or blocks of code
Java Programming: Advanced Topics
23
HTML-Based JSP Tags
Java Programming: Advanced Topics
24
HTML-Based JSP Tags (Cont.)
Java Programming: Advanced Topics
25
HTML-Based JSP Tags (Cont.)
Java Programming: Advanced Topics
26
HTML-Based JSP Tags (Cont.)
Java Programming: Advanced Topics
27
A Simple JSP
Java Programming: Advanced Topics
28
An XML JSP
Java Programming: Advanced Topics
29
How the Server Processes JSPs
• The Web container converts a JSP into a servlet
using the page compile process
• The Web container generates a service method
from the body of the JSP source
• By default, a JSP is compiled when it is first
called
• If you change the .jsp file between calls, the
server reads the source page and compiles,
loads, and runs it again
Java Programming: Advanced Topics
30
Java Coding in JSPs
• In a servlet, the doGet, doPost, and service
methods receive the HTTP request and
response objects as arguments and access the
servlet context by calling methods on those
arguments
• The JSP can call the getAttribute method on the
variables application, session, or request to
access objects stored there
Java Programming: Advanced Topics
31
Implicit Objects in JSPs
Java Programming: Advanced Topics
32
How to Retrieve Data
Java Programming: Advanced Topics
33
Custom Tags in JSPs
• Custom tags can help remove all Java code
from JSPs
• Custom tags can have attributes
• Custom tags are grouped into libraries, and
each library has a unique prefix
• For every tag library, there is a descriptor file
identified by the uri attribute of the taglib
directive
Java Programming: Advanced Topics
34
JSP Tags for JavaBeans
Java Programming: Advanced Topics
35
JSP Tags for JavaBeans (Cont.)
Java Programming: Advanced Topics
36
Frameworks for Building Web
Applications
• Frameworks are productivity aids for creating
resources that are common requirements in
Web apps
• Frameworks can incorporate best practices for
Web app design so that the Web apps they
produce are extensible, easy to maintain, and
lend themselves to being made secure
Java Programming: Advanced Topics
37
Building Robust Web Apps
• Servlets should be designed for multithreaded
use
• Turn off caching for all pages and responses
that include dynamic content
• Build precondition checks for servlets or JSPs
• Use JavaScript to disable the submit button after
the first click or program the servlet to record the
start and stop of processing as session state
data
Java Programming: Advanced Topics
38
Summary
• In a Web application, the content of HTML or
XHTML pages can be dynamically created at
runtime by Java components that run on the
server side
• A Web container provides the context within
which servlets and JSPs run
• A J2EE Web app must be packaged in a Web
archive (war file)
• A servlet is a server-side Java program that
runs in response to an HTTP request
• The Servlet API is included in javax.servlet and
javax.servlet.http packages
Java Programming: Advanced Topics
39
Summary (Cont.)
• HTTP requests and responses are stateless
• You can use Cookie objects to store data on the
client or session objects on the server
• A servlet can store data in and retrieve it from
global, session, request, and page scopes
• JSPs are HTML, XHTML, or XML documents
that contain snippets of Java code
• JSP use directives, declarations, expressions,
scriptlets, and custom tags
• For Web apps use the MVC design pattern
Java Programming: Advanced Topics
40