JSP – Scripting elements

 JSP – Scripting elements 2017-08-09T14:24:45+00:00

There are four types of tag in jsp scripting elements.

a) JSP Declaration tag.
b) JSP scriptlet tag.
c) JSP Expression tag.
d) Comment tag.
Now we describe each tag in detail.

a) JSP Declaration tag :

A JSP declaration lets you declare or define variables and methods (fields) that use into the jsp page.

Declaration tag Start with  <%! And End with %>

The Code placed inside this tag must end with a semicolon (;).
Syntax:    <%! Java Code; %>
EX.

 


You can also put these codes in a single syntax block of declaration like,

 


Note :  The XML authors can use an alternative syntax for JSP declaration tag:
<jsp:declaration>
      Java code;
</jsp:declaration>
EX.

 


You can also put these codes in a single syntax block of declaration like,

?

 


Remember that XML elements, unlike HTML ones, are case sensitive. So be sure to use lowercase.
Declarations do not generate any output so the developer uses declaration tag with JSP expression tag or JSP scriptlet tag for generating an appropriate output for display it in the appropriate browser.

b) JSP scriptlet tag :

A JSP scriptlet lets you declare or define any java code that use into the jsp page.
scriptlet tag Start with  <% and End with %>
The Code placed inside this tag must end with a semicolon (;).
Syntax:    <% Java Code; %>
EX.

 


Note :  The XML authors can use an alternative syntax for JSP scriptlet tag.
<jsp:scriptlet>
     Java code;
</jsp:scriptlet>

c) JSP Expression tag :

A JSP expression is used to insert Java values directly into the output.
JSP Expression tag is use for evaluating any expression and directly displays the output in appropriate web browser.
Expression tag Start with <%= and End with %>
The Code placed inside this tag not end with a semicolon (;).
Syntax:     <%= Java Code %>
For example. the following shows the date/time that the page was requested:
Current time: <%= new java.util.Date() %>
Note :  The XML authors can use an alternative syntax for JSP expressions:
<jsp:expression>
  Java Expression
</jsp:expression>

d) Comment tag :

Although you can always include HTML comments in JSP pages, users can view these if they view the page’s source. If you don’t want users to be able to see your comments, embed them within the <%– … –%> tag.


 


Output :
This is simple demo of test the comment.
Now right click on the browser and select the view source so we can get the code in Notepad like,

 

In the view sorce code, the comment put in <%– comment –%> will not display but the comment put in <!- comment –>  will display.

Now using these JSP Scripting elements, we are developing some simple examples so the use of these tags can easily understood.

?

 


Output :
 
i = 10
for loop execution start………..
j = 1
j = 2
j = 3
j = 4
j = 5
j = 6
j = 7
j = 8
j = 9
j = 10
for loop execution complete………..
The addition of two variable : a + b = 10 + 20 = 30
Current time : Sun Feb 12 14:00:35 IST 2012

 

 

Prev Next