JSP (Java Server Pages) basically separates the work of web designers and developers i.e. it separates ‘User Interfaces’ from ‘Content Generation’.
In this post, I am going to cover the basics of JSP, Hello World code sample, JSP Life Cycle, Components and exception/error handling.
What is a JSP
A JSP is simply an HTML page containing additional code to execute application logic which generates dynamic content. A JSP may contain the code for: JDBC, EJB, RMI etc.
How JSP Works
- The source code (JSP files) is copied to document directory of the web server. When a request is made for this page, the server recognizes the *.jsp extension and realizes that special handling is required.
- First time the file is requested, it is compiled into a servlet object and stored in memory and the output is sent back to the requesting client.
- After the first request, the server checks to see whether *.jsp file has changed. If not changed, server invokes the previously compiled servlet object.
JSP Life Cycle
To understand the JSP life cycle, let’s have a look at the below diagram which depicts the story of a JSP:
JSP Page Translation
First of all JSP page gets translated or parsed into a servlet class file. So putting simply, JSP turns into a servlet. Note that if there would be errors, you can get page translation errors too. To know more about JSP errors read here.
JSP Page Compilation and Loading
So now the parsed JSP file gets compiled and turns into a .class file and get loaded.
Instance Creation and Initialization
This phase instantiates the Servlet instance by calling jspInit() method.
This is the phase where JSP spends most of its life, i.e. serving the requests. When the request comes from any of the clients, _jspService() method gets called to get the response.
This is where the JSP gets ready for garbage collection. jspDestroy() method gets called in this phase and if needed all the DB connection, n/w connections get closed here.
We need to note that the generated servlet class from JSP, implements HttpJspPage interface. I would add more details around this and extend this post with that later.
There are three components of JSP which are required to understand to work with JSP:
C) Implicit Objects
We will cover all of these gradually starting Directives. So in my next post I would detail out JSP Directives. Till then happy learning JSP and share what you need to know more about the basics of JSP.