Java Server Pages is a technology that helps software developers to create platform independent dynamic web applications rapidly based on HTML or XML document types.
- JSP Directives
- Standard actions
- Built-in objects
- Java Standard Tag Library (JSTL)
- Expression Language (EL)
- Custom Tags
What is JSP? When we already have Servlets, then what is the need of JSP?
Java Server Pages is a technology that helps software developers to create platform independent dynamic web applications rapidly based on HTML or XML document types.
JSP technology separates the user interface from content generation, which enables the designers to change the overall page layout without altering the underlying dynamic content.
When we already have Servlets for the web development using Java, then why do we need JSP technology?
Limitations of Servlets :
- Web application development using Servlets requires strong knowledge of Java.
- Writing the HTML code within the Servlet page will be very difficult.
- Servlet is a mixture of both presentation logic written using HTML and business logic written using Java.
- Whenever we make any changes or modifications to the Servlet, it has to be recompiled and then redeployed to see the changes.
Advantage that developers will get using JSP :
- To develop a JSP program, it is not mandatory that the developer should have a strong programming knowledge.
- Writing the HTML code in JSP program is easy and it is similar to HTML.
- Modifications done to the JSP is recognized automatically and the changes will be effected immediate.
- JSP programs are by default thread safe.
- JSP programs can use standard tags, we can create our own custom tags using tag extension API. As we have reusable tag libraries, the amount of code needed to write powerful web applications will greatly reduces.
- It is easy to learn, implement, and to maintain JSP pages.
JSP Life-cycle
Now let us understand the life-cycle of JSP.

Whenever a client sends a request to the JSP file to the server :
- then the web container translates the JSP page into Java source code for creating Servlet class. If any JSP syntax errors are present, then they are caught in this phase, which is called as translation phase.
- the container tries to compile the Servlet Java source into class file. Java language or syntax errors are caught at this phase and this phase is called as compilation phase.
- Once the Java source file is complied to class file, then the container loads the newly generated Servlet class and then instantiates the Servlet and calls the Servlet’s jspInit() method, and
- then the container creates a new thread to handle the client’s request and the Servlet’s jspService() method runs and eventually the Servlet sends a response back to the client or forwards the request to another web app component.
When we deploy a web app with the JSP, the whole translation and compilation phase happens only when the JSP pages requested for the first time or the JSP page has been modified.
Once the JSP page has been translated and compiled, it is just like any other Servlet.
Once the Servlet has been loaded and initialized, the only thing that happens at request time is creation or allocation of a thread for the service method. And finally, whenever the container removes the Servlet instance from service, it calls the JSP destroy method to perform any required cleanup.
JSP Lifecycle is depicted in the below diagram.
Following steps explain the JSP life cycle:
- Translation of JSP page
- Compilation of JSP page(Compilation of JSP page into _jsp.class)
- Classloading (_jsp.java is converted to class file _jsp.class)
- Instantiation(Object of generated servlet is created)
- Initialisation(_jspinit() method is invoked by container)
- Request Processing(_jspservice() method is invoked by the container)
- Destroy (_jspDestroy() method invoked by the container)
Let us have more detailed summary on the above points:
- Translation of the JSP Page:
A Java servlet file is generated from a JSP source file. This is the first step of JSP life cycle. In translation phase, container validates the syntactic correctness of JSP page and tag files.
- The JSP container interprets the standard directives and actions, and the custom actions referencing tag libraries (they are all part of JSP page and will be discussed in the later section) used in this JSP page.
- In the above pictorial description, this demo.jsp is converted to the Servlet (demo_jsp.class ) wherein the JSP engine loads the JSP Page and converts to Servlet content.
- When the conversion happens all template text is converted to println() statements and all JSP elements are converted to Java code.
- Compilation of the JSP Page
- The generated java servlet file is compiled into java servlet class
- The translation of java source page to its implementation class can happen at any time between the deployment of JSP page into the container and processing of the JSP page.
- In the above pictorial description demo_jsp.java is compiled to a class file demo_jsp.class
- Classloading
- Servlet class that has been loaded from JSP source is now loaded into the container
- Instantiation
- In this step the object i.e. the instance of the class is generated.
- The container manages one or more instances of this class in the response to requests and other events. Typically, a JSP container is built using a servlet container. A JSP container is an extension of servlet container as both the container support JSP and servlet.
- A JSPPage interface which is provided by container provides init() and destroy () methods.
- There is an interface HttpJSPPage which serves HTTP requests, and it also contains the service method.
- Initialization
public void jspInit() { //initializing the code }
- _jspinit() method will initiate the servlet instance which was generated from JSP and will be invoked by the container in this phase.
- Once the instance gets created, init method will be invoked immediately after that
- It is only called once during a JSP life cycle, the method for initialization is declared as shown above
- Request processing
void _jspservice(HttpServletRequest request HttpServletResponse response) { //handling all request and responses }
- _jspservice() method is invoked by the container for all the requests raised by the JSP page during its life cycle
- For this phase, it has to go through all the above phases and then only service method can be invoked.
- It passes request and response objects
- This method cannot be overridden
- The method is shown above: It is responsible for generating of all HTTP methods i.eGET, POST, etc.
- Destroy
public void _jspdestroy() { //all clean up code }
- _jspdestroy() method is also invoked by the container
- This method is called when container decides it no longer needs the servlet instance to service requests.
- When the call to destroy method is made then, the servlet is ready for a garbage collection
- This is the end of the life cycle.
- We can override jspdestroy() method when we perform any cleanup such as releasing database connections or closing open files.
Now let us understand in detail about JSP life-cycle methods :

- The generated Servlet class for JSP file will be implementing HttpJspPage interface, which provides the _jspService() method and this method corresponds to the body of the JSP page, and this method is defined automatically by the JSP container and as a developer we should never provide the definition for the _jspService method explicitly.
HttpJspPage interface extends JspPage and the JspPage interface describes the generic interaction that a JspPage implementation class must satisfy, and this interface provides two methods, jspInit and jspDestroy.
- _jspInit() method is invoked when the JspPages initialize and the _jspDestroy() method is invoked when the JspPages about to be destroyed.
JspPage interface extends Javax.Servlet.servlet.
jspInit() and jspDestroy() methods can be overridden as per the requirement.
https://www.guru99.com/jsp-life-cycle.html
Understanding JSP Development Models
Whenever we develop an application, we need to first understand the best practices available for developing the application.
JSP supports two types of development models for developing the application, Model 1 Architecture, Model 2 Architecture.
Model 1 Architecture :

In Model 1, a request is made to a JSP page and then that JSP page handles all the responsibilities for the request :
- including processing the request,
- validating data,
- handling the business logic, and
- generating response.
The advantage of using Model 1 Architecture, it is very easy to develop web applications and also developing the applications will be faster, and hence Model 1 Architecture is commonly used in smaller and simple task applications.
Model 1 Architecture has many disadvantages. This architecture is not suitable for large scale application development because a great deal of functionality is duplicated in each JSP, and also the Model 1 Architecture unnecessarily ties together the business logic and presentation logic of the application.
Combining business logic with presentation logic makes is hard to introduce a new view or access point in an application. There won’t be no clear separation of logics as the JSP contains all mixed code. Modifications to one logic may affect the other logic.
Model 2 Development Architecture : MVC Architecture
Model 2 Architecture follows MVC architecture where M stands for model and V stands for view and C stands for controller.
Model hosts the real business logic and the state. In other words, it knows the rules for retrieving and updating the data. It is the only part of the application that talks to the database.
Views are responsible for the presentation. It gets the state of the model from the controller, although not directly, but the controller puts the model data in a place where the view can find it.
Controllers responsibility is to accept the user input from the request and to figure out what this means to the model. Controller tells the model to update itself and also makes the model state available for the view.
Now let us understand Model 2 MVC 1 Architecture :

In this model, JSP acts like the view and the controller, that is, if a request is made by the client it will be sent to the JSP and then the JSP handles the responsibility such as gathering the data, validating data, session management logic, presentation logic, etc.
And a separate component as a model layer which handles the business logic, persistence logic, middle-ware services, etc will be used.
This model is, again, suitable for developing simple web applications only.
Following MVC 1 Architecture, Apache’s software organization developed Jarkarta’s Project Struts framework.
Now let us understand Model 2 MVC 2 architecture in detail :

The model portion of an MVC based application typically uses Java Bean classes to define the internal state of the system. They also specify the actions that can be taken to change that state.
The view portion is constructed using JSP technology. JSP pages can contain static HTML or XML text, which is also called as template text, and also the view has the ability to insert dynamic content based on the interpretation at the page request time of special action packs. The JSP environment also includes a set of custom JSP tag libraries, standard JSP action tags, and a facility to install our own JSP custom tag libraries.
The controller portion of the application is focused on receiving requests from the client, deciding what business logic function is to be performed, and delegating responsibility for producing the next phase of the user interface to an appropriate view component is handled by the controller.
- Whenever the client sends a request, it is first intercepted by a Servlet, commonly referred as a Controller Servlet. This Servlet handles the initial processing of the request and determines which JSP page to display next.
- Client can never send a request directly to JSP page in the MVC Model 2 Architecture. This allows the Servlet to perform front end processing including authentication and authorization, centralized logging and help with internationalization, interacting with the model for the data.
- Once the request processing has been completed, the Servlet directs the request to the appropriate JSP page, how the next page is determine varies widely across different applications.
For example, in simple application the next JSP page to display may be hard coded in the Servlet, based on the request parameters and current application state. And for the large web applications, a workflow or rules engine can also be used.
We have many advantages in using MVC 2 Architecture :
- Navigation control is now centralized, that is, only controllers contains the logic to determine the next page.
- It supports the industry standard for developing the web applications.
- It is easy to maintain and also it is easy to extend.
- MVC 2 supports better separation of concerns. That is, modifications to one logic will not affect the other logic and
- also parallel development will be possible.
But one small limitation we have is :
- if we change the controller code, we need to recompile the Servlet class and we need to redeploy it.
