Presentation on theme: "MIT AITI 2004 JSP – Lecture 3 Including and Forwarding."— Presentation transcript:
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 My Page Today’s date:... Lots of duplicate HTML and JSP!
Static Include Put common code into separate files My Page Today’s date:... header.jsp footer.html becomes......
Include Directive 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 –Page directive
Dynamic Include Another way to include... Different from include directive! Text of file NOT pasted in before compiling Included page is called during execution
JSP Include Tag How it looks or How it works 1.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 Can put JSP expressions into JSP tags "> " />
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 How it works 1.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 Put JSP expressions into JSP tags "> " />
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()); %> use-date.jsp <% Date date = (Date)request.getAttribute("date");... %>
Include & Forward Review Static include –Include directive –Pastes text into original file Dynamic include –JSP include tag –Requests included file at runtime and returns Dynamic forward –JSP forward tag –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