JSP structure and models

Post on 30-Dec-2015

29 views 0 download

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

Java WWW Week 2

Version 2.2 2009 Slide 1j.c.westlake@staffs.ac.uk,n.a.shulver@staffs.ac.uk

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

Java WWW Week 2

Version 2.2 2009 Slide 2j.c.westlake@staffs.ac.uk,n.a.shulver@staffs.ac.uk

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

Java WWW Week 2

Version 2.2 2009 Slide 3j.c.westlake@staffs.ac.uk,n.a.shulver@staffs.ac.uk

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.

Java WWW Week 2

Version 2.2 2009 Slide 4j.c.westlake@staffs.ac.uk,n.a.shulver@staffs.ac.uk

Built-in objects

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

Java WWW Week 2

Version 2.2 2009 Slide 5j.c.westlake@staffs.ac.uk,n.a.shulver@staffs.ac.uk

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.

Java WWW Week 2

Version 2.2 2009 Slide 6j.c.westlake@staffs.ac.uk,n.a.shulver@staffs.ac.uk

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

Java WWW Week 2

Version 2.2 2009 Slide 7j.c.westlake@staffs.ac.uk,n.a.shulver@staffs.ac.uk

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

Java WWW Week 2

Version 2.2 2009 Slide 8j.c.westlake@staffs.ac.uk,n.a.shulver@staffs.ac.uk

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>

Java WWW Week 2

Version 2.2 2009 Slide 9j.c.westlake@staffs.ac.uk,n.a.shulver@staffs.ac.uk

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

Java WWW Week 2

Version 2.2 2009 Slide 10j.c.westlake@staffs.ac.uk,n.a.shulver@staffs.ac.uk

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>

Java WWW Week 2

Version 2.2 2009 Slide 11j.c.westlake@staffs.ac.uk,n.a.shulver@staffs.ac.uk

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

Java WWW Week 2

Version 2.2 2009 Slide 12j.c.westlake@staffs.ac.uk,n.a.shulver@staffs.ac.uk

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

Java WWW Week 2

Version 2.2 2009 Slide 13j.c.westlake@staffs.ac.uk,n.a.shulver@staffs.ac.uk

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

Java WWW Week 2

Version 2.2 2009 Slide 14j.c.westlake@staffs.ac.uk,n.a.shulver@staffs.ac.uk

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.

Java WWW Week 2

Version 2.2 2009 Slide 15j.c.westlake@staffs.ac.uk,n.a.shulver@staffs.ac.uk

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…

Java WWW Week 2

Version 2.2 2009 Slide 16j.c.westlake@staffs.ac.uk,n.a.shulver@staffs.ac.uk

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>

Java WWW Week 2

Version 2.2 2009 Slide 17j.c.westlake@staffs.ac.uk,n.a.shulver@staffs.ac.uk

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

Java WWW Week 2

Version 2.2 2009 Slide 18j.c.westlake@staffs.ac.uk,n.a.shulver@staffs.ac.uk

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)

Java WWW Week 2

Version 2.2 2009 Slide 19j.c.westlake@staffs.ac.uk,n.a.shulver@staffs.ac.uk

addtousers.jsp

Java WWW Week 2

Version 2.2 2009 Slide 21j.c.westlake@staffs.ac.uk,n.a.shulver@staffs.ac.uk

GetUserUsingInputExample.jsp

Java WWW Week 2

Version 2.2 2009 Slide 23j.c.westlake@staffs.ac.uk,n.a.shulver@staffs.ac.uk

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)