Saturday, April 28, 2012

JSP scripting elements : Declarations, Scriptlets And Expressions

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.
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
    <%! 
    //This is Declaration
    int counter;
    public void jspInit() {
        counter = 0;       
    }
    %>  
<html>
<head>
<title>JSP scripting elements example</title>
</head>
<body>
        <%
        //This is a scriptlet
        log("The lifecycle jsp has received a request");
        counter++;
        %>      
        <h2>JSP Life cycle : Request counter</h2>
        <p>This page has been called <%=counter %> times </p>
    </body>
</html>

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

<%! int i; %>
<%! int i = 0; %>
<%! public String methodA(int a)  { 
    return "some string"
} %>

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.
        <%
        //This is a scriptlet
        log("The lifecycle jsp has received a request");
        counter++;
        %>     
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
 <%   
  if(request.getAttribute("username") != null) {
 %>
  <div>Welcome <% out.write(request.getAttribute("username"));  %></div>
 <%} %>
Display 1 to 5 using loop
 <%    
  for(int i=1; i<6; i++) {
 %>
  <div><%= i %> </div>
 
 <%}%>

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.
 <p>This page has been called <%=counter %> times </p>
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.
<div>Welcome <%= request.getAttribute("username") %></div>
Display value of a variable
<div><%= variablename %> </div>
Display value of a session attribute
<div><%= session.getAttribute("name") %></div>

No comments:

Post a Comment