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
Scripting elements are of the following five types.
- Comment tag
- Directive tag
- Scriptlet tag
- Expression tag
- Declaration tag
But, we generally 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 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 in the HTTP response.
Note
A pure JSP comment is called a "hidden comment". It is not visible at the client site and the syntax is given below.
Syntax
- < % ...comment lines.. % >
Note
The HTML comment is known as an "output comment". It is visible at the client site and the syntax is given below.
Syntax
The following is an example to illustrate this tag.
- <html>
- <head>
- <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
- <title>JSP Comment tag</title>
- </head>
- <%
- int a=1;
- int b=2;
- int c=a+b;
- %>
-
- <body>
-
- < %.. This will add the two numbers ..% >
- <br>
- <br>
- The addition of two numbers is <% out.println(c);%>
- </body>
- </html>
Output
In the preceding output, you can see the comment is shown because we have used a pure JSP comment. 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
JSP Directive tag
Directive tags are used to provide special instructions to the web container, during the page translation.
Syntax
Directive tags, are the following three types:
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
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:
- <%@ page import="java.util.Calendar" %>
- Or
- <%@ page import="java.util.Date" %>
language attribute
The "language" attribute defines which scripting language is to be used, in the JSP page.
For example:
- <%@ page language="java" %>
ErrorPage attribute
The "errorPage" attribute indicates another JSP page that will handle all of the runtime exceptions thrown by the current JSP page.
For example:
- <%@ page errorPage="error.jsp" %>
isErrorPage attribute
The "isErrorPage" attribute declares whether the current page represents another JSP's page.
For example:
- <%@ page isErrorPage="true" %>
- Or
- <%@ page isErrorPage ="true" %>
contentType attribute
The "contentType" attribute defines the Multipurpose Internet Mail Extensions (MIME) type, for the JSP response.
For example:
- <%@ page contentType="text/html" %>
Session attribute
The "session" attribute defines whether or not the JSP page is participating in an HTTP session.
For example:
- <%@ page session="true" %>
- Or
- <%@ page session="false" %>
isThreadsafe attribute
The "isThreadSafe" attribute defines whether the JP page is thread-safe, or not.
For example:
- <%@ page isThreadSafe= "true" %>
- Or
- <%@ page isThreadSafe= "false" %>
autoFlush attribute
The "autoFlush" attribute defines whether the buffered output is flushed automatically. 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:
- <%@ page buffer="none" %>
- Or
- <%@ page buffer="10kb" %>
info attribute
The "info" attribute provides a description of the JSP page.
For example:
- <%@ page info="This page is built by Gopi" %>
extends attribute
The "extends" attribute defines a superclass that the generated servlet must extend.
For example:
- <%@ 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:
- <%@ include file="filename.jsp" %>
Example
include.jsp
- <html>
- <head>
- <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
- <title>Include page</title>
- </head>
- <body>
- <%@include file="add.jsp" %>
- </body>
- </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
- <html>
- <head>
- <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
- <title>Addition Page</title>
- </head>
- <%
- int a=2;
- int b=3;
- int c=a+b;
- %>
- <body>
- The sum of two numbers is:
- <%
- out.println(c);
- %>
- </body>
- </html>
Output
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:
- <%@ taglib prefix="prefixOfTag" uri="uriOfTagLibrary"%>
- Or
- <%@ taglib prefix="mine" uri="randomName"%>
Each library, used in a page, needs its own taglib directive with a unique prefix. 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 the userName.
- <html>
- <head>
- <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
- <title>Taglib page</title>
- </head>
- <%@ taglib prefix="mine" uri="yourTags" %>
- <body>
- Hello..!! <mine:userName/>
- </body>
- </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:
Let's see a simple JSP scriptlet tag example.
- <html>
- <head>
- <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
- <title>Sciptlet tag page</title>
- </head>
- <body>
- <% out.println("This is Scriptlet tag");%>
- </body>
- </html>
Output
Now, let's see another example.
In this example, the username will be printed along with the page count. For this, we will create two JSP files, one for the Graphical User Interface (GUI) and the other, for the scripting.
Index.jsp
- <html>
- <head>
- <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
- <title>JSP main Page</title>
- </head>
- <body>
- <div id="Login" style="background-color: pink">
- <form action="Scriptlet.jsp" method="post">
- <strong>Username</strong>:<input type="text" name="uname"><br><br>
- <input type="submit" name="Login">
- </form>
- </div>
- </body>
- </html>
Output
Scriptlet.jsp
- <html>
- <head>
- <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
- <title>Sciptlet tag page</title>
- <%
- int count=0;
- %>
- </head>
- <body>
- <%
- String name=request.getParameter("uname");
- out.println("Hello..!!"+name);
- %>
- <br>
- <br>
- The page count is:
- <% out.println(++count);%>
- </body>
- </html>
Output: After submitting
JSP Expression tag
The JSP expression tag is used to print out Java language that is put between the tags. There is no need to write "out.println()", to print the content. It is mainly used to print the values of variables and methods.
The syntax is as follows:
Let's see a simple example of this.
- <html>
- <head>
- <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
- <title>Expression tag Page</title>
- </head>
- <body>
- <div style="background-color: pink">
- <%= "Welcome to expresssion tag"%>
- </div>
- </body>
- </html>
Output
Another example
In this example, the output will print the username, along with the date and time.
Index.jsp
- <html>
- <head>
- <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
- <title>JSP main Page</title>
- </head>
- <body>
- <div id="Login" style="background-color:lightblue">
- <form action="expression.jsp" method="post">
- <strong>Username</strong>:<input type="text" name="uname"><br><br>
- <input type="submit" name="Login">
- </form>
- </div>
- </body>
- </html>
Output
Expression.jsp
- <html>
- <head>
- <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
- <title>Expression tag Page</title>
- </head>
- <body>
- <div style="background-color: pink">
- <%= "Hello..!!"+request.getParameter("uname")%>
- <br>
- <%="Current time is:"+java.util.Calendar.getInstance().getTime()%>
- </div>
- </body>
- </html>
Output: After submitting
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
- <%! Field or method declaration %>
Example that declares fields
- <html>
- <head>
- <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
- <title>Declaration tag Page</title>
- </head>
- <body>
- <div style="background-color: pink">
- <%!
- int a=4;
- int b=2;
- int c=a+b;
- %>
-
- <%="The sum of two numbers:"+c%>
- </div>
- </body>
- </html>
Output
An example that declares the method
- <html>
- <head>
- <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
- <title>Declaration tag Page</title>
- </head>
- <body>
- <div style="background-color: pink">
- <%!
- int cube(int n){
- return n*n*n;
- }
- %>
-
- <%="Cube of 4 is:"+cube(4)%>
- </div>
- </body>
- </html>
Output