Professional Documents
Culture Documents
(JSP)
This article explains the details of JavaServer Pages (JSP) scripting elements.
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
Scriptlet tag
Expression tag
Declaration tag
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
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
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:
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
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
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
10. </body>
11. </html>
Output
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
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
12. %>
13.
14. <%="The sum of two numbers:"+c%>
15. </div>
16. </body>
17. </html>
Output
12. %>
13.
14. <%="Cube of 4 is:"+cube(4)%>
15. </div>
16. </body>
17. </html>
Output