JSP structure and models

21
Java WWW Week 2 Version 2.2 2009 Slide 1 [email protected] , [email protected] 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

description

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. JSP Built-in Objects. - PowerPoint PPT Presentation

Transcript of JSP structure and models

Page 1: JSP structure and models

Java WWW Week 2

Version 2.2 2009 Slide [email protected],[email protected]

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

Page 2: JSP structure and models

Java WWW Week 2

Version 2.2 2009 Slide [email protected],[email protected]

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 pagecontextsession applicationconfig page

Page 3: JSP structure and models

Java WWW Week 2

Version 2.2 2009 Slide [email protected],[email protected]

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.

Page 4: JSP structure and models

Java WWW Week 2

Version 2.2 2009 Slide [email protected],[email protected]

Built-in objects

Response objects methods allow access to:Setting HTTP headers.Set cookies.Encode session information into URLs.

Page 5: JSP structure and models

Java WWW Week 2

Version 2.2 2009 Slide [email protected],[email protected]

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.

Page 6: JSP structure and models

Java WWW Week 2

Version 2.2 2009 Slide [email protected],[email protected]

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).

Page 7: JSP structure and models

Java WWW Week 2

Version 2.2 2009 Slide [email protected],[email protected]

Structure of JSPsDeclaration 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) %>

Page 8: JSP structure and models

Java WWW Week 2

Version 2.2 2009 Slide [email protected],[email protected]

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>

Page 9: JSP structure and models

Java WWW Week 2

Version 2.2 2009 Slide [email protected],[email protected]

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" %>

Page 10: JSP structure and models

Java WWW Week 2

Version 2.2 2009 Slide [email protected],[email protected]

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>

Page 11: JSP structure and models

Java WWW Week 2

Version 2.2 2009 Slide [email protected],[email protected]

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:

useBeansetPropertygetPropertyIncludeForwardplugin

We will review two of them in during the module:includeforward

Page 12: JSP structure and models

Java WWW Week 2

Version 2.2 2009 Slide [email protected],[email protected]

include action and directiveActions 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 fileThere are examples of both on the Tomcat server

Page 13: JSP structure and models

Java WWW Week 2

Version 2.2 2009 Slide [email protected],[email protected]

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

Page 14: JSP structure and models

Java WWW Week 2

Version 2.2 2009 Slide [email protected],[email protected]

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.

Page 15: JSP structure and models

Java WWW Week 2

Version 2.2 2009 Slide [email protected],[email protected]

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…

Page 16: JSP structure and models

Java WWW Week 2

Version 2.2 2009 Slide [email protected],[email protected]

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>

Page 17: JSP structure and models

Java WWW Week 2

Version 2.2 2009 Slide [email protected],[email protected]

Forward model

JSP

JSP

Client Request

(optional)

DATABASE

Forward model

The JSP parses the input from

the client form (and optionally

updates the Database)HTTP Server - Tomcat

HTML Form

(optional)

SQL

Response

HTML Form

Page 18: JSP structure and models

Java WWW Week 2

Version 2.2 2009 Slide [email protected],[email protected]

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 JSPsDemo of a simple Get JSP (SimpleGetUserExample.jsp)Demo of a user input version of get

(GetUserUsingInputExample.jsp)

Page 19: JSP structure and models

Java WWW Week 2

Version 2.2 2009 Slide [email protected],[email protected]

addtousers.jsp

Page 20: JSP structure and models

Java WWW Week 2

Version 2.2 2009 Slide [email protected],[email protected]

GetUserUsingInputExample.jsp

Page 21: JSP structure and models

Java WWW Week 2

Version 2.2 2009 Slide [email protected],[email protected]

Summary

You have learnt more about the structure of JSPs

Learning from examples is goodYou have seen an Add something JSPYou have seen a more sophisticated view

something JSP (compared to last week with the simple guest listing)