Architecture of JSPs
Download
Report
Transcript Architecture of JSPs
Java WWW Week 2
JSP structure and models
Format of lecture:
Assignment context
JSP models
JSPs calling other JSPs i.e. breaking up work
Parameter passing
JSPs with Add SQL examples
Summary
Version 2.2 2009
[email protected],
[email protected]
Slide 1
Java WWW Week 2
JSP Built-in Objects
JSPs have access to several objects that we
can use when processing a request from a
browser and then generate a dynamic
response.
There are eight in all:
request
out
response
pagecontext
session
application
config
page
Version 2.2 2009
[email protected],
[email protected]
Slide 2
Java WWW Week 2
Built-in objects
You will see these objects being used in the
examples provided during the module
coverage of JSPs.
Request objects methods allow us to access
information about:
The user
HTTP headers
Clients machine
Request URL and parameters.
Version 2.2 2009
[email protected],
[email protected]
Slide 3
Java WWW Week 2
Built-in objects
Response objects methods allow access to:
Setting HTTP headers.
Set cookies.
Encode session information into URLs.
Version 2.2 2009
[email protected],
[email protected]
Slide 4
Java WWW Week 2
Built-in objects
Out object - has methods to generate and
control output to the browser.
PageContext object - has page attributes.
Page object - represents the servlet instance
i.e. the object generated from having compiled
the JSP.
Application object - used for extracting
information about the lifecycle of the Servlet.
Version 2.2 2009
[email protected],
[email protected]
Slide 5
Java WWW Week 2
The Structure of JSPs
An initial selection of tags to get to know (Scriptlet
Tags = v. important!!!).
<% //embed Java code %>
Used for embedding blocks of Java code into JSPs.
Scriptlets provide control structures like decision (if
and switch) and iteration control structures (for and
while).
Version 2.2 2009
[email protected],
[email protected]
Slide 6
Java WWW Week 2
Structure of JSPs
Declaration Tags – also important.
<%! // Variable declaration - method declaration %>
Used for declaring variables and methods. For example:
<%! HttpSession ses = null; %>
<%! public String whereFrom(HttpServletRequest req)
{
ses = req.getSession();
return req.getRemoteHost();
}
%>
<% out.print("Hi there, I see that you are coming in from "); %>
<%= whereFrom(request) %>
Version 2.2 2009
[email protected],
[email protected]
Slide 7
Java WWW Week 2
Structure of JSPs
Expression Tags.
<%= java expression %>
Used for inserting the value of a Java
expression into JSPs.
e.g.
<td><%= rs.getString("LastName") %></td>
Version 2.2 2009
[email protected],
[email protected]
Slide 8
Java WWW Week 2
Structure of JSPs
Directive Tags – used to provide information to the JSP server.
There are three directives:
page
include
taglib
We will only review the page and include directive.
Page Directive - used to set properties of the JSP. E.g. what
packages it imports from the Java library or is an error page.
<%@ page page_attributes %> e.g.
<%@ page import="java.sql.*" %>
<%@ page errorPage=“myErrorPage.jsp" %>
Version 2.2 2009
[email protected],
[email protected]
Slide 9
Java WWW Week 2
Catching Errors – myErrorPage.jsp
<%@ page isErrorPage="true" %>
<HTML>
<HEAD><TITLE>My Error Page</TITLE></HEAD>
<BODY>
<H2>Exception Information</H2>
<TABLE>
<tr><td>Exception Class: </td><td><%= exception.getClass() %></td></tr>
<tr><td>Message: </td><td><%= exception.getMessage() %></td></tr>
</TABLE>
</BODY>
</HTML>
Version 2.2 2009
[email protected],
[email protected]
Slide 10
Java WWW Week 2
JSP Action Tags
Action tags are used to perform actions when a JSP page is requested
by a browser.
There are six JSP action tags:
useBean
setProperty
getProperty
Include
Forward
plugin
We will review two of them in during the module:
include
forward
Version 2.2 2009
[email protected],
[email protected]
Slide 11
Java WWW Week 2
include action and directive
Actions and Directives – there is a difference:
Actions are elements that can create and access programming
language objects and affect the output stream
include Action.
<jsp:include page= “Relative_URL” />
Used to include the output of another JSP
include Directive.
<%@ include file=“Relative_URL_to_file” %>
Used to include the text of another file
There are examples of both on the Tomcat server
Version 2.2 2009
[email protected],
[email protected]
Slide 12
Java WWW Week 2
include example
<body>
<%@ page buffer="5" autoFlush="false" %>
<p>In place evaluation of another JSP which gives
you the current time:
<jsp:include page="clock.jsp" flush="true">
<jsp:param name="type" value="small">
</jsp:include>
</html>
See: http://java.sun.com/products/jsp/tags/12/syntaxref1214.html
Version 2.2 2009
[email protected],
[email protected]
Slide 13
Java WWW Week 2
Action or Directive?
Well both have their pros and cons and, as always, it just depends on
what your requirements are. The benefits of using the <jsp:include/>
ACTION are:
Guarantees automatic recompilation,
Smaller class sizes,
Can make use of parameters (treated like form parameters)
JSP expressions can be used in attribute values
One of the main benefits of using the include DIRECTIVE is that local
page variables can be shared between the two files. It also has slightly
better run time efficiency and doesn't restrict output buffering.
Version 2.2 2009
[email protected],
[email protected]
Slide 14
Java WWW Week 2
forward Action
<jsp:forward page=“Relative_URL” />
Used to forward the request to another JSP
This will forward the current request object to the specified file
When a forward tag is encountered, the JSP engine doesn't process the
remainder of the current JSP file
Anything that has been written into the output buffer by the current JSP file
will be lost
The following example tests the virtual memory and either forwards to
another .jsp or another html page - see code screen grab on next page…
Version 2.2 2009
[email protected],
[email protected]
Slide 15
Java WWW Week 2
forward Action example
<%
double freeMem = Runtime.getRuntime().freeMemory();
double totlMem = Runtime.getRuntime().totalMemory();
double percent = 100*freeMem/totlMem;
if (percent < 50.0) {
%>
<jsp:forward page="/jsp/forward/one.jsp"/>
<% } else { %>
<jsp:forward page="two.html"/>
<% } %>
</html>
Version 2.2 2009
[email protected],
[email protected]
Slide 16
Java WWW Week 2
Forward model
Forward model
Client Request
HTML Form
HTTP Server - Tomcat
The JSP parses the input from
the client form (and optionally
updates the Database)
JSP
(optional)
SQL
HTML Form
JSP
Response
(optional)
DATABASE
Version 2.2 2009
[email protected],
[email protected]
Slide 17
Java WWW Week 2
Demos in server folder
CRUD examples
Create(add);Retrieve(view/get);
Adding information to a database
Demo of a add JSP (addtousers.jsp)
Retrieving (getting) information from the Guestbook
database with JSPs
Demo of a simple Get JSP (SimpleGetUserExample.jsp)
Demo of a user input version of get
(GetUserUsingInputExample.jsp)
Version 2.2 2009
[email protected],
[email protected]
Slide 18
Java WWW Week 2
addtousers.jsp
Version 2.2 2009
[email protected],
[email protected]
Slide 19
Java WWW Week 2
GetUserUsingInputExample.jsp
Version 2.2 2009
[email protected],
[email protected]
Slide 21
Java WWW Week 2
Summary
You have learnt more about the structure of
JSPs
Learning from examples is good
You have seen an Add something JSP
You have seen a more sophisticated view
something JSP (compared to last week with the
simple guest listing)
Version 2.2 2009
[email protected],
[email protected]
Slide 23