JSP ScriptletTag

JSP ScriptletTag

JSP scripting elements : These elements used to insert Java code inside the JSP(Java Server Pages). There are three type of scripting elements:

  1. Scriptlet  Tag
  2. Expression Tag
  3. Declaration  Tag

 

  1. Scriptlet Tag: The Tag From which we can execute java source code which is in JSP. The Syntax of this tag is like below

Let’s get understand it by an example. With the help of below Example we can print our java message on JSP.

Write this code on your JSP page and you will get output like below Image.

1

Now Lets get Understand it by one more example. We are creating two JSP file  first one is index.jsp and second is Welcome.jsp. We Will send data from index.jsp to Welcome.jsp and the code will be like below:

index.jsp:

Welcome.jsp:

And the result of these jsp is like below images:

2

3

 

  1. Expression Tag: This tag is used to write output of Placed code within it. We don’t need to write out.print() to write output. Most of the time this Tag is used to write the values of variable or methods. The Syntax of this Tag is written like below code

Let’s use last example for understanding this Tag.

Welcome.jsp:

The output for this expression tag code will look like below Image The index.jsp page output is same like above image and we did not change anything in index.jsp.

4

  1. Declaration  Tag : From the word it is clear that this tag is used for declaration. Declaration Tag is used to declare fields and methods. The code which is used in declaration tag placed outside service() method of auto generated servlet. It doesn’t get memory at each request.

Syntax of Declaration Tag is:

Let’s get understand it by an example. This is the example of Declare Field and printing the value of that field also we have used JSP expression tag for printing the output of declared field.

The output of above code will look like below image

5

Now lets do code for declare method. This is the example of Delcare method which will written square of given number and the output is displayed through Expression Tag.

The Output of this code will look like below Image.6