In this JSP tutorial, you will learn about JSP Architecture, page-centric approach, dispatcher approach and steps in execution of a JSP file.
JSP is a high-end technology that helps developers insert java code in HTML pages by making use of special JSP tags.
The JSP are HTML pages but do not automatically have .html as file extension. JSP files have .jsp as extension. The following steps takes place in execution of a JSP file.
- JSP files are compiled by JSP engine into a servlet. This step creates the .jsp file as a Java servlet source file.
- Once this is processed, the source file above is compiled into a class file.
- The engine then makes use of the compiled servlet from the above process and executes requests.
Out of the two processes, the first two take time to produce a compiled servlet. This is performed only once unless modification in the source file is required. Once the compiled servlet is completed, the execution of requests is performed at a faster speed.
There are two methods for using JSP technology:
- Page-Centric Approach
- Dispatcher Approach
The page-centric approach is also called Client-Server approach. The basic idea of Client-Server approach is that the application lies on the client side and services the requests connecting to the server application. JSP using this approach-processes as follows:
Client makes a request. The JSP page takes the request from client and processes it. The JSP have access to the database by using Java Beans. The requests are processed and the serviced results are sent back to client by JSP.
The above approach has the advantage of simplifying the process but its disadvantage is when the number of clients increases, the process becomes difficult to handle.
This is also called N-tier approach, where the server side of the above architecture is divided into multiple tiers, using JSP as a controller, passing requests to Java Beans.
JSP is popular because of its processing ability. Processing is distinctly divided between Presentation and Front Components. The popular JSP Architecture is the Model View Controller (MVC) model. In this MVC model, the request is sent by the browser to the controller or the servlet. This request is instantiated by the servlet as a Java Bean by JSP. The main aspect is JSP are compiled into servlets at the back end and the front end tasks are not interrupted. The servlet engine takes up the responsibility of compiling JSP Servlet and producing the final JSP servlet class for usage. The front end presentation modules are handled by JSP for viewing and the manipulation of data is handled by Java Bean and passed back to JSP when needed. The Presentation part of the MVC Model has no processing logic. It performs the task of extracting beans or objects that may have been initially created by the controller. It also extracts the dynamic content within for insertion within its static templates. The Application Manager in the MVC Model is the Controller that processes HTTP requests. They are not responsible for presentation tasks. That can be either servlets or JSP. They take the task of managing the application state, security, and presentation uniformity and thus, have a single point of entry.
This explains the approach and the process of execution of a request.
The following steps execute a JSP request from the time the request is received from client until it is processed and sent back to client.
Request from Client:
A JSP page has the extension as .jsp. The client request is made on the web browser by going to the .jsp extension JSP file.
Request Sent To Server:
The request received from client (web browser) is sent to the server side.
JSP Servlet Engine:
Since the extension of request made has .jsp extension, it indicates that it is a JSP file and therefore the web server passes the request received to the JSP Servlet Engine.
Process of generating servlet:
JSP files are compiled by the JSP engine into a servlet. This step creates the .jsp file as a Java servlet source file.
Process of making class file:
The source file from Step4 is compiled into a class file.
Instantiation of Servlet:
The instantiation of servlet is performed using the init and service methods. In these methods, the jspInit() method is defined by the developer and the jspService method is generated by the JSP engine.
The request received from client is executed and the output is sent as HTML.
Client Receives the Output:
The Client Receives the Output and thus the result namely the HTML gets displays in the client browser.