Presentation is loading. Please wait.

Presentation is loading. Please wait.

DT228/3 Web Development JSP: Directives and Scripting elements.

Similar presentations

Presentation on theme: "DT228/3 Web Development JSP: Directives and Scripting elements."— Presentation transcript:

1 DT228/3 Web Development JSP: Directives and Scripting elements

2 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

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

4 Template text= all text within the JSP page that is NOT JSP code For web sites, usually template text = HTML but Template text can be anything: WML for mobile devices, javascript, HTML, PDF etc HTML (or whatever) is just "passed through" to the client by the servlet created to handle the page. The HTML can be created by whatever tools you already are using for building Web pages. e.g. FrontPage Development JSP Pages: Template Text

5 Development JSP Pages: Template Text example JSP is Eazy JSP is as easy as … 1+2+3=

6 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

7 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:

8 JSP Pages: Directive Elements There are Three directives available to use: –

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

10 Directive Elements: Page Page directive - defines attributes that apply to an entire JSP page. Examples

11 List of attributes includes Directive Elements: Page

12 Details for all Page directive attribute can be found at 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

13 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

14 An Include Test The current date and time is Directive Elements: Include Example: jsp page name = includexample.jsp

15 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

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

17 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

18 Directive elements Action elements and JSTL Scripting elements Java Beans JSP dynamic processing Done

19 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

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

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

22 Expressions - examples any valid java expression

23 Scripting elements: Expressions Example Current time is:

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

25 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

Download ppt "DT228/3 Web Development JSP: Directives and Scripting elements."

Similar presentations

Ads by Google