jsp and servelets. how does jsp work? not a java scripting language not like php –jsp are not...

74
JSP and Servelets

Upload: noah-mackay

Post on 28-Mar-2015

217 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: JSP and Servelets. How does JSP work? NOT a Java scripting language NOT like php –JSP are NOT parsed on request Java code must involve classes, creation

JSP and Servelets

Page 2: JSP and Servelets. How does JSP work? NOT a Java scripting language NOT like php –JSP are NOT parsed on request Java code must involve classes, creation

How does JSP work?

• NOT a Java scripting language

• NOT like php– JSP are NOT parsed on request

• Java code must involve classes, creation of objects, etc…

• JSP is a designer-friendly way of writing servlets

Page 3: JSP and Servelets. How does JSP work? NOT a Java scripting language NOT like php –JSP are NOT parsed on request Java code must involve classes, creation

Clock example

Page 4: JSP and Servelets. How does JSP work? NOT a Java scripting language NOT like php –JSP are NOT parsed on request Java code must involve classes, creation

Server withTomcat

WebContainer

client

translationrequest processing

GET clock.jsp

1clock.jsp

read2

Serveletclock.java

generate

3

clock.class

compile and deploy

4

execute

5

http response

6

Page 5: JSP and Servelets. How does JSP work? NOT a Java scripting language NOT like php –JSP are NOT parsed on request Java code must involve classes, creation

public class clock implements Servlet { public void service (ServletRequest r, ServletResponse s) throws ServletException, IOException { s.setContentType (“text/html”); PrintWriter out = s.getWriter (); out.println (“<HTML>”); out.println (“<HEAD>”); out.println (“<TITLE>JSP… </TITLE>”); out.println (“</HEAD>”); out.println (“<BODY>”);

Page 6: JSP and Servelets. How does JSP work? NOT a Java scripting language NOT like php –JSP are NOT parsed on request Java code must involve classes, creation

out.println(“<H1>Date and Time</H1>”);

out.println(new

java.util.Date.toString()); out.println (“</BODY>”); out.println (“</HTML>”); }}

Page 7: JSP and Servelets. How does JSP work? NOT a Java scripting language NOT like php –JSP are NOT parsed on request Java code must involve classes, creation

JSP directive elements

• applied when the JSP is compiled into a servelet– Only executed once (on compilation)– Do not affect the response

• Used to set up resources such as– Java classes– inclusions

Page 8: JSP and Servelets. How does JSP work? NOT a Java scripting language NOT like php –JSP are NOT parsed on request Java code must involve classes, creation

JSP directive elements

• specify page information (static)

<%@ page … >scripting language, error page

<%@ include … >includes a file, e.g. an applet

<%@ taglib … >declare a tag library (custom actions)

Page 9: JSP and Servelets. How does JSP work? NOT a Java scripting language NOT like php –JSP are NOT parsed on request Java code must involve classes, creation

JSP and http

Page 10: JSP and Servelets. How does JSP work? NOT a Java scripting language NOT like php –JSP are NOT parsed on request Java code must involve classes, creation

JSP and http

• A JSP is a servelet

• Permanently resident in server memory

• Multi-threaded

• Request and response objects

• Sessions and cookies

Page 11: JSP and Servelets. How does JSP work? NOT a Java scripting language NOT like php –JSP are NOT parsed on request Java code must involve classes, creation

Accessing request information

• Methods of the request object provide all request information

• object is called “request”

public String getParameter (String name)

public String getMethod ()

public String getHeader (String name)

public Cookie [] getCookies ()

Page 12: JSP and Servelets. How does JSP work? NOT a Java scripting language NOT like php –JSP are NOT parsed on request Java code must involve classes, creation

javax.servelet.http.Cookie class• getName ()

– the name of the cookie

• getValue(), setValue (String value)– gets/sets the value of a cookie

• getDomain(), setDomain(String dName)– get/set the cookie domain name

• getPath(), String setPath(String path)– get/set the request path the cookie is associated with

• getMaxAge(), setMaxAge (int expiry)– get/set maximum age for the cookie

Page 13: JSP and Servelets. How does JSP work? NOT a Java scripting language NOT like php –JSP are NOT parsed on request Java code must involve classes, creation

javax.servelet.http.HttpSession

• provides standard functionality for handling sessions

• handles cookies as standard but must be extended to handle URL rewriting

• holds client state info resident in memory– automatically times out abandoned sessions

• created/returned by HttpServeletRequest class getSession method

Page 14: JSP and Servelets. How does JSP work? NOT a Java scripting language NOT like php –JSP are NOT parsed on request Java code must involve classes, creation

JSP and Java Beans

Page 15: JSP and Servelets. How does JSP work? NOT a Java scripting language NOT like php –JSP are NOT parsed on request Java code must involve classes, creation

Java Beans

• ordinary Java classes with the following properties:– introspection– customization– events– properties– persistence

Page 16: JSP and Servelets. How does JSP work? NOT a Java scripting language NOT like php –JSP are NOT parsed on request Java code must involve classes, creation

Java Beans

• introspection– an analyser can inspect how the Bean works

• properties– naming conventions for getter and setter methods

• persistence– implement the Serializable interface– Bean state can be stored

Page 17: JSP and Servelets. How does JSP work? NOT a Java scripting language NOT like php –JSP are NOT parsed on request Java code must involve classes, creation

Example Java beanpublic class ExampleBean implements

java.io.Serializable {private String name = null;private int score = 0;

public ExampleBean() {} // Empty constructor

/* Getter and Setter Methods */public String getName() {

return name;}

public void setName(String s) {name = s;

}

Page 18: JSP and Servelets. How does JSP work? NOT a Java scripting language NOT like php –JSP are NOT parsed on request Java code must involve classes, creation

Example Java bean

public int getScore() {return score;

}

public void setScore(int i) {score = i;

}

/* No method required to implement

Serializable*/

}

Page 19: JSP and Servelets. How does JSP work? NOT a Java scripting language NOT like php –JSP are NOT parsed on request Java code must involve classes, creation

JSP action elements

• action elements– perform an action when page is requested

<jsp:useBean>uses a JavaBean component

<jsp:getProperty>property from JavaBean used in the page

<jsp:setProperty>sets a JavaBean property (possibly

using request information)

Page 20: JSP and Servelets. How does JSP work? NOT a Java scripting language NOT like php –JSP are NOT parsed on request Java code must involve classes, creation

<jsp:useBeanid="userInfo"

class="com.ora.jsp.beans.userInfo.UserInfoBean“>

<jsp:setPropertyname = “userInfo”property = “userName”value = “Gandalf”/>

</jsp:useBean>

Page 21: JSP and Servelets. How does JSP work? NOT a Java scripting language NOT like php –JSP are NOT parsed on request Java code must involve classes, creation

The following information was saved:

<ul><li>User Name:<jsp:getProperty

name="userInfo"property="userName"/></li>

<li>Email Address:<jsp:getProperty

name="userInfo"property="emailAddr"/></li>

</ul></body></html>

Page 22: JSP and Servelets. How does JSP work? NOT a Java scripting language NOT like php –JSP are NOT parsed on request Java code must involve classes, creation

Other JSP action elements

<jsp:include>responses from other jsp pages or servelets

<jsp:forward>forwards processing to other jsp or servelet

<jsp:param>passes a parameter with include or forward

<jsp:plugin>generates the HTML to embed an applet

Page 23: JSP and Servelets. How does JSP work? NOT a Java scripting language NOT like php –JSP are NOT parsed on request Java code must involve classes, creation

JSP and web applications

Page 24: JSP and Servelets. How does JSP work? NOT a Java scripting language NOT like php –JSP are NOT parsed on request Java code must involve classes, creation

Java 2 Enterprise Edition

• J2EE• Edition of Java• Architecture specially designed for server-

based applications– Including web applications

• Highly compatible with M-V-C• Client Tier• Middle Tier• Enterprise Information System Tier

Page 25: JSP and Servelets. How does JSP work? NOT a Java scripting language NOT like php –JSP are NOT parsed on request Java code must involve classes, creation

J2EE Overview

Database

servlet

servlet

JSP

EJB

EJB

application

browser

Client Tier Middle Tier EIS Tier

EJB Container

Web Container JMSJavaIDL

RMIJNDIXML

JDBCJTA/JTS

Page 26: JSP and Servelets. How does JSP work? NOT a Java scripting language NOT like php –JSP are NOT parsed on request Java code must involve classes, creation

Which J2EE components?

• Pure JSP– Simple applications– Prototyping– Use M-V-C

• Business logic pages (model)

• presentation pages (view)

• Request processing pages (controller)

– Enhance with JavaBeans components and custom tags

Page 27: JSP and Servelets. How does JSP work? NOT a Java scripting language NOT like php –JSP are NOT parsed on request Java code must involve classes, creation

Tomcat web container

Tomcat subdirectories:• bin

– scripts to run Tomcat

• common/lib– jar archives with javax.servlet class files

• src– interface definitions for javax.servlet classes

• conf– configuration files (Tomcat parameters)

Page 28: JSP and Servelets. How does JSP work? NOT a Java scripting language NOT like php –JSP are NOT parsed on request Java code must involve classes, creation

Tomcat web container

• docs– HTML documentation about Tomcat

• work– a working directory for temporary files and

generated servlets

• webapps– all web applications run on Tomcat are placed

in subdirectories of this

Page 29: JSP and Servelets. How does JSP work? NOT a Java scripting language NOT like php –JSP are NOT parsed on request Java code must involve classes, creation

deploying a web application

• create a subdirectory of webapps, e.g. myDir

• create a WEB-INF subdirectory of myDir

• WEB-INF contains– web.xml file

• specifies deployment parameters for the web application

– lib directory (optional)• contains any special class libraries (.jar files) needed

– .classes directory• class files for servlet and helper classes defined for the

specific application

Page 30: JSP and Servelets. How does JSP work? NOT a Java scripting language NOT like php –JSP are NOT parsed on request Java code must involve classes, creation

the web.xml file

• written in XML and contains environment data– descriptions of servlet names and locations– custom tag libraries– security constraints on particular servlets– multiple url aliases for servlets– etc

Page 31: JSP and Servelets. How does JSP work? NOT a Java scripting language NOT like php –JSP are NOT parsed on request Java code must involve classes, creation

Creating a web app in NetBeans

Page 32: JSP and Servelets. How does JSP work? NOT a Java scripting language NOT like php –JSP are NOT parsed on request Java code must involve classes, creation

Creating a web app in NetBeans

Page 33: JSP and Servelets. How does JSP work? NOT a Java scripting language NOT like php –JSP are NOT parsed on request Java code must involve classes, creation

Creating a web app in NetBeans

Page 34: JSP and Servelets. How does JSP work? NOT a Java scripting language NOT like php –JSP are NOT parsed on request Java code must involve classes, creation

Creating a web app in NetBeans

Page 35: JSP and Servelets. How does JSP work? NOT a Java scripting language NOT like php –JSP are NOT parsed on request Java code must involve classes, creation

Creating a web app in NetBeans

Page 36: JSP and Servelets. How does JSP work? NOT a Java scripting language NOT like php –JSP are NOT parsed on request Java code must involve classes, creation

Creating a web app in NetBeans

Page 37: JSP and Servelets. How does JSP work? NOT a Java scripting language NOT like php –JSP are NOT parsed on request Java code must involve classes, creation

Creating a web app in NetBeans

Page 38: JSP and Servelets. How does JSP work? NOT a Java scripting language NOT like php –JSP are NOT parsed on request Java code must involve classes, creation

Adding a JSP to the Web App

Page 39: JSP and Servelets. How does JSP work? NOT a Java scripting language NOT like php –JSP are NOT parsed on request Java code must involve classes, creation

Adding a JSP to the Web App

Page 40: JSP and Servelets. How does JSP work? NOT a Java scripting language NOT like php –JSP are NOT parsed on request Java code must involve classes, creation

Adding a JSP to the Web App

Page 41: JSP and Servelets. How does JSP work? NOT a Java scripting language NOT like php –JSP are NOT parsed on request Java code must involve classes, creation

Adding a JSP to the Web App

Page 42: JSP and Servelets. How does JSP work? NOT a Java scripting language NOT like php –JSP are NOT parsed on request Java code must involve classes, creation

Running the Web App

Page 43: JSP and Servelets. How does JSP work? NOT a Java scripting language NOT like php –JSP are NOT parsed on request Java code must involve classes, creation

Browsing the JSP

Page 44: JSP and Servelets. How does JSP work? NOT a Java scripting language NOT like php –JSP are NOT parsed on request Java code must involve classes, creation

Adding custom tags to a JSP

Page 45: JSP and Servelets. How does JSP work? NOT a Java scripting language NOT like php –JSP are NOT parsed on request Java code must involve classes, creation

custom tags

• used to farm out processing to specialised classes or Beans

• identified by a tag library directive • basic JSP tag library

– e.g. <jsp:useBean … />

• Apache Taglib custom tag library• JavaServerFaces, struts libraries• can define your own custom tags

Page 46: JSP and Servelets. How does JSP work? NOT a Java scripting language NOT like php –JSP are NOT parsed on request Java code must involve classes, creation

custom tags

• defined as Java classes that extend base classes in javax.servlet.jsp.tagext package

• represented in a JSP using the syntax– <lib:tag att1=“...” att1=“...” att1=“...” />– (can also have element content)

• custom tag in JSP replaced by code in the servelet on translation

Page 47: JSP and Servelets. How does JSP work? NOT a Java scripting language NOT like php –JSP are NOT parsed on request Java code must involve classes, creation

custom tag examplepackage myPack;

import java.util.*;

import java.io.*;

import javax.servelet.jsp.*;

import javax.servelet.jsp.tagext .*;

public class DateStamper extends TagSupport {

protected String comment = null;

public String getComment () {

return comment;

}

Page 48: JSP and Servelets. How does JSP work? NOT a Java scripting language NOT like php –JSP are NOT parsed on request Java code must involve classes, creation

public void setComment (String cm) {

comment = cm;

}

public int doEndTag () {

try {

String dateStr = (new Date()).toString ();

pageContext.getOut ().println (

“<hr>This page entitled, “ + comment

+ “, was printed on “ +

dateStr +

“<br>”);

}

catch (Exception e) {//error handling}

return EVAL_PAGE;

}}

Page 49: JSP and Servelets. How does JSP work? NOT a Java scripting language NOT like php –JSP are NOT parsed on request Java code must involve classes, creation

<taglib>

<tlibversion>1.0 </tlibversion>

<jspversion>1.1 </jspversion>

<shortname>myTags</shortname>

<tag>

<name>DateStamper</name>

<tagclass>myPack.DateStamper</tagclass>

<bodycontent>empty</bodycontent>

<attribute>

<name>comment</name>

<required>true</required>

</attribute>

</tag>

</taglib>

tag library definition file,

myTags.tld

Page 50: JSP and Servelets. How does JSP work? NOT a Java scripting language NOT like php –JSP are NOT parsed on request Java code must involve classes, creation

deployment in a web application<web-app>

<taglib>

<taglib-uri>

/myTags

</taglib-uri>

<taglib-location>

/WEB-INF/tlds/myTags.tld

</taglib-location

</taglib>

</web-app>

Page 51: JSP and Servelets. How does JSP work? NOT a Java scripting language NOT like php –JSP are NOT parsed on request Java code must involve classes, creation

Tag libraries

Page 52: JSP and Servelets. How does JSP work? NOT a Java scripting language NOT like php –JSP are NOT parsed on request Java code must involve classes, creation

using the tag in a JSP

<%@ taglib uri=“/myTagLibrary” prefix = “myTags” %>

<html><head><title>Using a custom tag</title></head>

<body bgcolor = “FFFFFF”>

<h1>Using a custom tag</h1>

<p>Hello World. My custom tag has the following to say:

<myTags:DateStamper comment=“This is a custom tag” />

</body></html>

Page 53: JSP and Servelets. How does JSP work? NOT a Java scripting language NOT like php –JSP are NOT parsed on request Java code must involve classes, creation

JSP, Bean and Servelet example

Page 54: JSP and Servelets. How does JSP work? NOT a Java scripting language NOT like php –JSP are NOT parsed on request Java code must involve classes, creation

description

• football league result web application

• users request match results– different requests possible– database search

• html front end– user interaction through web-browser

• servelet/JSP request handling and data processing

Page 55: JSP and Servelets. How does JSP work? NOT a Java scripting language NOT like php –JSP are NOT parsed on request Java code must involve classes, creation

system architecture

football.html

football.jsp

FootballSearchBean

FootballGame

DBInfo

Database

tomcat

static page to request results

highly graphic interactive results page

handles the request, submits SQL and processes resultrepresents football game data contained in a DB tablehelper class to create the DB connection

holds a single table Teams containing match data

Page 56: JSP and Servelets. How does JSP work? NOT a Java scripting language NOT like php –JSP are NOT parsed on request Java code must involve classes, creation

football.html

<html><head><title>Football Search</title></head>

<body bgcolor = “white”>

<h1>Search the football league table</h1>

<ul><li>

<a href = “http://localhost:8080/jspeg/football.jsp?searchType=all”>

List all games </a>

</li><li>

<a href = http://localhost:8080/jspeg/football.jsp?searchType=drawn>

List drawn matches </a>

</li></ul>

</body></html>

call JSP with a search parameter

call JSP with a search parameter

Page 57: JSP and Servelets. How does JSP work? NOT a Java scripting language NOT like php –JSP are NOT parsed on request Java code must involve classes, creation

FootballGame class

package football;

import java.sql;

public class FootballGame {

private String team1,team2

private int score1, score2;

public String getTeam1 () {return team1;}

// similar methods to get team2, score1, score2

public void loadFromResultSet (ResultSet r) throws SQLException {

team1 = r.getString (“TEAM1”); // ditto for team2

score1 = r.getInt (“SCORE1”); // ditto for score2

}

}

to handle results of data query

loads DB query result into the FootballGame object

Page 58: JSP and Servelets. How does JSP work? NOT a Java scripting language NOT like php –JSP are NOT parsed on request Java code must involve classes, creation

FootballSearchBean

package football;

import java.sql;

import java.util;

public class FootballSearchbean {

private static final allstr = “select * from TEAMS”;

private static final drawstr =

“select * from TEAMS where SCORE1=SCORE2”;

private String searchType;

public void setSearchType (String type) {

searchType = type;

}

handle DB queriesto use Java Iterator class

set up SQL query strings

searchType query parameter from football.html ends up here

Page 59: JSP and Servelets. How does JSP work? NOT a Java scripting language NOT like php –JSP are NOT parsed on request Java code must involve classes, creation

private Vector results;

public Iterator games () {

if (results != null)

return results.iterator ();

else

return null;

}

public int numGames () {

if (results != null)

return results.size ();

else

return 0;

}

contains results from the DB query

for manipulating retrieved data

FootballSearchBean

counts the number of results

Page 60: JSP and Servelets. How does JSP work? NOT a Java scripting language NOT like php –JSP are NOT parsed on request Java code must involve classes, creation

public void doSearch () {

results = new Vector ();

try {

Connection db = DBInfo.connectToDatabase ();

Statement stmt = db.createStatement ();

String request = allstr;

if (“drawn”.equals (searchType))

request = drawstr;

// else add code for other search options

connect to the databasecreate a Statement object (SQL

query) to be fired at the DB

FootballSearchBean

Page 61: JSP and Servelets. How does JSP work? NOT a Java scripting language NOT like php –JSP are NOT parsed on request Java code must involve classes, creation

ResultSet r = stmt.executeQuery (request);

while (r.next ()) {

FootballGame fg = new FootballGame ();

fg.loadFromResultSet r;

results.addElement (fg);

}

r.close ();

stmt.close ();

db.close ();

} catch (Exception e) {// error-handling code here}

}

}

fire the query at the DB and store the retrieved rows

create a FootballGame object for each retrieved table row

FootballSearchBean

add the FootballGame to the results Vector

Page 62: JSP and Servelets. How does JSP work? NOT a Java scripting language NOT like php –JSP are NOT parsed on request Java code must involve classes, creation

<%@ page import = “java.util.*” %>

<%@ page import = “football.*” %>

<html><head><title>League Results</title></head><body>

<!-- cool and flashy DHTML content inserted here by the web designer. Embedded amongst that will be: -->

<h1>Results</h1>

<jsp:useBean

id = “theLeague”

class = “football.FootballSearchBean” />

<jsp:setProperty

name = “theLeague”

property = “*”/>

util package for the Iterator classfootball package with its classes

football.jsp

Bean created and given an Id

searchType property from football.html passed to Bean here

Page 63: JSP and Servelets. How does JSP work? NOT a Java scripting language NOT like php –JSP are NOT parsed on request Java code must involve classes, creation

<% theLeague.doSearch (); %>

<% if (theLeague.numGames () == 0) { %>

<p> No games played yet </p>

<% } else { %>

<!-- there are results so set up a table -->

<table> <caption>Results</caption>

<tr>

<th align = “center”>Home Team</th>

<th align = “center”>Home Team Score</th>

<th align = “center”>Away Team</th>

<th align = “center”>Away Team Score</th>

</tr>

searches the database with the user selected search optiondetermines the size of the result

set and acts accordingly

football.jsp

Page 64: JSP and Servelets. How does JSP work? NOT a Java scripting language NOT like php –JSP are NOT parsed on request Java code must involve classes, creation

<% Iterator it = theLeague.games ();

while (it.hasNext ()) {

FootballGame fg = (FootballGame) it.next (); %>

<tr>

<td><%= fg.getTeam1 () %> </td>

<td><%= fg.getScore1 () %> </td>

<td><%= fg.getTeam2 () %> </td>

<td><%= fg.getScore2 () %> </td>

</tr>

<% } %>

</table>

<% } %>

</body></html>

creates an Iterator containing FootballGame objects

explicit typecast of each object to access the get methods

football.jsp

use expressions to insert names and scores for each game in table

cells

Page 65: JSP and Servelets. How does JSP work? NOT a Java scripting language NOT like php –JSP are NOT parsed on request Java code must involve classes, creation

some design issues

• Java code in the JSP fragmented through the HTML

• difficult to trace errors– brackets– parameter declarations– incorrect program logic

• web designer could unwittingly break the code

Page 66: JSP and Servelets. How does JSP work? NOT a Java scripting language NOT like php –JSP are NOT parsed on request Java code must involve classes, creation

revised system architecture

football.html

MatchReport.jsp

FootballSearchBean

FootballGame

DBInfo

Database

tomcat

hyperlinks now go to a pre-processing servelet p

reprcessing servelet

No Result.jsp

web.xml

Page 67: JSP and Servelets. How does JSP work? NOT a Java scripting language NOT like php –JSP are NOT parsed on request Java code must involve classes, creation

web.xml

<web-app>

<servelet>

<servelet-name>FootballServelet</servelet-name>

<servelet-class>PreprocessServelet</servelet-class>

</servelet>

<servelet-mapping>

<servelet-name>FootballServelet</servelet-name>

<url-pattern>/FootballInfo</url-pattern>

</servelet-mapping>

</web-app>

Page 68: JSP and Servelets. How does JSP work? NOT a Java scripting language NOT like php –JSP are NOT parsed on request Java code must involve classes, creation

PreprocessServelet// import all the usual servelet stuff

import football.*;

public class preprocessServelet extends HttpServelet {

private static final String

allstr = “no results available yet”;

private static final String

drawstr = “there have been no drawn games”;

private static final String

homestr = “there have been no home wins”;

private static final String

awaystr = “there have been no away wins”;

private static final String jspFailPage = “NoResult.jsp”;

private static final String jspReportPage = “MatchReport.jsp”;

Page 69: JSP and Servelets. How does JSP work? NOT a Java scripting language NOT like php –JSP are NOT parsed on request Java code must involve classes, creation

PreprocessServeletpublic void doGet () {HttpServeletRequest req, HttpServeletresponse res) throws ServeletException, IOException {

String search = req.getParameter (“searchType”);

FootballSearchBean fsb = new FootballSearchBean ();

fsb.setSearchType (search);

fsb.doSearch ();

if (fsb.numGames () == 0) {

doSearchFail (search, req, res);

else

doSuccess (fsb, req, res);

}

Page 70: JSP and Servelets. How does JSP work? NOT a Java scripting language NOT like php –JSP are NOT parsed on request Java code must involve classes, creation

PreprocessServeletprivate void doSearchFail (String search, HttpServeletRequest req, HttpServeletresponse res) throws ServeletException, IOException {

String reason = allstr;

if (“drawn”.equals (search))

reason = drawstr;

// add similar clauses for other possible reasons

req.setAttribute (“Message”, reason);

RequestDispatcher d =

req.getRequestDispatcher (jspFailPage);

d.forward (req, res);

}

Page 71: JSP and Servelets. How does JSP work? NOT a Java scripting language NOT like php –JSP are NOT parsed on request Java code must involve classes, creation

PreprocessServelet

private void doSuccess (FootballSearchBean fsb, HttpServeletRequest req, HttpServeletresponse res) throws ServeletException, IOException {

req.setAttribute (“theLeague”, fsb);

RequestDispatcher d =

req.getRequestDispatcher (jspReportPage);

d.forward (req, res);

}

// end of PreprocessServelet

}

Page 72: JSP and Servelets. How does JSP work? NOT a Java scripting language NOT like php –JSP are NOT parsed on request Java code must involve classes, creation

revised JSP pages// NoResult.jsp

<!-- contains the following minimal content -->

<jsp:useBean scope=“request” id=“Message” class=“String”>

<%= Message %>

**********************************************************

//MatchReport.jsp

<jsp:useBean scope=“request” id=“theLeague” class=“football.FootballSearchBean”>

...

<% Iterator it = theLeague.games ();

while (it.hasNext ()) {

FootballGame fg = (FootballGame) it.next (); %>

<tr><td><%= fg.getTeam1 () %> </td> <!-- etc. -->

Page 73: JSP and Servelets. How does JSP work? NOT a Java scripting language NOT like php –JSP are NOT parsed on request Java code must involve classes, creation

revised architecture• much improved

– separation of most of the code from the web designer

• iterator code still exposed– replace by custom tags from Apache Struts library

<logic:iterate id=“fg” collection=“<%= theLeague.games() >” >

<td><bean:write name = “fg” property = “team1” />

... <!-- HTML and JSP actions to write other cells -->

</logic:iterate>

Page 74: JSP and Servelets. How does JSP work? NOT a Java scripting language NOT like php –JSP are NOT parsed on request Java code must involve classes, creation

Timetable change

From w/c 23/11/09

• No Monday Lecture

• Two lectures moved into one slot:– Wednesday 11-1– B39

• Labs will still be Thursday, 9-11