This tutorials explains the JSP scripting elements. It is assumed that you have already read the JSP introduction. If you don't have basic understanding of what is a JSP page, please read theJSP introduction first.
As explained in JSP introduction tutorial, JSP provides template-based approach to content generation. JSP pages are textual documents containing HTML, XHTML, or XML markup with embedded Java code and custom JSP tags. The embedded Java code is used to generate dynamic content. Here the JSP scripting elements comes into the picture.
Declarations, Scriptlets and Expressions are called JSP scripting elements. A JSP file with only static HTML content is also a valid JSP page, but it will have no dynamic content, It is the scripting elements which gives the JSP the capability of dynamic content generation.
Without going into further theoretical details, let's see an example of a JSP page with Declarations, Scriptlet and Expression.
Declarations
Declarations are used to declare variables and methods to be used In JSP page. Declarations have the following form.
Anything declared inside declarations goes directly into the class body of generated servlet class, outside of any method. Declarations can not produce any output. Declarations are initialized when the JSP page is initialized. Mostly declarations are used to declare variables that will be available to all scriptlets in the JSP page.
In above example, we have used declaration to declare a variable and a method. You can think of declarations as a place where you can declare variables and methods.
Examples of declarations
Scriptlets
Scriptlets has following syntax
Scriptlets can contain any Java code and are used to generate the output dynamically. Everything inside the Scriptlets goes into the _jspService() method of generated servlet class. Hence scriptlets are executed at request processing time. Scriptlets can be used to do iteration and conditional executions. Scriptlets has access to all the variables and methods declared in declarations. Scriptlets has access to all of the JSP implicit objects.
In the example above, we have used a Scriptlet to increment the value of counter variable. It is important to remember that scriptlets will be executed each time the JSP receives a request.
Scriptlet Examples
Display welcome message if request attribute with name username is present
Display 1 to 5 using loop
Expressions
JSP expressions has following syntax
An expression is evaluated, converted to string and then emitted to output each time a request is received. It is important to remember that the expressions must evaluate to string, or the expression result must be able to be converted to string, otherwise an Exception would occur.
In the example above, we have used an expression to print the value of the counter variable to the output. Expressions are evaluated at run time and hence has access to all of the JSP implicit objects.
Expressions Examples
Display value of username request attribute.
Display value of a variable
Display value of a session attribute
No comments:
Post a Comment