- Java EE Tutorials
- JSP Tutorials
- Recommended Books for Java Server Pages (JSP)
Java Server Pages are part of a 3-tier architecture. A server(generally referred to as application or web server) supports the Java Server Pages. This server will act as a mediator between the client browser and a database. The following diagram shows the JSP architecture.
Figure 1: JSP Architecture
JSP Architecture Flow
- The user goes to a JSP page and makes the request via internet in user’s web browser.
- The JSP request is sent to the Web Server.
- Web server accepts the requested .jsp file and passes the JSP file to the JSP Servlet Engine.
- If the JSP file has been called the first time then the JSP file is parsed otherwise servlet is instantiated. The next step is to generate a servlet from the JSP file. The generated servlet output is sent via the Internet form web server to users web browser.
- Now in last step, HTML results are displayed on the users web browser.
JSP page in a way is just another way to write a servlet without having to be a Java programming expert. Except for the translation phase, a JSP page is handled exactly like a regular servlet. Now that we saw a big picture in JSP architecture section, lets dive a more deeper and understand how a JSP file is treated in a container and what phases it passes through.
JSP Lifecycle
In this section we will discuss about each phase of a JSP execution cycle. A JSP life cycle is similar to a servlet life cycle with an added step wherein you need to compile a JSP into a servlet. JSP pages are usually managed by a web container which normally contains a servlet container and a JSP container.
Figure 2: JSP Life Cycle
The following table lists each phase of JSP life cycle with a description:
Phase | Description |
---|---|
Translation | JSP container parses the JSP pages. It then translate the JSP pages to generate corresponding servlet source code. If JSP file name is hello.jsp, usually it is named as hello_jsp.java by the container. |
Page Compilation | If the translation is successful, the generated java file is then compiled by the container. |
Class Loading | Once JSP is compiled as servlet class, its lifecycle is similar to servlet. The compiled class is then loaded into the memory. |
Instance Creation | Once JSP class is loaded into memory, its object is instantiated by the container. |
Call jspInit() or Initialization | During this phase the JSP class is initialized transformed from a normal class to servlet. Once initialization is over, ServletConfig and ServletContext objects become accessible to JSP class. Method jspInit() is called only once in JSP lifecycle. It initializes config params. |
Call _jspService or Request Processing | This method is called for each client request. |
Call jspDestroy or Destroy | This is the last phase and this method is called when the container decides to unload JSP from memory. |
Previous Tutorial : Introduction to JSP || Next Tutorial : JSP Example using Eclipse