JSP Scripting Elements

JSP Scripting Element


  • JSP Scripting element are written inside <% %> tags. 
  • These code inside <% %> tags are processed by the JSP engine during translation of the JSP page. 
  • Any other text in the JSP page is considered as HTML code or plain text.


Example:
in head tag
    <%
       int count = 0;
    %>
in body tag
        Page Count is <% out.println(++count); %>


  • There are five different types of scripting elements
    • Scripting Element Example
    • Comment <%-- comment --%>
    • Directive <%@ directive %>
    • Declaration <%! declarations %>
    • Scriptlet <% scriplets %>
    • Expression <%= expression %>

JSP Comment

  • JSP Comment is used when you are creating a JSP page and want to put in comments about what you are doing. 
  • JSP comments are only seen in the JSP page. 
  • These comments are not included in servlet source code during translation phase, nor they appear in the HTTP response. 
  • Syntax of JSP comment is as follows :

<%-- JSP  comment  --%>


  • Example:

        <%-- Code to show page count  --%>

JSP Scriptlet Tag

  • Scriptlet Tag allows you to write java code inside JSP page. 
  • Scriptlet tag implements the _jspService method functionality by writing script/java code. 
  • Syntax of Scriptlet Tag is as follows :

<% JAVA CODE %>

  • Example:

    <%
        int count = 0;
    %>
        Page Count is <% out.println(++count); %>

Example-02:
index.html



Mixing scriptlet Tag and HTML
Example:

JSP Declaration Tag

  • We know that at the end a JSP page is translated into Servlet class. 
  • So when we declare a variable or method in JSP inside Declaration Tag, it means the declaration is made inside the Servlet class but outside the service(or any other) method. 
  • You can declare static member, instance variable and methods inside Declaration Tag. 
  • Syntax of Declaration Tag :


<%! declaration %>

Example:
    <%! int count = 0;   %>

When to use Declaration tag and not scriptlet tag?

  • If you want to include any method in your JSP file, then you must use the declaration tag, because during translation phase of JSP, methods and variables inside the declaration tag, becomes instance methods and instance variables and are also assigned default values.


Example:
in head tag
    <%!
       int count = 0;
       int getCount() {
           System.out.println( "In getCount() method" );
           return count;
       }
    %>

JSP Directive Tag

  • Directive Tag gives special instruction to Web Container at the time of page translation. 
  • Directive tags are of three types: page, include and taglib.
    • 1. <%@ page ... %> - defines page dependent properties such as language, session, errorPage etc.
    • 2. <%@ include ... %> - defines file to be included.
    • 3. <%@ taglib ... %> - declares tag library used in the page

JSP Expression Tag

  • Expression Tag is used to print out java language expression that is put between the tags. An expression tag can hold any java language expression that can be used as an argument to the out.print() method. 
  • Syntax of Expression Tag


<%= Java Expression %>

Example:
When the Container sees this

<%= (2*5) %>
It turns it into this:

out.print((2*5));

Note: Never end an expression with semicolon inside Expression Tag. Like this:

<%= (2*5); %>

Example of Expression Tag
in head tag
    <%
       int count = 0;
    %>
in body tag
        Page Count is <%= ++count %> 











Thanks a lot for query or your valuable suggestions related to the topic.

Previous Post Next Post

Contact Form