1 / 15

Chapter 13

Chapter 13. Including Files and Applets in JSP Pages. Using jsp:include to include pages at request time Using <%@ include ... %> (the include directive) to include files at page translation time Using jsp:plugin to include applets for the Java Plug-in.

Download Presentation

Chapter 13

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Chapter 13 Including Files and Applets in JSP Pages

  2. Using jsp:include to include pages at request time • Using <%@ include ... %> (the include directive) to include files at page translation time • Using jsp:plugin to include applets for the Java Plug-in

  3. Including Pages at Request Time: The jsp:include Action • The jsp:include action includes the output of a secondary page at the time the main page is requested. • Although the output of the included pages cannot contain JSP, the pages can be the result of resources that use servlets or JSP to create the output

  4. Including Pages at Request Time: The jsp:include Action • The server runs the included page in the usual way and places the output into the main page • <jsp:include page="relative-path-to-resource" /> • The flush Attribute • <jsp:include page="relative-path-to-resource" flush="true" />: whether the output stream of the main page should flushed before the inclusion of the page (the default is false).

  5. Code: A News Headline Page • WhatsNew.jsp • Item1.html • Item2.html • Item3.html • The jsp:param Element: Augmenting Request Parameters • <jsp:include page="/fragments/StandardHeading.jsp"> <jsp:param name="bgColor" value="YELLOW" /> </jsp:include>

  6. Including Files at Page Translation Time: The include Directive • <%@ include file="Relative URL" %> • Think of the include directive as a preprocessor: • The included file is inserted character for character into the main page • Then the resultant page is treated as a single JSP page

  7. The include Directive • The fundamental difference between jsp:include and the include directive is • The time they are invoked: • jsp:include is invoked at request time • include directive invoked at page translation time.

  8. Additional Power from the include Directive • With the include directive, the included file is permitted to contain JSP code such as response header settings and field definitions that affect the main page • For example, suppose snippet.jsp contained the following line of code: • <%! int accessCount = 0; %>

  9. Additional Power from the include Directive • In such a case, you could do the following in the main page: • <%@ include file="snippet.jsp" %> • <%= accessCount++ %> • <%-- Uses accessCount --%>

  10. Additional Power from the include Directive • With jsp:include, this would be impossible • the undefined accessCount variable; • the main page would not translate successfully into a servlet. • Even if it could be translated without error, there would be no point; • jsp:include includes the output of the auxiliary page, and snippet.jsp has no output.

  11. With most servers, if you use the include directive and change the included file, you also have to update the modification date of the main page. • Some operating systems have commands that update the modification date without your actually editing the file (e.g., the Unix touch command), but a simple portable alternative is to include a JSP comment in the top-level page. Update the comment whenever the included file changes. For example, you might put the modification date of the included file in the comment, as below. • <%-- Navbar.jsp modified 9/1/03 --%> <%@ include file="Navbar.jsp" %>

  12. Forwarding Requests with jsp:forward • You use jsp:include to combine output from the main page and the auxiliary page. • Instead, you can use jsp:forward to obtain the complete output from the auxiliary page. • For example, here is a page that randomly selects either page1.jsp or page2.jsp to output.

  13. <% String destination; • if (Math.random() > 0.5) { destination = "/examples/page1.jsp"; } • else { destination = "/examples/page2.jsp"; } %> • <jsp:forward page="<%= destination %>" />

  14. Including Applets for the Java Plug-In • <jsp:plugin type="applet" code="MyApplet.class" width="475" height="350"> </jsp:plugin> • The jsp:param and jsp:params Elements • <jsp:plugin type="applet" code="MyApplet.class" width="475" height="350"> <jsp:params> <jsp:param name="PARAM1" value="VALUE1" /> <jsp:param name="PARAM2" value="VALUE2" /> </jsp:params> </jsp:plugin>

  15. A jsp:plugin Example • Listing 13.8 PluginApplet.jsp • Listing 13.9 PluginApplet.java • Listing 13.10 TextPanel.java • Listing 13.11 DrawingPanel.java • Listing 13.12 WindowUtilities.java

More Related