Nearby lessons

4 of 30

JSP - Template Text

📌 What You Will Learn
  • Understand what template text is
  • Know how the container processes template text
  • See how template text appears in the generated servlet

Template Text is any static content in a JSP page — HTML, XML, plain text — that is sent directly to the browser without processing. This lesson explains how template text works and how it relates to the generated servlet.

What is Template Text?

Template text is any content in a JSP page that is not a directive, action, or scripting element. It includes:

  • Plain text
  • HTML tags and attributes
  • XML tags

For template text, no processing is required — it becomes an argument to the out.write() method inside the _jspService() method of the generated servlet.

Example JSP:

Example01
JCode Cell
1 
2<h1>The Server Time is: <%= new java.util.Date() %></h1>
3

Generated Servlet

For the above JSP, the generated servlet contains:

Example02
JCode Cell
1 
2public final class demo_jsp extends ... {
3 public void _jspService(...) {
4 out.write("<h1>The Server Time is:"); ← template text
5 out.print(new java.util.Date()); ← expression
6 out.write("</h1>"); ← template text
7 }
8}
9

Why write() vs print()?

Template text uses out.write() because:

  • write() can only take character data as an argument
  • print() can take any type of data as an argument

Since template text is always character data, it becomes the argument of write().

Since an expression value can be any type (string, number, object), it must use print() which accepts any type.

📝 Key Takeaways
  • Template text is any static content in the JSP page
  • It becomes an argument to out.write() in the generated servlet
  • Expression values become arguments to print() instead

🧠 Test Your Knowledge

3 Questions
Progress: 0 / 3