Online Courses
Free Tutorials  Go to Your University  Placement Preparation 
0 like 0 dislike
2k views
in RTU B.Tech (CSE-V Sem) Advance Java Lab by Goeduhub's Expert (5.8k points)

JSP Architecture and its Life Cycle

Goeduhub's Top Online Courses @Udemy

For Indian Students- INR 360/- || For International Students- $9.99/-

S.No.

Course Name

 Coupon

1.

Tensorflow 2 & Keras:Deep Learning & Artificial Intelligence

Apply Coupon

2.

Natural Language Processing-NLP with Deep Learning in Python Apply Coupon

3.

Computer Vision OpenCV Python | YOLO| Deep Learning in Colab Apply Coupon
    More Courses

1 Answer

0 like 0 dislike
by Goeduhub's Expert (5.8k points)
 
Best answer

Introduction to JSP

  • Java Server Pages or as is normally called JSP is a Java based technology that simplifies the developing of dynamic web sites.
  • It is a technology developed by Sun Microsystems, back in 1999.
  • JSP pages are HTML pages with embedded code that allows to access data from Java code running on the server.
  • JSP contains an extension of .jsp
  • JSP provides separation of HTML presentation logic from the application logic.

Advantages of JSP

  • JSP Provides an extensive infrastructure for:
    • Tracking sessions.
    • Managing cookies.
    • Reading and sending HTML headers.
    • Parsing and decoding HTML form data.
  • JSP is Efficient: Every request for a JSP is handled by a simple Java thread
  • JSP is Scalable: Easy integration with other backend services
  • Seperation of roles
    • Developers
    • Content Authors/Graphic Designers/Web Masters

JSP Architecture

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.

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 Life Cycle

A JSP life cycle is defined as the process from its creation till the destruction. This is similar to a servlet life cycle with an additional step which is required to compile a JSP into servlet.

The four major phases of a JSP life cycle are very similar to the Servlet Life Cycle.

  • Compilation
  • Initialization
  • Execution
  • Cleanup

JSP Life Cycle

JSP Compilation

When a browser asks for a JSP, the JSP engine first checks to see whether it needs to compile the page. If the page has never been compiled, or if the JSP has been modified since it was last compiled, the JSP engine compiles the page.

The compilation process involves three steps −

  • Parsing the JSP.
  • Turning the JSP into a servlet.
  • Compiling the servlet.

JSP Initialization

When a container loads a JSP it invokes the jspInit() method before servicing any requests. If you need to perform JSP-specific initialization, override the jspInit() method.

public void jspInit(){
   // Initialization code...
}

Typically, initialization is performed only once and as with the servlet init method, you generally initialize database connections, open files, and create lookup tables in the jspInit method.

JSP Execution

  • This phase of the JSP life cycle represents all interactions with requests until the JSP is destroyed.
  • Whenever a browser requests a JSP and the page has been loaded and initialized, the JSP engine invokes the _jspService() method in the JSP.
  • The _jspService() method takes an HttpServletRequest and an HttpServletResponse as its parameters as follows 
void _jspService(HttpServletRequest request, HttpServletResponse response) {
   // Service handling code...
}

The _jspService() method of a JSP is invoked on request basis. This is responsible for generating the response for that request and this method is also responsible for generating responses to all seven of the HTTP methods, i.e, GET, POST, DELETE, etc.

JSP Cleanup

The destruction phase of the JSP life cycle represents when a JSP is being removed from use by a container.

The jspDestroy() method is the JSP equivalent of the destroy method for servlets. Override jspDestroy when you need to perform any cleanup, such as releasing database connections or closing open files.

public void jspDestroy() {
   // Your cleanup code goes here.
}

For more RTU V Sem Advance Java Lab Experiments Click here


3.3k questions

7.1k answers

394 comments

4.6k users

 Goeduhub:

About Us | Contact Us || Terms & Conditions | Privacy Policy || Youtube Channel || Telegram Channel © goeduhub.com Social::   |  | 
...