Skip to content

YE01‐Without html tag

Zhamri Che Ani edited this page Apr 10, 2026 · 1 revision

Example Code-1

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
</head>
<body>

Name: ${student.name} <br>
Matric No: ${student.matricNo} <br>
Program: ${student.program}

</body>
</html>

Example Code-2

Name: ${student.name} <br>
Matric No: ${student.matricNo} <br>
Program: ${student.program}

What happens internally

Both codes are converted to Servlet like:

out.write("Name: ");
out.write(student.getName());
out.write("<br>");

So:

  1. Expression Language (EL) works the same in both
  2. Output is the same

Important difference

Code 1 (Correct / Complete Page)

  • ✔ Valid HTML document
  • ✔ Can open directly in browser
  • ✔ Recommended for full page

Code 2 (Fragment / Partial)

  • ✔ Works inside JSP
  • ❌ Not a complete HTML page
  • ❌ Not ideal as standalone page

When to use each

Use Case Recommended
Full page Code 1
Included fragment (header/footer) Code 2
AJAX response Code 2

Best Practice

  1. Always use Code 1 for main JSP page
  2. Use Code 2 for reusable parts

Example

include header/footer

<jsp:include page="header.jsp"/>

header.jsp usually looks like this (no tag):

<h1>Welcome</h1>

Clone this wiki locally