JSP Scripting Elements

This entry is part 4 of 14 in the series JSP Course

In this tutorial we are going to learn how to use JSP Expressions. In JSP there are three types of scripting elements:

  • JSP Expressions: It is a small java code which you can include into a JSP page. The syntax is “<%= some java code %>”
  • JSP Scriptlet: The syntax for a scriptlet is “<% some java code %>”. You can add 1 to many lines of Java code in here.
  • JSP Declaration: The syntax for declaration is “<%! Variable or method declaration %>”, in here you can declare a variable or a method for use later in the code.

Let’s take a look at these in detail.

JSP Expressions

Using the JSP Expression you can compute a small expression, always a single line, and get the result included in the HTML which is returned to the browser. Using the code we have previously written, let’s explore expressions.

Code

The time on the server is <%= new java.util.Date() %>

Output

The time on the server is Thursday January 21 07:21:43 GMT 2016.

Explanation

Here the “new java.util.Date()” is processed into the actual date and time shown through HTML on the browser. Let’s explore expressions through a couple of more examples.

Examples

  • In the first example we are going to see an expression for converting a string from lower case to upper case. Here is the code:

The Expression: Converting a string to uppercase <%= new String(“Hello World”).toUpperCase() %>

Here we are creating a “String” object with “Hello World” set as the value for object. Following that we are calling a Java function “.toUpperCase” to convert the string from lower case to upper case.

The HTML: Converting a string to uppercase: HELLO WORLD

  • You can also make use of mathematical expressions in JSP.

The Expression: 25 multiplied to 4: <%= 25*4 %>

Using simple mathematical expressions you can always compute and get answers to your mathematical problems embedded in your HTML.

The HTML: 25 multiplied to 4: 100

  • You can also have Boolean expressions in JSP.

The Expression: Is 75 less than 69? <%= 75 <69 %>

The return type of these Boolean expressions is string which is either going to be “true” or “false”

The HTML: Is 75 less than 69? False

JSP Scriptlets

This JSP Scripting Element allows you to put in a lot of Java code in your HTML code. This Java code is processed top to bottom when the page is the processed by the web server. Here the result of the code isn’t directly combined with the HTML rather you have to use “out.println()” to show what you want to mix with HTML. The syntax is pretty much the same only you don’t have to put in an equal sign after the opening % sign.

Let’s take a look at the code:

Code:

 

Output:

I really love counting: 1

I really love counting: 2

I really love counting: 3

I really love counting: 4

I really love counting: 5

Explanation:

In this example we have set up a basic h2 heading and following that we have a “for loop” in the scriptlet. Just to remember println means print line. In every iteration of the loop we print the “I really love counting” and appends it with the integer value of the “i” printed through the HTML.

Just try to make sure that you don’t put in a lot of code in a scriptlet in JSP. This will make it readable and easy to manage. If you can’t help it, try to refactor this code into different Java classes and make use of MVC to keep it under control.

JSP Declarations

The declarations come in handy when you have a code snippet that you want executed more than once. Using the declaration, you can declare the method in the beginning of the code and then call the same method whenever you need in the same page. The syntax is simple:

 

Code

Here is the method declaration:

Here is how you call it

Lower case “Hello World”:<%= makeItLower(“Hello World”) %>

Output

Lower case “Hello World”: hello world

Explanation

In the method declaration you have your standard java method with the return type of a String. You take a string as an argument and return it converted to lower case. Later we call this function through a JSP Expression.

Series Navigation<< JSP Fundamentals: JSP vs ServletMore JSP Fundamentals >>

Leave A Comment

Your email address will not be published. Required fields are marked *