SIGN UP MEMBER LOGIN:    
ARTICLE

Introduction Of JSP Scripting Tags

Posted by Vikas Mishra Articles | JSP November 21, 2011
JSP scripting tags are very important tags which allow a java programmer to add a script code into the java code of a JSP.
Reader Level:
Download Files:
 

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

Login to add your contents and source code to this article
share this article :
post comment
 
6 Months Free & No Setup Fees ASP.NET Hosting!
Become a Sponsor
PREMIUM SPONSORS
  • Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
    The leading .NET charting control now features PDF, Flash and Silverlight export, visualization of large datasets and more. Deliver true charting functionality to your BI, Scorecard, Presentation or Scientific apps. Download evaluation now.
6 Months Free & No Setup Fees ASP.NET Hosting!
Become a Sponsor