Download presentation
Presentation is loading. Please wait.
Published byDelilah Robinson Modified over 9 years ago
1
CSC 2720 Building Web Applications JavaServer Pages (JSP) JSP Directives and Action Elements
2
JSP Directives page directives – defines attributes that apply to an entire JSP page include directives – includes a resource of text or code when the JSP page is translated taglib directives – defines a tag library and prefix for the custom tags used in the JSP page Syntax: <%@ directive attribute 1 ="value 1 " attribute 2 ="value 2 "... %> where directive can be page, include, or taglib
3
page Directives Defines attributes that apply to an entire JSP page. Which classes are imported For examples, To specify a different superclass for the current JSP page To specify the MIME type of the generated content To indicate if the JSP page is thread safe or not To turn on/off automatic session participation To change the size and behavior of the output buffer To indicate which page is to be used to handle unexpected errors/exceptions
4
page Directives – import Syntax Note: To import all classes from a package, use package.* Purpose Generate import statements at for the resulting servlet class Note: Class files should be placed under the folder …/Web_App_Name/WEB-INF/classes and in the appropriate sub-folders that match the package name. .jar library files should be placed under the folder …/Web_App_Name/WEB-INF/lib
5
page Directives – contentType Syntax Purpose Specify the MIME type of the content generated by the JSP page Default value is "text/html;charset=ISO-8859-1" e.g.: HTML content encoded in simplified Chinese characters References Wiki: MIME: http://en.wikipedia.org/wiki/MIMEhttp://en.wikipedia.org/wiki/MIME MIME reference: http://www.w3schools.com/media/media_mimeref.asphttp://www.w3schools.com/media/media_mimeref.asp Character set reference: http://www.iana.org/assignments/character-setshttp://www.iana.org/assignments/character-sets
6
First Last Email Address Marty Hall hall@corewebprogramming.com Larry Brown brown@corewebprogramming.com Bill Gates gates@sun.com Larry Ellison ellison@microsoft.com <%-- Note: The values are separated by tabs and not by spaces --%> Generating Excel Spreadsheets
7
Other Attributes of the page Directive session Lets you turn on/off session participation Default value is "true" e.g.: extends Changes parent class of the resulting servlet isThreadSafe Lets you specify whether the JSP page is thread safe Default value is "true" Setting this attribute to "false" makes the resulting servlet a single- threaded servlet e.g.:
8
Other Attributes of the page Directive language Let you specify the scripting language Default value is "java" info Allows you to insert a string that later can be retrieved using the getServletInfo() method. e.g.: buffer Changes the minimum size of buffer used by JspWriter The unit of the size is in kilobyte e.g.: means don't buffer the output e.g.: means set buffer size to 12kbytes autoflush Requires the developer to explicitly flush buffer
9
Other Attributes of the page Directive errorPage Designates a page to handle unplanned errors (i.e., when an exception is uncaught in the JSP page) e.g.: isErrorPage Indicates if the current page is a page designated for handling error Default value is "false" If "true", the "exception" implicit object is made available to this page. You can find out from the "exception" object which JSP page is throwing an exception and what kind of exception it is. Note: We can also configure error pages in the web.xml (web application deployment file).
10
A Note On Directive Elements Except for " import ", other attributes cannot repeat. The following is illegal The following is illegal <%@ page buffer="16384" session="false" buffer="8192" %> The following is legal
11
The include Directive Static include – to include the contents of other files in the current JSP page at translation time. Syntax: // Content A // Content B // Content X // Content A // Content X // Content B Translated into Servlet codes original.jsp x.html Notes: Servers may not detect changes made to the included files. Thus, you need to update the "last modified date" of the JSP files whenever the included files change.
12
The include Directive Purposes To reuse JSP content in multiple pages e.g.: Menus, headers, footers, etc. When to use If the included file contains static text If the included file is rarely changed To include files (including JSP fragment files) that are placed under the /WEB-INF folder. Shortcoming Lack of locality (The main file and all the included files share the same scope difficult to debug) Not suitable for files that’s are too big. (A Java method cannot exceeds 64kbytes in size.)
13
Standard Action Elements Special Tags that can be embedded in a JSP page At compile time, these tags are also converted into corresponding Java codes. 7 standard JSP action elements: jsp:include, jsp:forward, jsp:param More commonly used ones jsp:useBean, jsp:setProperty, jsp:getProperty For used with JSP Bean (Will discussed later) jsp:plugin For generating or tags for Java applet.
14
jsp:include Action Dynamic include – To incorporate static or dynamic resources into the current page at request time. Similar to using a RequestDispatcher object to include a resource Can pass data to the included resources Syntax or …
15
jsp:include Action // Content A // Content B // Content X // Java codes to produce content A pageContext.include("x.html"); // Java codes to produce content B Translated into servlet codes original.jsp x.html Java byte codes Compiled into Java byte codes Executed by web container Included into the output stream at rune time. If the included resource is a JSP file, the output produced by the JSP file is included in the output stream of original.jsp.
16
jsp:include Action Advantages over the include directive Easier to maintain Changes made to the included files automatically reflected in the files that include them. Easier to debug (Strong locality) Shortcoming Slower Cannot include files placed in /WEB-INF folder When to use If the file is subject to modification very often If the size of the file is huge
17
Include Example – Menu Menu Item 1 Menu Item 2 Menu Item 3 JSP Menu Demo Contents goes here... menu.html (note: With include directive, you can give any file extension to this file.) In this example, using produces same output
18
Reusable JSP Content: ContactSection.jsp <%-- The following become fields in each servlet object that results from a JSP page that includes this file. --%> <%! private int accessCount = 0; private Date accessDate = new Date(); private String accessHost = " No previous access "; %> © 2000 my-company.com. This page has been accessed times since server reboot. It was last accessed from at.
19
… Some Random Page Information about our products and services. Blah, blah, blah. Yadda, yadda, yadda. Using the JSP Content
20
jsp:forward Action Used to instruct a web server to stop processing the current page and start another one. Similar to using a RequestDispatcher object to forward a request Syntax or …
21
Note: For both jsp:include and jsp:forward The parameters passed to the included/forwarded resources can be obtained as request.getParameter("paramName") If the value of page attribute begins with '/', then the path is evaluated relative to the application root folder. Otherwise the path is evaluated relative to the current folder.
22
References Wikipedia: JavaServer Pages http://en.wikipedia.org/wiki/JavaServer_Pages Free Tutorial (Java, JSP, Java Servlets) http://www.courses.coreservlets.com/Course-Materials/ Sample JSP codes http://www.java2s.com/Code/Java/JSP/CatalogJSP.htm
Similar presentations
© 2025 SlidePlayer.com Inc.
All rights reserved.