JSP Scripting Element

JSP Scripting Element

The scripting elements of JSP are written inside <% %>. Through a scripting element we can insert a java code into servlet. The content written inside <% %> in JSP is called scriptlet and a scriptlet can have java statements, function declaration, variables or expressions. Thy syntax of a scriptlet is as follows:

SYNTAX:

<% code %>

When the JSP page is translated the code inside <% %> is processed by the engine of JSP. The code in the JSP page that is not written inside this tag is marked as an HTML code or a plain text. Consider the following example in which we have inserted a scriptlet tag:

CODE:

< html >

< head >

< title > JSP Page < /title >

< /head >

< %

       int n = 1;

   % >

< body >

        Value of n:

< % out. println (++n); % >

< /body >

< /html >

In the above example if we remove the scriptlet tag and then if we run it as a JSP page then all the things will appear as it is on the browser. This is because we removed the scriptlet tag and the content will be considered as an HTML code.

 

Types of Scripting Elements:

Consider the following table in which the scriptlet elements with an example are shown:

Scripting Element Example
Comment <% –comment– %>
Declaration <%! declaration %>
Directive <%@ directive %>
Scriptlet <% scriplets %>
Expression <% = expression %>

 

JSP Comments:

Comments in JSP are the statements that are ignored by the web container of JSP. The JSP comments are used when the user wants to hide a part of the JSP page or when the user wants the part of JSP page to be ignored by the JSP container. Comments in a JSP page are inserted to add more information about the code that what the actual functionality of the code is. Comments save the time of user.

The user simply reads the comments and gets the idea of code and he does not need to go through the code thoroughly. During the translation of the JSP page the comments are not inserted into the servlet code and they are also not inserted into HTTP, the JSP comments are only in the JSP page.

The following is the syntax of a JSP comment:

SYNTAX:

<% –comment– %>

EXAMPLE:

Consider the following example in which we have inserted a JSP comment in the JSP page.

CODE:

< html >

< head >

< title > JSP Page < /title >

< /head >

< %

       int n = 1;

   % >

< body >

<% Printing the value of n %>

        Value of n:

< % out. println (++n); % >

< /body >

< /html >

In the above example the line <% Printing the value of n %> is a comment and the JSP container will ignore it while translation to servlet.