JSP Life Cycle Explained in 6 Phases

Day 29 — #100DaysOfJava ☕ What happens when a browser requests a JSP page? Not "it shows HTML." What actually happens. Step by step. Inside the server. --- The JSP Life Cycle has 6 phases. Here is every one of them. Phase 1 — Translation your .jsp file is not Java yet. The server's JSP Container takes your file and converts it into a .java Servlet file. Every JSP tag becomes Java code. Every piece of HTML becomes an out.write() call. You never see this file unless you look inside the server's metadata folder. Phase 2 — Compilation That generated .java file is compiled into a .class bytecode file. Same as any Java program. This is why JSP is fast after the first request — the compilation already happened. Phase 3 — Loading and Instantiation The .class file is loaded onto the server and an object of that Servlet class is created. One object. Created once. Shared across all requests. Phase 4 — Initialization The jspInit() method runs once. This is where you set up resources that every request will share — database connections, configuration values. Runs one time only. Never again unless the server restarts. Phase 5 — Request Handling This is where your users actually get served. For every single request that comes in, _jspService() runs. This is the method that processes dynamic content, reads request parameters, and writes the HTML response. Every request. Every time. Phase 6 — Destroy When the server shuts down or the JSP is no longer needed, jspDestroy() runs. Cleanup happens here — close connections, release resources. Runs once. Then the object is gone. --- The three interview questions this answers: How many phases in JSP life cycle? — Six. Which method handles client requests? — _jspService(). Not doGet(). Not doPost(). The JSP Container calls _jspService() directly — it handles both GET and POST internally. What creates the initial JSP object? — The JSP Container. Not you. You never instantiate a JSP manually. The container controls the entire lifecycle. --- The thing that clicked today — JSP life cycle and Servlet life cycle are almost identical. Servlet: init() → service() → destroy() JSP: jspInit() → _jspService() → jspDestroy() JSP just has two extra steps at the beginning — Translation and Compilation — because your file starts as JSP and needs to become a Servlet first. After that, it IS a Servlet. Running the same lifecycle. The same container managing it. The same object serving every request. JSP is not a different technology from Servlets. It is Servlets with a friendlier way to write HTML. --- One thing most beginners miss — that .class file is cached. The first request to a JSP page is slow. Translation. Compilation. Loading. Instantiation. Initialization. All happening before a single user sees anything. Every request after that? Fast. The object is already loaded. _jspService() just runs. #Java #JSP #Servlets #JSPLifeCycle #BackendDevelopment #WebDevelopment #JavaDeveloper

To view or add a comment, sign in

Explore content categories