JSP Tags and Action
JSP Tags and Action
variables in the form of declaration tag i.e., declaration tag is used for declaring the variables
in JSP page.
Syntax:
<%! Variable declaration or method definition %>
Example
<%!
int a = 10, b = 30;
int count() {
return (a + b);
}
%>
Expression tag: Expression tags are used for writing the java valid expressions as a part of
JSP page. A JSP expression element contains a scripting language expression that is
evaluated, converted to a String, and inserted where the expression appears in the JSP file.
Syntax:
Scriplet tag: Scriplets are basically used to write a pure java code. Whatever the java code
we write as a part of scriplet, that code will be available as a part of service () method of
servlet.
Syntax:
<html>
<title>Print Numbers 1-10</title>
<head>Numbers 1-10</head><br>
<body>
<%
for (int i = 1; i <= 10; i++) {
out.println (i);
}
%>
</body>
</html>
Directives:
Directives are basically used to configure the code that is generated by container in a servlet.
As a part of JSP we have three types of directives; they are page directives, include directives
and taglib directives.
<%@
page attribute_name1=attribute_value1,
attribute_name2=attribute_value2,
....................................,
Attribute_nameN=attribute_valueN
%>
import This attribute is used for importing either pre-defined or user- defined
packages. The default value is java.lang.*
For example:
contentType . This attribute is used for setting the MIME type (plain/text, img/jpeg,
img/gif, audio/wave, etc.). The default value is text/html.
For example:
language This attribute represents by default java i.e., in order to represent any
business logic a JSP program is making use of java language. The attribute
language can support any of the other programming languages for
developing a business logic at server side.
For example:
Syntax:
Standard Actions:
These are basically used to pass runtime information to the container. As a part of JSP we
have the following standard actions; they are <JSP:forward/>, <JSP:include/>, <JSP:param/>,
<JSP:useBean/>, <JSP:setProperty/> and <JSP:getProperty>.
1. <JSP:forward/>:
When we want to forward a request and response to the destination JSP page from the
source JSP we must use <JSP:forward>.
Syntax:
Without body:
With body:
For example:
<JSP:forward page="y.jsp">
<JSP:param name="v1" value="10"/>
<JSP:param name="v2" value="20"/>
</JSP:forward>
When we use this tag we get the response of destination JSP page only but not source
JSP page.
2. <JSP:include/>:
This tag is used for processing a client request by a source JSP page by including
other JSP pages and static resources like html's. One source JSP can include 'n'
number of server side resources and finally we get the response of source JSP only.
Syntax:
Without body:
<JSP:include page="y.jsp">
<JSP:param name="v1" value="10"/>
<JSP:param name="v2" value="20"/>
</JSP:include>