mit aiti 2004 jsp – lecture 3 including and forwarding
TRANSCRIPT
MIT AITI 2004JSP – 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!
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" %>
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"%>
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
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
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>
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)
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.
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>
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>
Print Two Names Snapshot
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>
Print One Name Snapshot
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
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
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"); . . .%>
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