To build interactive web pages in JavaServer Pages (JSP), developers rely on three key language features: Scripting Elements for embedded Java execution, Directive Elements for page-level headers, and pre-instantiated Implicit Objects provided automatically by the servlet container.
1. JSP Scripting Elements (Tags)
JSP scripting elements allow embedding Java code directly into an HTML page. There are 3 types of scripting tags:
1. Scriptlet Tag <% ... %>
The Scriptlet tag is used to execute standard Java control logic (loops, conditionals, variable operations) inside the generated servlet's _jspService() method.
<%
// Valid Java code goes here
String message = "JSP is better than Servlets";
out.println(message);
%>
2. Declarative Tag <%! ... %>
The Declarative tag is used to declare class-level variables and methods in the generated Servlet class outside the _jspService() method scope.
<%!
// Instance variable declaration
String globalMsg = "Welcome to JSP Page";
// Custom method declaration
public int calculateSquare(int number) {
return number * number;
}
%>
3. Expression Tag <%= ... %>
The Expression tag evaluates a Java expression, converts the result into a String, and outputs it directly into the client response stream. Note: Do not include a terminating semicolon inside expression tags!
<h2>Message: <%= globalMsg %></h2>
<p>Square of 5 is: <%= calculateSquare(5) %></p>
| Tag Type | Syntax | Execution Scope | Primary Purpose |
|---|---|---|---|
| Scriptlet Tag | <% code %> |
Inside _jspService() method |
Executes statements, loops, conditionals, and business logic. |
| Declarative Tag | <%! declarations %> |
Outside _jspService() method (Class level) |
Declares class instance variables and reusable helper methods. |
| Expression Tag | <%= expression %> |
Evaluated directly into response output | Prints string values directly without requiring out.print(). |
2. JSP Directive Elements (<%@ ... %>)
Directives specify instructions and settings for the JSP container during the translation phase when converting the .jsp file into a Java Servlet.
<%@ directive attribute_name="value" %>
A. Page Directive (<%@ page ... %>)
Provides global instructions to the JSP container about the page character set, language, package imports, session support, and exception handling.
language="java": Specifies the scripting language used (default: Java).contentType="text/html": Specifies the MIME type and character encoding of the response.import="package.name.*": Imports Java packages into the generated servlet file.errorPage="error.jsp": Defines the unhandled exception fallback error handler page.session="true|false": Controls whether the page participates in HTTP session tracking.
<%@ page language="java" contentType="text/html" import="java.util.*" %>
<html>
<head><title>JSP Directives Demo</title></head>
<body>
<h3>Today's Date: <%= new Date().toString() %></h3>
</body>
</html>
B. Include Directive (<%@ include ... %>)
Includes another file (static HTML, text, or JSP snippet) at translation time before compilation occurs.
C. Taglib Directive (<%@ taglib ... %>)
Declares custom tag libraries (such as JSTL core tags) and establishes a prefix for tag references.
3. JSP Implicit Objects Reference
JSP implicit objects are predefined Java objects that the JSP container automatically instantiates and exposes inside the _jspService() method. Developers can access them directly without manual declaration!
| Implicit Object | Underlying Java Class / Interface | Description & Usage Example |
|---|---|---|
request |
javax.servlet.http.HttpServletRequest |
Encapsulates incoming HTTP request data (form params, headers, attributes).String user = request.getParameter("username"); |
response |
javax.servlet.http.HttpServletResponse |
Manages outbound HTTP response headers, content-type, cookies, and redirects.response.setContentType("text/html"); |
session |
javax.servlet.http.HttpSession |
Stores state data persistent across client requests.session.setAttribute("user", username); |
application |
javax.servlet.ServletContext |
Provides application-wide shared context data for all users.String info = application.getServerInfo(); |
config |
javax.servlet.ServletConfig |
Contains servlet configuration and initialization parameters.String name = config.getServletName(); |
exception |
java.lang.Throwable |
Captures unhandled runtime errors inside JSP error pages (requires isErrorPage="true"). |
out |
javax.servlet.jsp.JspWriter |
Output stream buffer used to send text and HTML to client output.out.println("Welcome to JSP World!"); |
Next Topic in Series: JSTL Tag Libraries
Learn how JSTL replaces Java scriptlet tags with clean XML-based core tags and functions:
Read Part 3: JSTL Core Tags & EL Functions Guide →