📜 Static vs Dynamic Content in JSP
Aspect | Static Content | Dynamic Content |
---|---|---|
Definition | Content that does not change for each user or request. | Content that can change depending on request parameters, user input, or backend logic. |
Written as | Plain HTML, CSS, JavaScript inside the JSP page. | Embedded Java code, expressions, or custom tags inside the JSP page. |
Processing | Directly copied into the servlet’s output (out.write() ). | Evaluated at runtime, and result is inserted into the output. |
Performance | Very fast — no calculation needed. | Slightly slower — because server has to execute Java code first. |
Example | <h1>Welcome to our website</h1> | <h1>Welcome, <%= user.getName() %>!</h1> |
🔥 Examples
Static Content Example:
<html>
<body>
<h1>Welcome to our website</h1> <!-- Static: same for every visitor -->
</body>
</html>
This HTML will be the same for everyone.
Server just copies it into the HTTP response.
Dynamic Content Example:
<html>
<body>
<h1>Welcome, <%= request.getParameter("username") %>!</h1> <!-- Dynamic -->
</body>
</html>
If a user accesses hello.jsp?username=Stanley
, they will see:
Welcome, Stanley!
If another user accesses hello.jsp?username=Marina
, they will see:
Welcome, Marina!
Here, <%= request.getParameter("username") %>
is evaluated during request processing.
⚙️ How They Are Handled Internally:
When the server translates a JSP into a servlet:
- Static content becomes simple
out.write("...")
Java statements.
- Dynamic content becomes actual Java code execution inside the
_jspService()
method.
Example for Static:
out.write("<h1>Welcome to our website</h1>");
Example for Dynamic:
out.write("<h1>Welcome, " + request.getParameter("username") + "!</h1>");
🎯 Final Summary:
- Static content: Always the same, copied directly into response.
- Dynamic content: Depends on request or server-side calculations, generated at runtime.
Both coexist in a typical JSP file to create web pages that are partly constant and partly personalized.