Developing JSPs

Download Report

Transcript Developing JSPs

j4www/jea Week 3
JSP models



Format of lecture:
Assignment context: CRUD - “update details”
JSP models

Multiple JSPs to handle a request.




JSPs calling other JSPs i.e. breaking up work
JSPs with Update SQL examples
Designing for JSPs
Summary
Version 2.3.2009
[email protected], edits:
nas1
Slide 1
j4www/jea Week 3
Multiple JSPs



Last week we introduced the two forms of the include tag.

Action - <jsp:include …> for dynamic content (although it can handle static
content too)

Directive - <%@ include …> for static content
For this module we are particularly interested in the dynamic
capabilities of the include action tag
The directive version is still useful, but it is not as powerful
Version 2.3.2009
[email protected], edits:
nas1
Slide 2
j4www/jea Week 3
Include directive in an JSP
<HTML>
<HEAD><TITLE>Including Text From Another File</TITLE></HEAD>
<BODY>
<%@ include file="SUBanner.html" %>
<div align="center">
<img SRC="trent.jpg" />
<h1>Welcome Open Day Students <br />
Hope You Enjoy The Day</h1>
</div>
</BODY>
</HTML>
Version 2.3.2009
[email protected], edits:
nas1
Slide 3
j4www/jea Week 3
Include directive result
Version 2.3.2009
[email protected], edits:
nas1
Slide 4
j4www/jea Week 3
Include action – very useful
Why are we more interested in the action
tag?
 The directive version is only used for static documents like
template JSP, HTML or images.
 The include action treats the included file as a dynamic
document - it includes the output of the included file instead
of including the code – this is interesting!
 We can now think about JSPs working together – the web
engineering concept of modular code or referred to as
component software.
Version 2.3.2009
[email protected], edits:
nas1
Slide 5
j4www/jea Week 3
The include action - why?
 Spreads out the work of an application (design
implication – see later slides).
 The example we will now show illustrates this:

It contains an HTML form for user entry of requests

passes them on to a primary JSP which is responsible for the
request and generating the response

the primary JSP uses the values received from the HTML form
to select the appropriate “worker” JSP to run (two JSPs we
have already seen).
 The model is depicted on the next slide.
Version 2.3.2009
[email protected], edits:
nas1
Slide 6
j4www/jea Week 3
Include action model

Include model
HTTP Server - Tomcat
HTML Form
Primary
JSP
The JSP parses the input from
the HTML form and includes the
appropriate JSP to service the
client request
SQL
JSPs
DATABASE
Client Request and
The included JSPs are translated into their Servlet code,
Response
compiled and then executed - it is the output of that
execution that is inserted into the output of the primary JSP
Version 2.3.2009
[email protected], edits:
nas1
Slide 7
<HTML>
<HEAD><TITLE>Using Guestbook Data</TITLE></HEAD>
<BODY BGCOLOR="cyan">
<CENTER> <FONT SIZE="5" COLOR="navy">HTML form with multiple JSPs to run</FONT></CENTER>
<FORM NAME="DataSelection"
ACTION="http://server etc:8080/includeguestbookexample.jsp"
METHOD="POST">
<TABLE WIDTH="100%" ALIGN="center">
<TR><TD>&nbsp;</TD></TR>
<TR ALIGN="center">
Call to the Primary JSP
<TD><INPUT TYPE="checkbox" NAME="list" VALUE="list" CHECKED>
List JSP
</TD>
</TR>
important
<TR ALIGN="center">
<TD><INPUT TYPE="checkbox" NAME="list" VALUE="search" CHECKED>
Get JSP
</TD>
</TR>
<TR><TD>&nbsp;</TD></TR>
<TR ALIGN="center">
<TD><INPUT TYPE="submit" NAME="btnSubmit" VALUE="Submit"></TD>
</TR>
</TABLE></FONT></FORM></BODY></HTML>
important
<html>
includeguestbookexample.jsp
<body>
<p> example of the include action - used to include the output of another JSP
<%@ page import="java.util.*" %>
<%
Enumeration params = request.getParameterNames();
while (params.hasMoreElements()) {
String inputName = (String)params.nextElement();
String inputValue = request.getParameter(inputName);
if (inputValue.equals("list")) {
System.out.println('\n' + inputValue); //output the value %>
<jsp:include page="SimpleGuestList.jsp" flush="true" />
<%
} else if (inputValue.equals("search")){
Worker JSP
%>
<jsp:include page="getusinginputexample.jsp" flush="true" />
<% } // end else clause
} // end while loop
%>
</p>
</html>
j4www/jea Week 3
So……
 The primary JSP includes the “output” of other “worker” JSPs
 In this example a sort of menu has been achieved
 It is the primary JSP’s role to accept the request from the user
and generate a response to the user which can be done by using
the include action
 There is no limit to the number of JSPs which can be used - nice
scalable solution - separating out work! - if a new feature/menu
item is needed - just add another JSP
 There are some issues about the work organisation however and we
will discuss this later in the module when we look at the MVC
concept
Version 2.3.2009
[email protected], edits:
nas1
Slide 10
j4www/jea Week 3
The update problem
 How to tackle this problem?
 What do we need to do?
 Design some steps
 Understand the SQL we can use
 Solution using the Guests example
 We need to:
 Allow the user to select a guest record
 Let the user view it
 Allow the user to modify it and then write it back to the database i.e.
update it
 Are there any aspects we don’t wish to allow the user to do?
Yes – they cannot alter the primary key
Version 2.3.2009
[email protected], edits:
nas1
Slide 11
j4www/jea Week 3
The update solution
 So…….we need …..
 A form that can call a jsp which simply displays the result
from the fetch in a set of text boxes rather than a table format
– this allow the user to do something with the data (those who
are new to HTML will need to think about this)
 We can then allow the user to:
 change the content of the text box for firstname and then
 call a jsp to do the update to the database and then
 call another jsp to display the confirmation or return to our
starting point
Version 2.3.2009
[email protected], edits:
nas1
Slide 12
j4www/jea Week 3
UPDATE SQL
A JSP which can use the parameters from the
update form and call a JSP to update the
database
Use the Update SQL command
Version 2.3.2009
[email protected], edits:
nas1
Slide 13
j4www/jea Week 3
Demo of update
Now follows
http://fcet11:8080/nas1/examples/updateGuests.html
We have put the code in a zip file linked into
the schedule
Copy the HTML, 2 JSPs and a DB into your
folder and modify the connection string to the
access the database
Let us run it and then have a look at the code
Version 2.3.2009
[email protected], edits:
nas1
Slide 14
j4www/jea Week 3
Assignment context
You can use the same approach to modify
and update something in the database
For example an FAQ...
You now have from the opening 3 weeks of
the module
How to CRU
 How to add (create) something
 How to view something (retrieve)
 How to change something (update)
Version 2.3.2009
[email protected], edits:
nas1
Slide 15
j4www/jea Week 3
Summary
You have seen an update example
This type of web design starts to look like a
service oriented architecture (SOA),
JSPs that will service other JSPs or HTML
forms given the appropriate information
Version 2.3.2009
[email protected], edits:
nas1
Slide 16