Java Lanjut
Java Lanjut
Java Lanjut
2
6
Web Applications:
Part 1
OBJECTIVES
In this chapter you will learn:
Web application development using Java
Technologies and Java Studio Creator 2.0.
To create JavaServer Pages with JavaServer
Faces components.
To create web applications consisting of multiple
pages.
To validate user input on a web page.
To maintain state information about a user with
session tracking and cookies.
26.1 Introduction
26.2 Simple HTTP Transactions
26.3 Multitier Application Architecture
26.4 Java Web Technologies
26.4.1 Servlets
26.4.2 JavaServer Pages
26.4.3 JavaServer Faces
26.4.4 Web Technologies in Java Studio Creator 2
26.5 Creating and Running a Simple Application in Java
Studio Creator 2
26.5.1 Examining a JSP File
26.5.2 Examining a Page Bean File
26.5.3 Event-Processing Life Cycle
26.5.4 Relationship Between the JSP and Page Bean
Files
26.1 Introduction
Web-based applications create web content for
web browser clients
AJAXprovides interactivity and responsiveness
users typically expect of desktop applications
Fig. 26.1 | Client interacting with web server. Step 1: The GET request.
Fig. 26.2 | Client interacting with web server. Step 2: The HTTP response.
26.4.1 Servlets
Use the HTTP request-response model of
communication between client and server
Extend a servers functionality by allowing the
server to generate dynamic content
Servlet container executes and interacts with
servlets
Packages javax.servlet and
javax.servlet.http contain the servlet
classes and interfaces.
26.4.1 Servlets
Servlet container
Receives HTTP requests from clients
Directs each request to the appropriate servlet
Servlet processes the request and returns response to the
clienttypically an XHTML or XML document
Servlets implement the Servlet interface of
package javax.servlet
Ensures that each servlet can execute in the framework
Declares methods used by the servlet container to manage
the servlets life cycle
26.4.1 Servlets
Servlet life cycle
Begins when the servlet container loads it into memoryusually
in response to the first request for the servlet
init method
- Called only once by container during a servlets life-cycle to initialize
the servlet
service method
- Called once per request
- Receives the request
- Processes it
- Sends a response to the client
destroy method
- Called to release any resources held by the servlet when container
terminates servlet
Time.jsp
Tag libraries available for
use in this web application (1 of 2 )
(2 of 2 )
Time.java
(2 of 8 )
Time.java
(3 of 8 )
Time.java
(4 of 8 )
(5 of 8 )
Time.java
(6 of 8 )
Time.java
(7 of 8 )
Time.html
(2 of code
HTML 2 ) that
was generated for a
StaticText
object
Fig. 26.11 | JSP file generated for Page1 by Java Studio Creator 2.
Fig. 26.12 | Page bean file for Page1.jsp generated by Java Studio Creator 2.
Fig. 26.13 | Time.jsp after inserting the first Static Text component.
Fig. 26.14 | Time.jsp after adding the second Static Text component.
Text Field
Used to obtain text input from the user
Components JSP tags are added to the JSP file in the
order they are added to the page.
Tabbing between components navigates the components
in the order in which the JSP tags occur in the JSP file
To specify navigation order
Drag components onto the page in appropriate order, or
Set each input fields Tab Index property; tab index 1 is the
first in the tab sequence
Designer generated
code for Grid
WebComponents
Panel
.jsp
Designer
(2 of 5 )generated
code for Text
Field
Designer generated
WebComponents
code for Drop
.jsp
Down List
(3 of 5 )
Designer generated
code for
Hyperlink
Designer generated
WebComponents
code for Button
.jsp
(4 of 5 )
WebComponents
.jsp
(5 of 5 )
Specifies the
(2 of 6 )
validator for the
email Text Field
Specifies the
validator for the
phone number
Text Field
Validation.jsp
(3 of 6 )
Validation.jsp
(4 of 6 )
Validation.jsp
(5 of 6 )
Validation.jsp
(6 of 6 )
26.7.1 Cookies
Cookie
A piece of data typically stored in a text file on the users
computer
Maintains information about the client during and between
browser sessions
When a user visits the website, the users computer might
receive a cookie
This cookie is then reactivated each time the user revisits
that site
HTTP-based interactions between a client and a server
Includes a header containing information either about the
request (when the communication is from the client to the
server) or about the response (when the communication is
from the server to the client)
26.7.1 Cookies
In a request, the header includes
Request type
Any cookies that have been sent previously from the server to be
stored on the client machine
In a response, the header includes
Any cookies the server wants to store on the client computer
Other information, such as the MIME type of the response
Expiration date determines how long the cookie remains
on the clients computer
If not set, the web browser maintains the cookie for the browsing
sessions duration
26.7.1 Cookies
Setting the action handler for a Hyperlink
enables you to respond to a click without
redirecting the user to another page
To add an action handler to a Hyperlink that
should also direct the user to another page
Add a rule to the Page Navigation file
Right click in the Visual Designer and select Page
Navigation, then drag the appropriate Hyperlink to
the destination page
26.7.1 Cookies
A cookie object is an instance of class Cookie in package
javax.servlet.http
An HttpServletResponse (from package
javax.servlet.http) represents the response
This object can be accessed by invoking the method
getExternalContext on the page bean, then invoking
getResponse on the resulting object
An HttpServletRequest (from package
javax.servlet.http) represents the request
This object can be obtained by invoking method
getExternalContext on the page bean, then invoking getRequest
on the resulting object.
HttpServletRequest method getCookies returns an array of
the cookies previously written to the client.
Web server cannot access cookies created by servers in other domains
Validation.java
(1 of 5 )
Validation.java
(2 of 5 )
Validation.java
(3 of 5 )
Validation.java
(4 of 5 )
Custom email
validation via
regular expression
(5 of 5 )
Options.jsp
(2 of 4 )
Options.jsp
(3 of 4 )
Options.jsp
(4 of 4 )
Options.java
(1 of 6 )
Options.java
(2 of 6 )
Options.java
(3 of 6 )
Options.java
(4 of 6 )
Options.java
(5 of 6 )
Creates the
Cookie object
Options.java
(6 of 6 )
Recommendations .
jsp
(1 of 2 )
Recommendations .
java
(1 of 4 )
Recommendations .
java
(2 of 4 )
Recommendations .
java
(3 of 4 )
Obtains the
Cookie object(s)
from the request
Options.jsp
(1 of 5 )
Options.jsp
(2 of 5 )
Options.jsp
(4 of 5 )
Options.jsp
(5 of 5 )
Fig. 26.27 | New Property dialog for adding a property to the SessionBean.
SessionBean.java
(1 of 3 )
SessionBean.java
(2 of 3 )
SessionBean.java
(3 of 3 )
Manually coded
Properties
object
Options.java
(2 of 6 )
Options.java
(3 of 6 )
Options.java
(4 of 6 )
Options.java
(5 of 6 )
Add a
recommendation
Options.java
(6 of 6 )
Recommendations .
jsp
(1 of 2 )
Recommendations .
java
(1 of 4 )
Recommendations .
java
(2 of 4 )
Recommendations .
java
(3 of 4 )
(4 of 4 )