SCRIPTING ELEMENTS
Scripting is a JSP mechanism for directly embedding Java code fragments into an HTML page.
Three scripting language components are involved in JSP scripting. Each component has its appropriate location in the generated servlet.
Declarations
JSP declarations are used to define Java variables and methods in a JSP. A JSP declaration must be a complete declarative statement. JSP declarations are initialized when the JSP page is first loaded. After the declarations have been initialized, they are available to other declarations, expressions, and scriptlets within the same JSP.
Three scripting language components are involved in JSP scripting. Each component has its appropriate location in the generated servlet.
Declarations
JSP declarations are used to define Java variables and methods in a JSP. A JSP declaration must be a complete declarative statement. JSP declarations are initialized when the JSP page is first loaded. After the declarations have been initialized, they are available to other declarations, expressions, and scriptlets within the same JSP.
The syntax for a JSP declaration is as follows:
<%! declaration %>
A sample variable declaration using this syntax is shown here:
<%! String name = new String("BOB"); %>
A sample method declaration using the same syntax is as follows:
<%! public String getName() { return name; } %>
To get a better understanding of declarations, let’s take the previous string declaration and embed it into a JSP document. The sample document would look similar to the following code snippet:
<HTML>
<BODY>
<%! String name = new String("BOB"); %>
</BODY>
</HTML>
When this document is initially loaded, the JSP code is converted to servlet code and the name declaration is placed in the declaration section of the generated servlet. It is now available to all other JSP components in the JSP. Note It should be noted that all JSP declarations are defined at the class level, in the servlet generated from the JSP, and will therefore be evaluated prior to all JSP expressions and scriptlet code.
Expressions
JSP expressions are JSP components whose text, upon evaluation by the container, is replaced with the resulting value of the container evaluation. JSP expressions are evaluated at request time, and the result is inserted at the expression’s referenced position in the JSP file. If the resulting expression cannot be converted to a string, then a translation-time error will occur. If the onversion to a string cannot be detected during translation, a ClassCastException will be thrown at request time.
The syntax of a JSP expression is as follows:
<%= expression %>
A code snippet containing a JSP expression is shown here:
Hello <B><%= getName() %></B>
Here is a sample JSP document containing a JSP expression:
<HTML>
<BODY>
<%! public String getName() { return "Bob"; } %>
Hello <B><%= getName() %></B>
</BODY>
</HTML>
Scriptlets
Scriptlets are the JSP components that bring all the JSP elements together. They can contain almost any coding statements that are valid for the language referenced in the language directive.
They are executed at request time, and they can make use of all the JSP components. The syntax for a scriptlet is as follows:
<% scriptlet source %>
When JSP scriptlet code is converted into servlet code, it is placed into the generated servlet’s service() method. The following code snippet contains a simple JSP that uses a scripting element to print the text “Hello Bob” to the requesting client:
<HTML>
<BODY>
<% out.println("Hello Bob"); %>
</BODY>
</HTML>
We should note that while JSP scriplet code can be very powerful, composing all our JSP logic using scriptlet code can make our application difficult to manage. This problem led to the creation of custom tag libraries.
<%! declaration %>
A sample variable declaration using this syntax is shown here:
<%! String name = new String("BOB"); %>
A sample method declaration using the same syntax is as follows:
<%! public String getName() { return name; } %>
To get a better understanding of declarations, let’s take the previous string declaration and embed it into a JSP document. The sample document would look similar to the following code snippet:
<HTML>
<BODY>
<%! String name = new String("BOB"); %>
</BODY>
</HTML>
When this document is initially loaded, the JSP code is converted to servlet code and the name declaration is placed in the declaration section of the generated servlet. It is now available to all other JSP components in the JSP. Note It should be noted that all JSP declarations are defined at the class level, in the servlet generated from the JSP, and will therefore be evaluated prior to all JSP expressions and scriptlet code.
Expressions
JSP expressions are JSP components whose text, upon evaluation by the container, is replaced with the resulting value of the container evaluation. JSP expressions are evaluated at request time, and the result is inserted at the expression’s referenced position in the JSP file. If the resulting expression cannot be converted to a string, then a translation-time error will occur. If the onversion to a string cannot be detected during translation, a ClassCastException will be thrown at request time.
The syntax of a JSP expression is as follows:
<%= expression %>
A code snippet containing a JSP expression is shown here:
Hello <B><%= getName() %></B>
Here is a sample JSP document containing a JSP expression:
<HTML>
<BODY>
<%! public String getName() { return "Bob"; } %>
Hello <B><%= getName() %></B>
</BODY>
</HTML>
Scriptlets
Scriptlets are the JSP components that bring all the JSP elements together. They can contain almost any coding statements that are valid for the language referenced in the language directive.
They are executed at request time, and they can make use of all the JSP components. The syntax for a scriptlet is as follows:
<% scriptlet source %>
When JSP scriptlet code is converted into servlet code, it is placed into the generated servlet’s service() method. The following code snippet contains a simple JSP that uses a scripting element to print the text “Hello Bob” to the requesting client:
<HTML>
<BODY>
<% out.println("Hello Bob"); %>
</BODY>
</HTML>
We should note that while JSP scriplet code can be very powerful, composing all our JSP logic using scriptlet code can make our application difficult to manage. This problem led to the creation of custom tag libraries.
No comments:
Post a Comment