Presentation is loading. Please wait.

Presentation is loading. Please wait.

Three types of scripting elements: 1.Expressions 2.Scriptlets 3.Declarations Scripting elements.

Similar presentations


Presentation on theme: "Three types of scripting elements: 1.Expressions 2.Scriptlets 3.Declarations Scripting elements."— Presentation transcript:

1 Three types of scripting elements: 1.Expressions 2.Scriptlets 3.Declarations Scripting elements

2 Scripting elements: Scriptlets Scriplets are java code fragments with a JSP page Enables more complex functionality than expressions Java code is placed between characters (just like expressions, but without the = sign at the start of the sequence.) Can have any number of valid java code statements Scriptlet contains Java code that is executed every time the JSP is invoked Syntax:

3 Scripting elements: Scriptlets Example – jsp page that outputs numbers 1 to 10 onto a web page html> Count to 10 in JSP scriptlet <% for(int i=1;i<=10;i++) {%> <% } %> Expression used to output to page. Can’t put HTML Tags into the scriptlet. Can only contain valid java code Scriptlets

4 Scripting elements: Scriptlets Example – jsp page that outputs numbers 1 to 10 onto a web page – Output in browser will be:.. Count to 10 in JSP 1 2 3 4 5 6 7 8 9 10

5 Scriplets: mixing scriplets with HTML and other tags When you mingle scripting elements with HTML and JSP tags, you must always end a scripting element before you start using tags and then reopen the scripting element afterwards, like this:... tags follow... At first, this may look a bit strange, but it ensures that the scripting elements are transformed correctly when the JSP source file is compiled

6 Scripting elements: Scriptlets Example 2 – jsp page that outputs “Hello! The time is now Wed Sep 03 13:17:58 BST 2006 Your machine's address is 127.0.0.1” onto a web page <% java.util.Date date = new java.util.Date(); %> Hello! The time is now <% out.println( date ); out.println( " Your machine's address is " ); out.println( request.getRemoteHost()); %> Note: could have put this line outside the scriptlet as HTML

7 Note: Any text, HTML tags, or JSP elements you write must be outside the scriptlet Readability: Mixture of Tags and Java code can be difficult to read – especially for HTML developers Scripting elements: Scriptlets

8 Have a nice day! Have a lousy day! Scripting elements: Scriptlets In background, JSP container compiles the JSP page into Java code.. converting the HTML snippets into java code to out.print statements --- 

9 Scripting elements: Scriptlets if (Math.random() < 0.5) { out.println("Have a nice day!"); } else { out.println("Have a lousy day!"); } JSP code on previous page will be converted in background to the following java code..

10 <% for ( int i = 0; i < n; i++ ) { %> Number <% } %> Example 3 – fragment of JSP page that generates a table in HTML containing numbers 1 to n Note: would need to supply int variable n before it will work… HTML within “for” loop is output n times Scripting elements: Scriptlets

11 Three types of scripting elements: 1.Expressions 2.Scriptlets 3.Declarations Scripting elements

12 Scripting elements: Declarations Declaration: Declares a variable or method that can be used throughout the JSP page Examples Syntax Note the “ ;”

13 Scripting elements: Declarations Declarations don’t generate any output onto the page by themselves -  usually used with expressions and/or scriptlets Examples Accesses to page since server reboot: Prints the number of times the current page has been requested since the server was booted

14 Example – Declares method getDate() <%! Date theDate = new Date(); Date getDate() { System.out.println( "In getDate() method" ); return theDate; } %> Hello! The time is now Scripting elements: Declarations Method getDate() returns a Date object

15 Declaration Declarations (between tags) contain one or more variable or method declarations that end or are separated by semicolons: You must declare a variable or method in a JSP page before you use it in the page. The scope of a declaration is usually a JSP file, but if the JSP file includes other files with the include directive, the scope expands to cover the included files as well.

16 Declarations Warnings! Declaring variables in a JSP page using declarations can cause synchronisation problems.. Compiled servlets see these as variables which may be shared across all users using the JSP page. On previous date example, Date stays the same when the page is reloaded.. Because only a single instance of the page (and there of the variable theDate is available). Better to use local variables within declared methods or scriptlets

17  Scripting elements enable java code to be embedded directly into the JSP page  Be aware of: code readability, complexity, maintainability  Three forms of Scripting elements:  Expressions  Scriptlets  Declarations Scripting elements - Summary

18 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 "Three types of scripting elements: 1.Expressions 2.Scriptlets 3.Declarations Scripting elements."

Similar presentations


Ads by Google