JavaServer Pages (JSP) is used to develop dynamic web content on the server using HTML, XML templates, and Java code. JSP is extensively used because it is portable, secure, fast, and independent of server platforms. JSPs are HTML pages that get compiled into Java servlets the first time they are invoked.
JSP specification extends the Java Servlet API, utilizes Servlet semantics and has JSP Standard Tag Library (JSTL).
It is difficult to separate and reuse portions of the code in Servlet hence JSP is preferred by developers when a different output format is needed.
Overview
JavaServer Pages allows developer to build applications containing dynamic web content such as HTML, DHTML, XHTML and XML. A JSP file contains static HTML and dynamic actions defining the process to handle a request.
How a JSP file is processed:
Web server after recognizing a .jsp file extension pass the request to the JSP engine. The request is then translated into a Java class, which is then compiled into a servlet.
Translation and compilation occurs only when the JSP file is requested for the first time. Thereafter, the request directly goes to the servlet byte code.
init() method is called to initialize the Servlet when it is first loaded into the virtual machine.
Each request is sent to service() method, which creates a new thread to generate a response.
Request received is converted into a Java object of type HttpServletRequest, which is passed to the Servlet along with an HttpServletResponse object that is used to send the response back to the browser.
Components of JSP
- JSP tags are almost similar to XML tags.
- JSP Tags can be of two types: One will have a start tag with optional attributes, body and a matching end tag while the other will have an empty tag with attributes.
- Attribute values in the tag always appear in "" quotes.
- Any whitespace in the JSP is read and preserved during translation into a servlet.
- \ is used as an escape character in a tag
A JSP page can be categorized into:
- Part that is processed on the server
- Template data
Part of the JSP which is processed on the server, can be categorized into:
- JSP Directives
JSP directives are messages by JSP to JSP container that set global values. They do not produce any output to the client. JSP directives affect a particular JSP file.
JSP Directives are characterized by the @ character within the tag
There are 3 JSP Directives:
- page
- include
- taglib
- Scripting elements
Scripting elements are used to declare variables and methods, evaluate an expression and include scripting code within the JSP.
There are three types of Scripting elements:
- Declaration - is used to define class-wide variables and methods. Declarations are initialized when the JSP page is initialized. Methods and variables defined under Declaration are available throughout JSP page.
- Scriptlets - consists of one or more valid Java statements enclosed between <% and %>. They are executed at request-processing time. What the Scriptlet actually does depends on the code. They can modify objects inside them as a result of method invocations or show output.
- Expressions - is a shorthand notation for a scriptlet enclosed within <%= and %> that outputs a value. When the expression is evaluated, the result is converted to a string and displayed. If any part of expression is an object, the conversion is done using the toString() method of the object.
- Standard actions
Standard actions are tags that alter the runtime behavior of the JSP and enhance the response sent back to the client.
Resource: