JSP: Scripting Elements

We can use JSP pages to write presentation logic to separate the business logic from presentation layer. Once the JSP page is requested; web-server translates into a Servlet and Servlet will insert the business logic wherever required. To write the presentation logic in JSP we need JSP elements. In this article I am going to discuss about the scripting elements.

There are 3 scripting elements JSP use to allow to embed Java code; they are expressions, scriptlets and declarations.

Expressions

The expression element enclosed in the pair <%= and %>. The results of the statements which we write inside expression tags are goes to out.println() statements. So we can use expression elements, to avoid out.println() statements inside JSP code.

Lets take a simple example and see how we can use expression elements in JSP page.

// demo.java

package com.shilpa.codesteps;

public class Demo
{
   public static String show()
   {
      return “hello”;
   }
}

From the above code, we have created a Demo class and added a member function “show”. “show” function just returns “hello” message. Observe that this is a pure Java code.

Now we are going to call this “show” function from the JSP page. The JSP page code looks like below:

<!-- view.jsp -->

<%@page import="com.shilpa.codesteps.Demo" %>

<%= Demo.show()%>

In the above code we have used “page” directive to import the package into current JSP page and called the “Demo” class’s method “show”. I will explain more about “page” directive in our upcoming articles.

We need to remember below things when using expression elements within the JSP page:

  • We are not allowed to write semicolon (“;”) at the end of statements in expressions, because an expression element is not a Java statement.
  • The result of a Java expression in an expression element goes to “out.println()” indirectly. Hence the result of a Java expression should not be of type void.
  • The code written in expressions will goes into _jspSrevice(); a JSP life cycle method responsible for generating response for each request. This method is invoked by the JSP Engine.

Scriptlets

Scriptlet is a block of Java code enclosed between <% and %>

For example, below is the simple code to demonstrate the usage of Scriptlets:

<% out.println("This is a Scriptlet."); %>

<h1>This is an HTML</h1>

Below things we need to remember when writing code within the Scriptlets:

  • The code which we write inside scriptlets is goes into _jspService().
  • We can declare variables in scriptlets; those variables are local variables to _jspService().
  • We are not allowed to write methods in scriptlets because in Java there is no concept of method inside a method (because if we write a method the method goes to _jspService() method, which is wrong.)

Declarations

Declaration element is identified by <%! and %>. We use declaration elements to declare any variables and even class level data like its members and methods etc.,. Also we can override jspInit() and jspDestroy() methods in declaration elements. These are the JSP life cycle methods. Remember that we can not override the JSP life cycle method _jspService().

Below is an example of a declaration element:

<%!
   int a;

   void show()
   {
      // TODO
   }
%>

These are the scripting elements in JSP. We will discuss about more topics in JSP in our upcoming articles.

1 comment for “JSP: Scripting Elements

Leave a Reply