DT228/3 Web Development JSP: Directives and Scripting elements.
Published byModified over 5 years ago
Presentation on theme: "DT228/3 Web Development JSP: Directives and Scripting elements."— Presentation transcript:
DT228/3 Web Development JSP: Directives and Scripting elements
JSP techniques Directive elements Action elements and Java Standard Tags Library Scripting elements (java) JSP provides variety of techniques to enable dynamic processing: Java Beans In this topic
Development JSP Pages Q: Do you need to use Java to develop JSP pages? A: You can (using scripting elements), but you don’t have to and there are good reasons not to. Can use other techniques provided as part JSP technology, in particular: The Java Standard Tag Library (JSLT) – provides a set tags which provide the equivalent functionalty of writing java code in JSP pages.
Development JSP Pages: Template Text example JSP is Eazy JSP is as easy as … 1+2+3=
Development JSP Pages: Comments A comment in a JSP page looks like: Everything between the is ignored by the JSP container The comments are JSP code elements so are NOT send to the browser (so not visible in View source) - Hidden comments
JSP Pages: Directive Elements Directive Elements - Used to provide information about the general set-up of the page. e.g. inclusion of header files, name of page to report errors, whether session tracking is required etc Always enclosed between Syntax:
JSP Pages: Directive Elements There are Three directives available to use: –
Directive Elements Each directive has a set of associated attributes (similar to some tags in HTML) Usually placed at top of JSP file but doesn’t have to be Example: Full list of attributes available at: http://java.sun.com/products/jsp/syntax/1.1/syntaxref11.html
Directive Elements: Page Page directive - defines attributes that apply to an entire JSP page. Examples
List of attributes includes Directive Elements: Page
Details for all Page directive attribute can be found at http://java.sun.com/products/jsp/syntax/1.1/synt axref11.html Home page for this is located in ‘Web development’ on distrib/dt228/dt228-3/web development E.g. extract for page directiveextract Directive Elements: Page
Directive Elements: Include Include directive - Includes a static file in a JSP file at translation time Syntax The included file can be an HTML file, a JSP file, a text file, or a code file written in the Java programming language Useful for including repetitive HTML content across a web site – headers, navigation bars, footers etc Useful for common logic – e.g. date displays
An Include Test The current date and time is Directive Elements: Include Example: jsp page name = includexample.jsp
Directive Elements: Include Example (continued) jsp page name = date.jsp When includexample.jsp is run, displays as The current date and time are Aug 30, 2006 2:38:40 Included into includexample.jsp from previous page
Directive Elements: Taglib Taglib directive - Defines a tag library and prefix for the custom tags used in the JSP page. Syntax Example: More on taglib directives later when we use Java Standard Tag Library (JSTL)
Directive Elements: summary Three directives: page, include, taglib Used to define general set-up information about the JSP page By themselves, don’t “do” anything At least one used in most JSP pages
Directive elements Action elements and JSTL Scripting elements Java Beans JSP dynamic processing Done
Scripting elements Developers in JSP can insert java code directly into a JSP pages using scripting elements The code is executed when the page is requested Should be used with extreme care: Too much code difficult to maintain Difficult for HTML programmers More suitable for simple applications with small development team Q: How do you specify that the language being used in page by scripting elements is java? A: Page directive language attribute
Scripting elements Three types of scripting elements: 1.Expressions: The expression syntax defines a scripting language expression.. “result” 2.Scriptlets: The scriptlet syntax can handle declarations, expressions, or any other type of code fragment valid in the page scripting language. When you write a scriptlet, end the scriptlet with %> before you switch to HTML, text, or another JSP tag 3. Declarations: The declaration syntax declares variables or methods.
Scripting elements: Expressions Expressions: Contains any valid java expression in the JSP page Used to output dynamic values directly onto web page (e.g. result of a calculation, dates) Enclosed between output as a string Syntax e.g.
Expressions - examples E.gs: any valid java expression
Scripting elements: Expressions Example Current time is:
Scripting elements: Expressions Note: Evaluated at run time. Result is added to the response body and output directly to web page Can use an expression within a line of text, whether or not it is tagged with HTML Must be a valid java expression No “;” required at end of expression (unlike scriptlets)
Topic summary Directive elements Specify set-up info about the JSP page Scripting elements Enable java code to be embedded into the JSP page Page Include Taglib Expressions Scriptlets Declarations