JSP Scripting tags (Scripting elements)
The scripting elements provides the
ability to insert java code inside the jsp. There are three types of scripting
elements:
- scriptlet tag
- expression tag
- declaration tag
JSP scriptlet tag
A scriptlet tag is used to execute java
source code in JSP. Syntax is as follows:
<% java source code %>
Example of JSP scriptlet tag
In this example, we are displaying a
welcome message.
index.jsp
=========
<html>
<body>
<form
action="scriptletTag.jsp">
Please enter your name :
<input type="text" name="uname"> <br />
<input
type="submit" value="submit"><br/>
</form>
</body>
</html>
scriptletTag.jsp
==========
<html>
<body>
<%
String
name=request.getParameter("uname");
out.print("welcome
"+name);
%>
</form>
</body>
</html>
JSP expression tag
The code placed within JSP
expression tag is written to the output stream of the response.
So you need not write out.print() to write data. It is mainly used to print the
values of variable or method.
Syntax of JSP expression tag
<%= statement %>
Example of JSP expression tag
In this example of jsp expression tag, we
are simply displaying a welcome message.
<html>
<body>
<%= "welcome to jsp" %>
</body>
</html>
JSP Declaration Tag
The JSP declaration tag is
used to declare fields and methods.
The code written inside the jsp
declaration tag is placed outside the service() method of auto generated
servlet.
So it doesn't get memory at each request.
Syntax of JSP declaration tag
The syntax of the declaration tag is as
follows:
<%!
field or method declaration
%>
Example of declarative
Tag.
<%!
int cube(int n){
return n*n*n;
}
%>
<%=
"Cube of 3 is:"+cube(3) %>
<%
out.println("<br />");
out.println("Cube of 3
is:"+cube(3));
%>
No comments:
Post a Comment