| Java Server Pages Overview |
| |
| Scripting Elements |
| |
| The scripting elements in a JSP page comprise of four types : |
1. Comments
2. Class level declarations
3. Expression
4. Scriptlets |
| |
| |
| Comments |
| Like other Java classes we can add comments in a JSP page. Comments are useful as it makes your code more understandable. |
| |
The syntax of JSP comment is as follows: |
| <%-- This is a JSP Comment --%> |
| |
| |
| Class Level Declarations |
| This feature allows us to add our own class level methods and variables which can be accessed by the scriptlets. |
| |
| It's syntax is as follows : |
| <%! Declaration Statement %> |
| |
| For example : |
| <%! String author = "Aman Kumar"; %> |
| Above code declares a class level variable named author with a value of "Aman Kumar". |
| |
| |
| Expression |
| It is a short hand for writing output directly to the output stream. |
| |
| Its syntax is as follows: |
| <%= statement %> |
| The value of the Java statement is displayed to the user. e.g, |
| <%= new java.util.Date().toString() %> |
| Above code displays the current date and time to the user. |
| |
| |
| Scriptlets |
| Within scriptlets we can add whatever Java code we like. All this code will go inside the _jspService() method, so we cannot declare class level methods or variables within scriptlets |
| |
| It's syntax is as follows : |
| <% statements %> |
| For example: |
<%
String name = "Aman Kumar";
out.println("My name is " + name);
%> |
| Above code first declares a name variable with value of "Aman Kumar" and then displays it to the user screen. |
| |
| |
|
| |
| |