Scripting Elements in JavaServer Pages (JSP)

Scripting elements

Scripting elements are those that provide the ability to insert Java code inside JSP. In JSP the scripting elements are written inside "< %.....% >" tags. The codes inside "<%....% >" tags are processed by the JSP engine during the translation of the JSP pages. The text or code written in JSP pages apart from the preceding tags is treated as HTML content.

Types of scripting elements

Basically scripting elements are of the following five types.

  • Comment tag
  • Directive tag
  • Scriptlet tag
  • Expression tag
  • Declaration tag

types of scripting elements 

But mainly we have the following three types.
  • Scriptlet tag
  • Expression tag
  • Declaration tag

JSP Comment tag

A JSP comment tag is used in JSP pages when you are creating something. When you want to explain how the procedure you are using to build the JSP pages, you can put your explanation in comment tags. These comments are only seen in JSP pages and not included in the servlet source code during the translation phase, they are not even in the HTTP response.

Note

A pure JSP comment is called a "hidden comment" that is not visible at the client site and the syntax is given below.

Syntax

  1. < %...comment lines..% >  
Note

The HTML comment is known as an "output comment" and is visible at the client site and the syntax is given below.

Syntax

  1. <!.. comment lines ..>  
The following  is an example to illustrate this tag.
  1. <html>  
  2. <head>  
  3. <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">  
  4. <title>JSP Comment tag</title>  
  5. </head>  
  6. <%  
  7. int a=1;  
  8. int b=2;  
  9. int c=a+b;  
  10. %>  
  11.   
  12. <body>  
  13.   
  14. < %.. This will add the two numbers ..% >  
  15. <br>  
  16. <br>  
  17. The addition of two numbers is <% out.println(c);%>  
  18. </body>  
  19. </html>  
Output

HTML output comment
In the preceding output you can see the comment is shown because we have use a pure JSP comment and if we use the HTML comment <!...comment…> then the comment will not be shown. Let's have a look at the next output with HTML comments.

Output

comment will not be shown

JSP Directive tag

Directive tags are used to provide the special instructions to the web container during the page translation.

Syntax

  1. <%@ directives %>  
Directive tags are of the following three types:
  • page
  • include
  • taglib

Page directive

The page directive defines various types of page-independent properties (such as language, session, errorPage, and so on) that communicate to the web container during the translation.

Syntax

  1. <%@ page… %>  
Some of the "page directives" are as follows.

import attribute

The "import" attribute defines that the specified set of classes or packages must be imported in the servlet class definition.

For example:
  1. <%@ page import="java.util.Calendar" %>  
  2. Or  
  3. <%@ page import="java.util.Date" %>  
language attribute

The "language" attribute defines which scripting language is to be used in the JSP page.

For example:
  1. <%@ page language="java" %> 
ErrorPage attribute

The "errorPage" attribute indicates another JSP page that will handle all the runtime exceptions thrown by the current JSP page.

For example:
  1. <%@ page errorPage="error.jsp" %>  
isErrorPage attribute

The "isErrorPage" attribute declares whether the current page represents another JSP's page.

For example:
  1. <%@ page isErrorPage="true" %>  
  2. Or  
  3. <%@ page isErrorPage ="true" %>  
contentType attribute

The "contentType" attribute defines the Multipurpose Internet Mail Extensions (MIME) type for the JSP response.
 
For example:
  1. <%@ page contentType="text/html" %>  
Session attribute

The "session" attribute defines whether or not the JSP page is participating in an HTTP sesseion.

For example:
  1. <%@ page session="true" %>  
  2. Or  
  3. <%@ page session="false" %>  
isThreadSafe attribute

The "isThreadSafe" attribute defines whether the JP page is thread-safe or not.
 
For example:
  1. <%@ page isThreadSafe= "true" %>  
  2. Or  
  3. <%@ page isThreadSafe= "false" %>  
autoFlush attribute

The "autoFlush" attribute defines whether the buffered output is flushed automatically and it's default value is true.

For example:

<%@ page autoFlush="true" %>

buffer attribute

The "buffer" attribute specifies buffering characteristics for the server output response object.
 
For example:
  1. <%@ page buffer="none" %>  
  2. Or  
  3. <%@ page buffer="10kb" %>  
info attribute

The "info" attribute provide a description of the JSP page.
 
For example:
  1. <%@ page info="This page is built by Gopi" %>  
extends attribute

The "extends" attribute defines a super class that the generated servlet must extend.

For example:
  1. <%@ page extends="somePackage.SomeClass" %>  
Include directives

The "include directives" indicates the web container to copy everything from the included file and paste it in the current JSP page.

The syntax is as following:
  1. <%@ include file="filename.jsp" %>  
Example

include.jsp
  1. <html>  
  2. <head>  
  3. <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">  
  4. <title>Include page</title>  
  5. </head>  
  6. <body>  
  7. <%@include file="add.jsp" %>   
  8. </body>  
  9. </html>  
In the preceding example, <%@include file="add.jsp" %> says to insert the complete contents of the "add.jsp" file into the "include.jsp" file.

add.jsp
  1. <html>  
  2. <head>  
  3. <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">  
  4. <title>Addition Page</title>  
  5. </head>  
  6. <%  
  7. int a=2;  
  8. int b=3;  
  9. int c=a+b;  
  10. %>  
  11. <body>  
  12. The sum of two numbers is:  
  13. <%  
  14. out.println(c);  
  15. %>  
  16. </body>  
  17. </html>  
Output

output of include.jsp
You can see in the output that by running "include.jsp" we get the content of "add.jsp" as the output.

Taglib directives

The "taglib directives" is generally used to define the tag library that the current JSP page uses.

The syntax of the taglib directive is as follows:
  1. <%@ taglib prefix="prefixOfTag" uri="uriOfTagLibrary"%>  
  2. Or  
  3. <%@ taglib prefix="mine" uri="randomName"%>  
Each library used in a page needs its own taglib directive with a unique prefix and the prefix is used to distinguish the custom tag from other library custom tags.

URI is the unique name for the tag library.

Example

The tag we are using in this example is userName.
  1. <html>  
  2. <head>  
  3. <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">  
  4. <title>Taglib page</title>  
  5. </head>  
  6. <%@ taglib prefix="mine" uri="yourTags" %>  
  7. <body>  
  8. Hello..!! <mine:userName/>  
  9. </body>  
  10. </html>  
JSP Scriptlet tag

A JSP scriptlet tag simply allows you to write the Java code in your JSP page.

The syntax is as follows:
  1. <% Java code %>  
Let's see a simple JSP scriptlet tag example.
  1. <html>  
  2. <head>  
  3. <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">  
  4. <title>Sciptlet tag page</title>  
  5. </head>  
  6. <body>  
  7. <% out.println("This is Scriptlet tag");%>  
  8. </body>  
  9. </html>  
Output

JSP scriptlet tag
Now let's see another example.

In this example the username will be printed along with the page count. And for this we will create two JSP files, one for the Graphical User Interface (GUI) and the other for the scripting.

Index.jsp
  1. <html>  
  2. <head>  
  3. <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">  
  4. <title>JSP main Page</title>  
  5. </head>  
  6. <body>  
  7. <div id="Login" style="background-color: pink">  
  8. <form action="Scriptlet.jsp" method="post">  
  9. <strong>Username</strong>:<input type="text" name="uname"><br><br>  
  10. <input type="submit" name="Login">  
  11. </form>  
  12. </div>  
  13. </body>  
  14. </html>  
Output

username will be print
Scriptlet.jsp
  1. <html>  
  2. <head>  
  3. <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">  
  4. <title>Sciptlet tag page</title>  
  5. <%  
  6. int count=0;  
  7. %>  
  8. </head>  
  9. <body>  
  10. <%   
  11. String name=request.getParameter("uname");  
  12. out.println("Hello..!!"+name);  
  13. %>  
  14. <br>  
  15. <br>  
  16. The page count is:  
  17. <% out.println(++count);%>  
  18. </body>  
  19. </html>  
Output: After submitting

After submitting
JSP Expression tag

The JSP expression tag is used to print out Java language that is put between the tags. So there is no need to write " out.println() " to print the content and it is mainly used to print the values of variables and methods.

The syntax is as follows:
  1. <%= expression%>  
Let's see a simple example of this.
  1. <html>  
  2. <head>  
  3. <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">  
  4. <title>Expression tag Page</title>  
  5. </head>  
  6. <body>  
  7. <div style="background-color: pink">  
  8. <%= "Welcome to expresssion tag"%>  
  9. </div>  
  10. </body>  
  11. </html>  
Output

JSP Expression tag
Another example

In this example, the output will print the username along with the date and time.

Index.jsp
  1. <html>  
  2. <head>  
  3. <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">  
  4. <title>JSP main Page</title>  
  5. </head>  
  6. <body>  
  7. <div id="Login" style="background-color:lightblue">  
  8. <form action="expression.jsp" method="post">  
  9. <strong>Username</strong>:<input type="text" name="uname"><br><br>  
  10. <input type="submit" name="Login">  
  11. </form>  
  12. </div>  
  13. </body>  
  14. </html>  
Output

output will print the username
Expression.jsp
  1. <html>  
  2. <head>  
  3. <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">  
  4. <title>Expression tag Page</title>  
  5. </head>  
  6. <body>  
  7. <div style="background-color: pink">  
  8. <%= "Hello..!!"+request.getParameter("uname")%>  
  9. <br>  
  10. <%="Current time is:"+java.util.Calendar.getInstance().getTime()%>  
  11. </div>  
  12. </body>  
  13. </html>  
Output: After submitting

Expression Output
JSP Declaration tag

When we declare a variable or method in JSP inside a "declaration tag" the declaration is made in the servlet class (because at the end a JSP page is translated into a servlet class) but outside the service method. You can declare static member, instance variable and methods inside the declaration tag.

Syntax of declaration tag
  1. <%! Field or method declaration %>  
Example that declares fields
  1. <html>  
  2. <head>  
  3. <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">  
  4. <title>Declaration tag Page</title>  
  5. </head>  
  6. <body>  
  7. <div style="background-color: pink">  
  8. <%!  
  9. int a=4;  
  10. int b=2;  
  11. int c=a+b;  
  12. %>  
  13.   
  14. <%="The sum of two numbers:"+c%>  
  15. </div>  
  16. </body>  
  17. </html>  
Output

out of declares fields
Example that declares method
  1. <html>  
  2. <head>  
  3. <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">  
  4. <title>Declaration tag Page</title>  
  5. </head>  
  6. <body>  
  7. <div style="background-color: pink">  
  8. <%!  
  9. int cube(int n){  
  10. return n*n*n;  
  11. }  
  12. %>  
  13.   
  14. <%="Cube of 4 is:"+cube(4)%>  
  15. </div>  
  16. </body>  
  17. </html>  
Output
 
 output of declares method

Up Next
    Ebook Download
    View all
    Learn
    View all