Engineering Full Stack Apps with Java and JavaScript
A JSP page can be said to be divided into two types of contents, which are template text and JSP elements. Template text is any non-java lines like html, comments, javascript, css etc. that are directly sent to the client without any processing. Java code is embedded into JSP pages using scripting elements and these elements will be processed and not directly rendered to the output like template text.
JSP elements can be mainly divided into 3 categories:
Directive element
Most often use these to communicate global information to your page.
Scripting elements
To incorporate dynamic information or execute presentation logic.
Actions
These use XML-style tags for the inclusion of dynamic data.
Directives are elements that provide the JSP translator with directions on how it should treat the JSP page being translated.
Our simplejsp from previous demo had the below page directive in it:
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
A directive starts with <%@ followed by the directive name (e.g. page), some attributes with values (e.g. language="java") and ends with %>
Important directives are page, include and taglib directives.
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 (starts with <%= and ends with %>)
Scriptlets (starts with <% and ends with %>
Declarations (starts with <%! and ends with %>
Comments (starts with <%-- and ends with %>
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}.
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.
Actions are XML elements that encapsulate functionality on a JSP page. Programmers can create them and they can be used by the nonprogrammer to introduce dynamic behavior to a JSP page without having any knowledge of Java programming language. For those, non-programmers these will like a new html or xml tag element.
There are two forms of action elements:
standard
custom.
Standard actions are used for the same purpose as Java language-based scripting. Most things that we can achieve with scripting elements are achievable with other standard actions also. Difference between standard and custom actions are that you can rely on any J2EE-compliant JSP container to provide support for all the standard actions defined in the JSP spec. Custom actions will have to be imported manually.