... Building a Web Application
•
Modify and add files to create web site
index .jsp
Prompts user for
number of widgets
reciept .jsp
Displays number of
widgets purchased
Deploying your Site to Tomcat
•
Right-click ... site
Structure shown
in the project
window
Creates an
initial index .jsp
page (default
home page of
the new site)
The index .jsp is initially just a
“Hello world” page
B...
... library
22
Commenting JSP Files
•
Crucial to future maintenance of site
•
Inside of JSP code (between <% and %>):
// comment
/* comment */
•
Outside of JSP code (that is, in html)
<! comment >
8
JSP ... 10
JSP Scoping
•
Variable declared in a
block of JSP on a page
•
May be accessed by
any other JSP on same
page
•
No access to variable
from any ... code
Data from...
... etc.).
–
Decide which JSP should be sent back as response.
–
Forward control (and request data) to that JSP.
request
Control
servlet
JSP
response
JSPs act
as views
JSP
JSP
Servlet Details
•
Important ... JSP)
–
Methods invoked by a request for the servlet
–
Usually redirects to a JSP
Tomcat
Request for servlet
Servlet
Servlet methods called
JSP
Redirect to
appropriate
respo...
... context.getInitParameter(“name");
•
Accessing from a JSP:
–
String value = application.getInitParameter(“name");
Built-in object in JSP
(like “request”)
Context Parameters
Form of xml tag
Welcome ... Parameters
Form of xml tag
Welcome Page Properties
•
Page shown at webapp startup
–
Default: index .jsp
•
Can change to a new welcome file
Configuration Files
•
Each webapp has a...
... approach:
Force JSP to repeat validation done in servlet to determine which
messages to display
•
Better approach:
Once servlet detects error, it creates error message and passes to
JSP as attribute
Servlet
Detects ... type=“text” name=“fieldname”
value= “<%= fieldValue %>” >
Displaying Error Messages in JSP
Calendar Dates in Java
•
Construct a new GregorianCalendar object...
... other JSPs test whether attribute is null
•
If so, redirect to another page
–
Initial page in sequence
–
Error page telling session has expired
•
Syntax for redirection from JSP:
< ;jsp: forward ... Data
•
Servlets/JSPs can store data associated with session ID
•
Servlets/JSPs can look up that data in future when
passed the session ID in request
Server
session ID Client data
Servlet or...
... list
Used together to
loop through and
show all Items on
“shopping cart” page
Getting Cost in JSP
Order Class
Business Model Objects
Key idea:
•
Methods in model objects should implement
business ... set/get quantity
–
Method to get total cost of order
•
Computed from quantity here instead of in JSP/ servlet
–
Method to check whether quantity valid in terms of
business model
•
Servlet...