Monday, June 18, 2012

JSP'S Scripting Elements


JSP'S Scripting Elements

JSP'S Scripting Elements


There are 3 types of Scripting elements in JSP

1. Scriptlets
2. Expressions
3. Declarations

1.Scriptlets (<%?any java Statements?.%>) : -

Inside a scriptlet, we can write any valid java statements and all the statements must be terminated with " ;"
All the java statements inside the scriptlet will be placed inside _jspService().

Example: -

<%
out.println("JAVAYOM");
int x=10,y=20;
%>
Expressions (<%=any expression%>): - 

Expression is a shortcut form of out.println( ). All the expressions will be placed inside _jspService( ).
Don't use the semicolon at the end of the expression.
Don't include the expressions inside the scriptlet.

Example: -

<%= " JAVAYOM " %>

Declarations (<%!any declarations%>): -

Declarations are used to declare variables and methods.
Contents of declaration section will be placed outside _jspService( ) and inside servlet class.

Example: -

<%!
int a=99;
void m1( )
{
}
%>
Note: Don't write these 3 elements one in another, which gives compilation error.

Example: -

Hai.jsp

<html>
<body>
<%=" JAVAYOM "%>
<%
out.println("JAVAYOM ");
int x=10,y=20;
%>
<%!
int a=99;
void m1()
{
}
%>

This JSP when converted into java class it is as follows: -

class hai_jsp extends HttpJspBase
{
int a=99;
void m1()
{
}
_jspService(-,-)
{
out.write("<html>/r/n<body>");
out.println("javayom");
out.print("javayom");
int x=20,y=20;
out.write("</body>/r/n</html>");
}
}
But user can override init () and destroy () methods inside declarations.

No comments:

Post a Comment