🔁 JSP Request-to-Response Lifecycle
1. Client Sends HTTP Request
A user visits a URL like:
http://example.com/hello.jsp
This sends an HTTP GET (or POST) request to the web server.
2. JSP File Is Located
The server (like Tomcat) checks if hello.jsp exists in the web application directory (usually inside /webapp).
3. JSP Is Translated into a Servlet
If it’s the first time the JSP is requested (or it has changed):
- The JSP engine (part of the servlet container) translates the JSP into a Java servlet source file.
- Example:
hello_jsp.java
This Java class contains equivalent servlet code that implements HttpServlet
.
4. Servlet Is Compiled
- The generated servlet source file is compiled into a
.class
file. - Example:
hello_jsp.class
Now, the servlet is ready to handle the request.
5. Servlet Is Loaded into Memory
- The servlet class is loaded by the container’s classloader.
- A singleton instance is created (unless already loaded).
5. Servlet Is Loaded into Memory
- The servlet class is loaded by the container’s classloader.
- A singleton instance is created (unless already loaded).
6. Servlet’s service()
Method Is Called
- The container calls the servlet’s
service()
method, which usually callsdoGet()
ordoPost()
, depending on the request type. - The servlet executes any embedded Java logic and writes HTML into the response.
7. HTML Response Sent to Client
- The servlet’s output (HTML) is sent back in the HTTP response to the browser.
- The user sees a dynamically generated web page.
8. Subsequent Requests
- The translated and compiled servlet is reused.
- No need to recompile the JSP unless the file changes.
📌 Diagram Summary:
[Browser] → [Web Server] → [JSP Engine] →
→ [Translate JSP → Servlet] → [Compile Servlet] →
→ [Execute Servlet] → [Generate HTML] →
→ [Send HTML Response] → [Browser Displays Page]
⚙️ Technologies Involved:
- JSP Engine (e.g., Jasper in Apache Tomcat)
- Servlet Container (e.g., Catalina)
- Java Compiler
- HTTP Server (e.g., Tomcat/Jetty)