Engineering Full Stack Apps with Java and JavaScript
With the scripting support you can write your own java logic in a JSP page.
There are mainly two forms of scripting in a JSP
Newer Expression Language (EL)
Traditional Scripting
Traditional language-based scripting elements in a JSP are further divided into:
Expressions
Expressions use the result of evaluating a piece of Java code directly in the page output.
An expression must begin with <%= and conclude with %>.
The evaluated value is passed as the parameter to the print method of a JspWriter, and hence we should not end the statement with a semicolon.
We can also use a function, but it should return something (and no semicolon).
if we provide a function that return void inside an expression, JSP page will fail the translation stage. The expression result will be used as parameter code inside a method call, which will not accept a void.
Example: <%= new Date() %>
When this is generated into servlet code, the resulting Java statement will probably look like this: out.print(new java.util.Date());
Scriptlets
Scriptlets allow you to include different sets of Java statements inside the _jspService() method, and are executed on every request to the page.
Here, the Java statements are incorporated “as is,” so you must terminate each with a semicolon.
A scriptlet begins with a <% and ends with a %>.
We can mix both template text and scriptlets. All code within scriptlets will go directly to the _jspService() method and the template text (such as <tr><td><font color="red">) is incorporated as out.write() statements in the _jspService() method, and expressions (such as <%= planets[i] %>) as out.print() statements.
The template text, expressions, and scriptlets are all translated and generated into _jspService() in order of their appearance in the JSP page source.
You can declare a local variable in one scriptlet and use that in another scriptlet or expression, as long as both are in same page. This is because both of them will be added in order to the _jspService() method.
Example: <% System.out.println("Normal Java Code"); %>
Declarations
If you want to place code in the generated servlet outside of the _jspService() method (e.g. declaring another method), you can use declarations.
It begins with <%! and end with %>.
You can place in your declaration any code that can legally appear in a servlet source file: instance methods, static methods, static initialization code, static or instance data members, inner classes.
Example:
<%! public void jspInit() {
System.out.println("TRIAL INITIALIZED");
} %>
Comments (starts with <%-- and ends with %>
Whatever you give inside the comment element are ignored by the compiler.
Starts with <%-- and end with --%>
If you want comments sent within the web page output, you can use regular HTML comment syntax: Open the comment with <!-- and close with -->.
An HTML comment is treated exactly like other HTML template text. So it’s perfectly acceptable to include JSP scriptlets, expressions, expression language, or any other legal JavaServer Page syntax within the comment—and it will be processed at translation time. However you cannot have scriptlet or any other traditional scripting element inside a JSP comment.
Expression language (EL) allows us to write scriptless JSP components by replacing the JSP expression scripting element.
EL expressions are represented in code as ${expression}.
<html>
<head></head>
<body>
$ { body_content }
</body>
</html>
EL only replaces expressions and we can also replace other traditional scripting elements with tag libraries, servlets or helper classes, allowing us to better divide tasks between java developers and html.xml developers.
EL can also be used along with standard actions and custom tags.
We will see expression language (EL) in detail later.