by Dinesh Thakur Category: JSP Elements

A JSP page contains tags. The tags are in the form of scripting tags, directive tags and comments. Tags help programmers to develop Java codeless JSP program. Implicit objects increase the functionality of JSP page. Scripting element also helps in generating dynamic content and displaying dynamic content.

Categorization of Tags

We have categorized the tags into mainly three types:

• Scripting tags

• Directive tags

• Standard action tags

JSP also has some commenting tags for Java code as well as for html code that are given in this chapter. We will discuss the directive tags and action tags in the chapters.

JSP scripting elements enable us to insert code into the Servlet that is generated from JSP page. In most of the cases, java is used as scripting language to build JSP page however other supported languages can be used.

Types of Scripting Tags

There are three type of scripting tags:

1. Scriptlet tag

2. Declaration tag

3. Expression tag

1. Scriptlet Tag

Java code placed in scriptlet goes to _jspService (-,-) of JSP equivalent Servlet. We generally place business logic or request processing logic in scriptlets. Then what happens for methods, variables, and for implicit objects.

For methods

We can call methods from scriptlet but we cannot define methods in side scriptlet. Because our defined method is going to be placed into _jspService (-,-) as a nested method but Java does not support nested methods.

For variables

Scriptlet variables are locale variables in _jspService (-,-).

For implicit objects

All implicit objects are visible inside scriptlet

There are two syntaxes for JSP document/page.

Standard syntax or traditional syntax

<% script codes %>

XML based syntax

<jsp:scriptlet>

Script code

</jsp:script code>

Working with scriptlet

Create a deployment directory structure and place the JSP file parallel to WEB-INF and give request to the JSP file as done in the above program.

a.jsp

<% int a=12;

int b=230;

int c=a*b;

out.println (“multiplication is” +c);

%>

In this example, JSP page prints multiplication of two number as output on the browser window.

2. Declaration Tag

Code placed in declaration tag goes outside of _jspService (-,-) in JSP equivalent Servlet. This tag allows to declare instance and class variables and implements class methods, such as jspInit (), jspDestroy (). Here we can

define methods. Implicit objects are not visible to declaration tag.

Standard syntax of declaration tag

<%! Script codes %>

Xml based syntax

<jsp:declaration>

Script codes

<jsp:declaration>

Example to use declaration tag

abc.jsp

<%! public int diff (int x, int y)    

{

   return x-y;

}%>

<% out.println (“difference is” +diff (87348,20));%>

3. Expression Tag

We have decided to make Java code less JSP program. Then why are we using out.println (). Is there any option to avoid it! Yes by using expression tag?

This tag is used to evaluate expression and to write the generated output on browser window. The code of this tag goes to _jspService (-,-) method of java equivalent Servlet. So in expression tag? implicit objects are visible. This tag is, used for method calls, for displaying variables and to evaluate the expression.

Standard syntax of expression tag

<%= java expression/script code %>

• XML based syntax

<jsp:expression>

Java expression/script code

</jsp:expression>

Example using expression tag in JSP page

xyz.jsp

<% int a=70;

int b=20; %>

<font color=“red”>You have entered a value=: <%=a%> </font> <br>

<font color=“red”>You have entered b value=: <%=b%> </font> <br>

<font color= “green”> <b> Sum, difference, multiplication is respectively---->:

<%= (a+b) + “,”+ (a-b) + “ ”+ (a*b) %> </b> </font>

To execute multiple expression at a time, write: <%= (a+b)+ “”+(b+a) %>

What does expression tag do and why does it is not end with a semicolon? Container takes everything placed inside <%= %> and puts as the argument of implicit response out of Print Writer.

The code

<%=a%> becomes

out.print (a);

If we place ‘;’ at the end of expression tag then

<%=a;%> it becomes

out.print(a;); -> this is not going to compile

So never end an expression tag with semicolon ( ;)

Point to be considered while using expression tags

Do not end an expression with ‘,’ because the expression given in this tag is

placed into the out.print () method. i.e. <%= i;%> becomes out.println (i;). The expression given in the expression tag should not be void type.

Nested scripting is not allowed. That means we cannot place <%= %> inside <%%>, it will generate a translation error. For example:

<% int a=12;

int b=23;

int c=a+b;

<%=c %>

%>

Output-error

Example using all scripting tag utilization

Here is a simple example of obtaining square of a number: in exercise part.

Request url- http://localhost:8081/JspApp2/ second.jsp

         

second.jsp

<marquee > <b> <font color=“green” size=14> Example giving all scripting

tags utilization </b> </font> </marquee>

<form action= “third.jsp”>

<font color= “red”> Enter the number <input type=“text” name=“x”> </font>

<input type= “submit” value= “submit”>

third.jsp

<%@ page import=“java.util.*”%>

<% int n=Integer.parseInt (request.getParameter (“x”);

%>

<%! public int square (int n) {

return n*n;

}%>

<font color=“red” size=7> <%=square (n)%> </font>

For better understanding of JSP program, we are giving JSP equivalent Servlet code that is generated by Tomcat server. Because after knowing which code is placed where in JES (JSP equivalent), JSP becomes a plain old servlet.

JES of second.jsp

public final class second_jsp extends org.apache.jasper.runtime.HttpJspBase

implements org.apache.jasper.runtime.JspSourceDependent

{

     ……………..

     public void _jspInit () {……}

     public void _jspDestroy () { }

     public void _jspService (HttpServletRequest request, HttpServlet

     Response response) throws java.io.IOException, ServletException

    {

         try

        {

             out.write (“<marquee> <b> <font color=\“green\” size=14>

             Example giving all scripting tags utilization </b> </font>

             </marquee\r\n”>;

             out.write(“<form action=\“third.jsp\”>\r\n”);

             out.write (“<font color=\“red\”> Enter the number <input type=\

             “text\” name=\“x\”> </font>\r\n”);

              out.write (“\r\n”);

              out.write (“<input type=\“submit\” value=\“submit\”>\r\n”);

         }

         Catch  () {}

         Finally {}

     }

}

We can see from here that all html codes are placed in _jspService (-,-) as the argument of out.write ();

JES for third.jsp

public final class third_jsp extends org.apache.jasper.runtime.HttpJspBase

implements org.apache.jasper.runtime.JspSourceDependent

{

     public int square (int n)

    {

         return n*n;

    }

     public void _jspInit () {…..}

     public void _jspDestroy () {}

     public void _jspService (HttpServletRequest request, HttpServlet

     Response response) throws java.io.IOException, ServletException

    {

         out.write (‘\r’);

         out.write (‘\n’ );

         int n=Integer.parseInt (request.getPararneter (“x”));

         out.write (‘\r’) ;

         out.write (‘\n’) ;

         out.write (“\r\n”);

         out.write (“<font color=red size=7>”);

         out.print (square (n));

         out.write (“</font>\r\n”);

.... }

}

From this JES we can get:

Public int square (int n) {return n*n;}-the declaration codes are placed outside of _jspService (-,-) and just below the class declaration.

int n=Integer.parseInt (request.getParameter (“x”));-scriptlet code is placed inside _jspService (-,-) but not as out.write () method arguments.

out.write (“<font color=“red” size=7>”);

out.print (square (n));

out.write (“</font>\r\n”);

Expression code is placed inside out.print () of _jsp5ervice (-,-) and we can also see the clean separation of logic with the use of out.write () and out.

print ()

Process to use life cycle convince method using declaration tag

In every server, generated Java equivalent Servlet _isp5ervice (-,-) comes with scriptlet or expression tag values. To put our logic inside other life-cycle convince method, use declaration tag as follows:

<%! public void jspInit ()

{

Put your logic .....

}%>

<%! public void jspDestroy ()

{

Put your logic.….

}%>





About Dinesh Thakur

Dinesh ThakurDinesh Thakur holds an B.SC (Computer Science), MCSE, MCDBA, CCNA, CCNP, A+, SCJP certifications. Dinesh authors the hugely popular blog. Where he writes how-to guides around Computer fundamental , computer software, Computer programming, and web apps. For any type of query or something that you think is missing, please feel free to Contact us.