Java2all.com  




Share This Topic -

1. JSP Scripting elements: 

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.
 <%! private int i = 10; %>
       <%! private int squre(int i) 
       {
              i = i * i ;
              return i;
       }	
 %> 

 
You can also put these codes in a single syntax block of declaration like,
 <%! private int i = 10;
        private int squre(int i)
	{
         	i = i * i ;
        	return i;
        }	
%> 

 
Note :  The XML authors can use an alternative syntax for JSP declaration tag: 
 
<jsp:declaration> 
      Java code;
</jsp:declaration>
 
EX.
 <jsp:declaration> private int i = 1; </jsp:declaration>
  <jsp:declaration>
    {
	private int squre(int i)
	i = i * i ;
	return i;
    }	
</jsp:declaration> 


You can also put these codes in a single syntax block of declaration like,
 <jsp:declaration> 
      private int i = 1; 
	private int squre(int i)
	{
		i = i * i ;
	      return i;
	}	
</jsp:declaration> 

 
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.
<% 
   int a = 10;
   out.print("a ="+a);
%>

 
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.

 <html>
  <head>
	    <title>Comment demo</title>
  </head>
  <body>
  	<p>This is simple demo of test the comment </p>
    	<!-- This is a comment. Comments are not displayed in the browser but               displayed in source code -->
<%-- This is a comment. Comments are not displayed in the browser as well as not displayed in source code also --%>
  </body>
</html> 

 
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,
 <html>
   <head>
      <title>Comment demo page</title>
   </head>
   <body>
  	<p>This is simple demo of test the comment. </p>
    	<!-- This is a comment. Comments are not displayed in the browser but displayed in source code-->
   </body>
 </html> 

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.
 <html>
  <head>
	    <title>Scripting Demo page</title>
  </head>
  <body>
  	<%-- declaration tag --%>
    <%! int i = 10; %>
    
    <%-- scriptlet tag --%>
    <% out.print("i = "+i); %>
    <br>
    <br>
    <% out.print("for loop execution start..........."); %>
    <br>
    <% 
    for(int j=1;j<=10;j++)
    {
    	out.print("j = "+j);
    %>
    <br>
    <% 
    }
    out.print("for loop execution complete...........");
    %>
    <br>
    <br>
    
    <%-- expression tag --%>
    <%! int a = 10;
    	int b = 20; 
    %>
    The addition of two variable : a + b = 10 + 20 = <%= a+b %>
    <br>
    <br>
    Current time : <%= new java.util.Date() %>
  </body>
</html> 

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