Hello guys, let's learn the life cycle of JSP
It is a very important concept and frequently asked in interviews.
So, when a client access a JSP the server does the following tasks (client bole to web browser)
Step-1. Server checks if requested JSP is available or not, if JSP is not available 404 (resource not found) error will be shown at client side (bole toh server ko JSP nahin mili toh 404 error show ho jayegi)
Step-2. If JSP is available than server will translate the JSP into a JAVA file (bole toh .java file), inside this JAVA file the server will define a public class (hum sub ye toh jante hi hain ki public class ka naam and program ka name hamesha same hota hai)
for example: if JSP name is user.jsp than server will translate it to user_jsp.java file
Step-3. Server compiles the JAVA file into a BYTECODE (bole toh .class file)
for example: this bytecode is also known as SERVLET <<== iska meaning ye hai ki internally har JSP ek SERVLET hi hoti hai
note: Server will perform step-3, step-4, and step-5 only when we change the source code of JSP (yani agar hum JSP ka source code change na karen toh, har client request ke liye _jspService() method chalta rahega)
note: One more thing, a developer can override jspInit() and jspDestroy() methods inside declaration tag (bole toh <%! -------- %>), but we cant override _jspService() method, because this method is defined by the server itself
It is a very important concept and frequently asked in interviews.
So, when a client access a JSP the server does the following tasks (client bole to web browser)
Step-1. Server checks if requested JSP is available or not, if JSP is not available 404 (resource not found) error will be shown at client side (bole toh server ko JSP nahin mili toh 404 error show ho jayegi)
Step-2. If JSP is available than server will translate the JSP into a JAVA file (bole toh .java file), inside this JAVA file the server will define a public class (hum sub ye toh jante hi hain ki public class ka naam and program ka name hamesha same hota hai)
for example: if JSP name is user.jsp than server will translate it to user_jsp.java file
Step-3. Server compiles the JAVA file into a BYTECODE (bole toh .class file)
for example: this bytecode is also known as SERVLET <<== iska meaning ye hai ki internally har JSP ek SERVLET hi hoti hai
Step-4. Server loads the BYTECODE and create and object of it
Step-5. Now the server invokes jspInit(), _jspService(), jspDestroy() methods on this object <<== these methods are known as lifecycle methods, beccause they are invoked by the server in a particular order (we cant change this order)
jspInit(): This method will be invoked when the web-application is deployed in server or when the first request of JSP comes in (so, this method executes once only)
note: jspInit() is used for initialization propose
_jspService(): This method will be invoked for each incoming request of client (this method executes many times)
note: _jspService() is used for executing actual business logic (like any type of calculations/computations/data processing/or database access).
jspDesttor(): This method will be invoked when the server is stopped (so, this method executes once only)
note: jspInit() is used for initialization propose
note: One more thing, a developer can override jspInit() and jspDestroy() methods inside declaration tag (bole toh <%! -------- %>), but we cant override _jspService() method, because this method is defined by the server itself
Very interesting sir, wonderful
ReplyDeleteThank You Sir!!! You made it very easy to understand
ReplyDeleteExplained in a very simple and easy way.. Thank you sir
ReplyDeletevery interesting concept,I find it very easy to understand cause language is very easy
ReplyDeleteWah sir maza aa gaya, great article
ReplyDeleteVery simple to understand . I like it
ReplyDeleteVery helpful sir
ReplyDelete