mit aiti 2004 jsp – lecture 3 including and forwarding

18
MIT AITI 2004 JSP – Lecture 3 Including and Forwarding

Upload: jasper-james

Post on 16-Dec-2015

213 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: MIT AITI 2004 JSP – Lecture 3 Including and Forwarding

MIT AITI 2004JSP – Lecture 3

Including and Forwarding

Page 2: MIT AITI 2004 JSP – Lecture 3 Including and Forwarding

Shared Code

• Usually every page of a site requires a common header, footer, and structure.

• Suppose each page of a site is written as <%@page import="java.util.Date"%> <HTML> <HEAD><TITLE>My Page</TITLE></HEAD> <BODY BGCOLOR="blue"> Today’s date: <%=new Date()%> . . . </BODY> </HTML>

• Lots of duplicate HTML and JSP!

Page 3: MIT AITI 2004 JSP – Lecture 3 Including and Forwarding

Static Include

• Put common code into separate files <%@page import="java.util.Date"%>

<HTML>

<HEAD><TITLE>My Page</TITLE></HEAD>

<BODY BGCOLOR="blue">

Today’s date: <%=new Date()%>

. . .

</BODY>

</HTML>

header.jsp

footer.html

• becomes . . . <%@include file="header.jsp" %>

. . .

<%@include file="footer.html" %>

Page 4: MIT AITI 2004 JSP – Lecture 3 Including and Forwarding

Include Directive

• <%@include file="filename"%>

• Pastes text of file directly into the page before the page is compiled.

• Beware of variable name clashes

• Don’t use in confusing ways(for instance, included file ending an if statement)

• We now know two directives:– Include directive <%@include file="filename"%>– Page directive <%@page import="javaclass"%>

Page 5: MIT AITI 2004 JSP – Lecture 3 Including and Forwarding

Dynamic Include

• Another way to include <jsp:include page="header.jsp" />

. . .

<jsp:include page="footer.html" />

• Different from include directive!

• Text of file NOT pasted in before compiling

• Included page is called during execution

Page 6: MIT AITI 2004 JSP – Lecture 3 Including and Forwarding

JSP Include Tag

• How it looks <jsp:include page="filename" />

or <jsp:include page="filename">

</jsp:include>

• How it works1. Passes the request on to the included page

2. When done, returns to previous page

Page 7: MIT AITI 2004 JSP – Lecture 3 Including and Forwarding

JSP Include Tag 2

• Also send additional parameters to included page

<jsp:include page="printname.jsp">

<jsp:param name="firstname" value="Greg" />

</jsp:include>

• Can put JSP expressions into JSP tags

<jsp:include page="<%=pageVar%>">

<jsp:param name="firstname" value="<%=nameVar%>" />

</jsp:include>

Page 8: MIT AITI 2004 JSP – Lecture 3 Including and Forwarding

Static or Dynamic Include?

• Try to use static include (include directive) if possible, because it is faster

• Use dynamic inclusion (JSP include tag) if– Including large file with a lot of variables

(avoid potential variable name clashes)

– Passing parameters to included page– The page you want to include is a variable

(include the result of a JSP expression)

Page 9: MIT AITI 2004 JSP – Lecture 3 Including and Forwarding

Forwarding

• JSP Forward Tag <jsp:forward page="filename" />

• How it works1.Passes the request on to the page forwarded to

2.When done, does not return to previous page, just stops processing the request.

Page 10: MIT AITI 2004 JSP – Lecture 3 Including and Forwarding

JSP Forward Tag

• Like jsp:include, you can . . .• Send additional parameters to forwarded page <jsp:forward page="printname.jsp"> <jsp:param name="firstname" value="Greg" /> </jsp:forward>

• Put JSP expressions into JSP tags <jsp:forward page="<%=pageVar%>"> <jsp:param name="firstname" value="<%=nameVar%>" /> </jsp:forward>

Page 11: MIT AITI 2004 JSP – Lecture 3 Including and Forwarding

Dynamic Include Example

• print-two-names.jsp

<html> <body>

<jsp:include page="printname.jsp"> <jsp:param name="firstname" value="Greg" /> </jsp:include><p>

<jsp:include page="printname.jsp"> <jsp:param name="firstname" value="Eric" /> </jsp:include>

</body> </html>

Page 12: MIT AITI 2004 JSP – Lecture 3 Including and Forwarding

Print Two Names Snapshot

Page 13: MIT AITI 2004 JSP – Lecture 3 Including and Forwarding

Dynamic Forward Example

• print-one-name.jsp

<html> <body>

<jsp:forward page="printname.jsp"> <jsp:param name="firstname" value="Greg" /> </jsp:forward>

<jsp:forward page="printname.jsp"> <jsp:param name="firstname" value=“Eric" /> </jsp:forward>

</body> </html>

Page 14: MIT AITI 2004 JSP – Lecture 3 Including and Forwarding

Print One Name Snapshot

Page 15: MIT AITI 2004 JSP – Lecture 3 Including and Forwarding

Request Parameters Limiting

• Request parameters are limiting because they must be Strings

• Must convert back and forth from Objects to String representation of Objects

• String reps must be unique

• Good news!

We can put Objects in the request too

Page 16: MIT AITI 2004 JSP – Lecture 3 Including and Forwarding

Request Attributes• Objects stored in the request object• Add attribute with request.setAttribute <% request.setAttribute("dataString", dataObject); %>

• Get attribute with request.getAttribute <% DataType dataObject = (DataType)request.getAttribute("dataString"); %>

• getAttribute returns Object, so must cast

Page 17: MIT AITI 2004 JSP – Lecture 3 Including and Forwarding

Request Attribute Example

• prepare-date.jsp<% int month = Integer.parseInt(request.getParameter("m")); int day = Integer.parseInt(request.getParameter("d")); int year = Integer.parseInt(request.getParameter("y")); Calendar cal = Calendar.getInstance(); cal.clear(); cal.set(year, month, day); request.setAttribute("date", cal.getDate());%><jsp:forward page="use-date.jsp" />

• use-date.jsp<% Date date = (Date)request.getAttribute("date"); . . .%>

Page 18: MIT AITI 2004 JSP – Lecture 3 Including and Forwarding

Include & Forward Review

• Static include– Include directive <%@include file="filename"%>– Pastes text into original file

• Dynamic include– JSP include tag <jsp:include page="file.jsp" />– Requests included file at runtime and returns

• Dynamic forward– JSP forward tag <jsp:forward page="file.jsp" />– Requests forwarded file at runtime and does not return

• Extra functionality when dynamically loading a page– Send string parameters with jsp:param tag– Use JSP expressions within the tags

• Send object attributes to dynamically loaded page– Add attribute to the request object with request.setAttribute– Get attributes from the request object with request.getAttribute