Introduction Of JSP Scripting Tags


Working with Scripting Tags :

JSP Scripting Tags allow a java programmer to add a script code into the java code of a JSP. In most of the cases java is the scripting language used to build a JSP page. There are many types of scripting tag:

Types of Scripting Tags:

There are mainly three JSP scripting tags:

  1. The Scriptlet Tag: This tag allow you to declare variables, that you can use later in JSP page. It is also allow you to write the script code for implementing the _jspService method. A JSP scriptlet is used to contain any code fragment that is valid for the scripting language used in a page.

    The syntax for a scriptlet is as follows: 

    <%  
         script code
    %>

    Example:

    <html>
    <body>
    <%
    for (int i=0; i<10;i++)
    {
    out.println(i);
    }
    %>
    </body>
    </html>

  2. The Declarative Tag: This tag allows you to declare instance and class variable and implement class  methods such as jspInit and jspDestroy. JSP declaration is used to declare variables and methods in a page's scripting language.

    The syntax for a declaration is as follows:

    <%!
         script code
    %>

    Example:

     <%!
    int i, j;
    int fun (int i)
    {
    return i;}
    %>
    <%i=1;>
    <%j=fun (i);%>
    <%out.println(j);%>

  3. The Expression Tag: Through this tag we show the resultant value. The expression tag places in the out. print method during the translation stage of the JSP lifecycle.

    A JSP expression is used to insert the value of a scripting language expression, converted into a string, into the data stream returned to the client. When the scripting language is the Java programming language, an expression is transformed into a statement that converts the value of the expression into a String object and inserts it into the implicit out object.

    The syntax for an expression is as follows:

    <%=
        script code
    %>

    Example:

    <%! int count; %>
    <html> <body>
    <% count++ %>
    This page is requested by <%=count%> number of users till now.
    </body>
    </html>

Example of Scripting tags:

<%@page import="java.util.*" language="java" %>
<html>
<body bgcolor="pink">
<center><h1>Using Scripting Elements</h1></center>
<%! int count,a,b;
int fun (int a)
{
return 10*a;
}%>
<% a=1;
count++;
for (int i=0;i<5;i++)
{
out.println("value of i in iteration no.<br/>");
}
b=fun(a);
out.println("value of b");
%>
This page is requested by <b><%=count%></b>number of times on date
<b><%=new Date()%>
</b>
</body>
</html>

Running the application on the server:

Run the tomcat server then write the below line in the URL

http://localhost:8080/JSP/

Here JSP is the Context path, which we mentioned in the server.xml file, which is present in (E:\Program Files\Apache Software Foundation\Tomcat 6.0\conf) directory.

After giving the URL a set a listing will come, here only one appears

As index.JSP,click it

http://localhost:8080/JSP/index.JSP

OUTPUT:

scriptingtag.gif

Up Next
    Ebook Download
    View all
    Learn
    View all