Presentation is loading. Please wait.

Presentation is loading. Please wait.

MIT AITI 2004 JSP – Lecture 3 Including and Forwarding.

Similar presentations


Presentation on theme: "MIT AITI 2004 JSP – Lecture 3 Including and Forwarding."— Presentation transcript:

1 MIT AITI 2004 JSP – Lecture 3 Including and Forwarding

2 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!

3 Static Include Put common code into separate files My Page Today’s date:... header.jsp footer.html becomes......

4 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

5 Dynamic Include Another way to include... Different from include directive! Text of file NOT pasted in before compiling Included page is called during execution

6 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

7 JSP Include Tag 2 Also send additional parameters to included page Can put JSP expressions into JSP tags "> " />

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

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

10 JSP Forward Tag Like jsp:include, you can... Send additional parameters to forwarded page Put JSP expressions into JSP tags "> " />

11 Dynamic Include Example print-two-names.jsp

12 Print Two Names Snapshot

13 Dynamic Forward Example print-one-name.jsp

14 Print One Name Snapshot

15 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

16 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

17 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");... %>

18 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


Download ppt "MIT AITI 2004 JSP – Lecture 3 Including and Forwarding."

Similar presentations


Ads by Google