• Declarations
• Scriptlets
• Expressions
• Comments
• Directives
JSP Declaration
• A declaration tag is a piece of Java code for declaring variables, methods and classes. If we declare a variable or method inside declaration tag it means that the declaration is made inside the servlet class but outside the service method.
• We can declare a static member, an instance variable (can declare a number or string) and methods inside the declaration tag.
Syntax of declaration tag:
<%! Dec var %>
Example
<%! int count =10; %>
<% out.println("The Number is " +count); %>
JSP Scriptlet
• Scriptlet tag allows to write Java code into JSP file.
• JSP container moves statements in _jspservice() method while generating servlet from jsp.
• For each request of the client, service method of the JSP gets invoked hence the code inside the Scriptlet executes for every request.
• A Scriptlet contains java code that is executed every time JSP is invoked.
Syntax of Scriptlet tag:
<% java code %>
Example
<body>
<%
int num1=10;
int num2=40;
int num3 = num1+num2;
out.println("Scriplet Number is " +num3);
%>
</body>
JSP Expression
• Expression tag evaluates the expression placed in it.
• It accesses the data stored in stored application.
• It allows create expressions like arithmetic and logical.
• It produces scriptless JSP page.
Syntax:
<%= expression %>
Example
<% out.println("The expression number is "); %>
<% int num1=10; int num2=10; int num3 = 20; %>
<%= num1*num2+num3 %>
JSP Comments
• Comments are the one when JSP container wants to ignore certain texts and statements.
• When we want to hide certain content, then we can add that to the comments section.
Syntax:
<% -- JSP Comments %>
Example
<%-- Comments section --%>
<% out.println("This is comments example"); %>
JSP directives
JSP directives are the messages to JSP container. They provide global information about an entire JSP page.
• JSP directives are used to give special instruction to a container for translation of JSP to servlet code. • Directives can have many attributes by comma separated as key-value pairs.
• In JSP, directive is described in <%@ %> tags.
There are three types of directives:
1.Page directive
Defines page-dependent attributes, such as scripting language, error page, and buffering requirements. <%@ page attribute = "value" %>
2. Include directive
Includes a file during the translation phase. <%@ include file = "relative url" >
3. Taglib directive
Declares a tag library, containing custom actions, used in the page
<%@ taglib uri="uri" prefix = "prefixOfTag" >
It provides attributes that get applied to entire JSP page.
• It defines page dependent attributes, such as scripting language, error page, and buffering requirements.
• It is used to provide instructions to a container that pertains to current JSP page.
Following are its list of attributes associated with page directive:
Language
Extends
Import
contentType
info
session
isThreadSafe
autoflush
buffer
IsErrorPage
pageEncoding
errorPage
isELIgonored