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.
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
A JSP page can be categorized into:
Part of the JSP which is processed on the server, can be categorized into:
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:
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:
Standard actions are tags that alter the runtime behavior of the JSP and enhance the response sent back to the client.