JSP Scripting Elements

 < Free Open Study > 



There are three kinds of scripting element in JSP:

  • Declarations

  • Scriptlets

  • Expressions

Declarations

Declarations are used to define methods and instance variables. They do not produce any output that is sent back to the client. Declarations in JSP pages are embedded between <%! and %> delimiters, for example:

     <%!       public void jspDestroy() {         System.out.println("JSP destroyed");       }       public void jspInit() {         System.out.println("JSP loaded");       }       int myVariable = 123;     %> 

The two methods and the variable will be made available to the page implementation class and we can access these functions and variables within the JSP pages.

Scriptlets

Scriptlets are used to embed Java code within JSP pages. The contents of scriptlets go within the _JSP pageservice() method. The lines of code embedded in JSP pages should comply with the syntactical and semantic constructs of Java. Scriptlets are embedded between <% and %> delimiters, for example:

     <%       int x = 10;       int y = 20;       int z = 10 * 20;     %> 

Expressions

Expressions in JSP pages are used to write dynamic content back to the browser and are embedded in <%= and %> delimiters. If the output of the expression is a Java primitive, the value of the primitive is printed back to the browser. If the output is a Java object, the result of calling the toString() method on the object is written back to the browser.

For example this would print the string Fred Flintstone to the client:

     <%="Fred" + "Flintstone" %> 

This expression would print the string 10 to the client:

     <%= Math.sqrt(100) %> 



 < Free Open Study > 



Professional Java Servlets 2.3
Professional Java Servlets 2.3
ISBN: 186100561X
EAN: 2147483647
Year: 2006
Pages: 130

Similar book on Amazon

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net