Defining Variables with JSP Declarations
Declaration blocks in JSP allow you to define class-level variables or methods that can be utilized by scripting elements within the page. Any variable or method defined here is initialized when the JSP is translated into a servlet.
The standard syntax for declaring members is:
<%! member definition; %>
You may also use the XML-compliant equivalent:
<jsp:declaration>
member definition
</jsp:declaration>
Example of initializing variables and objects:
<%! int counter = 0; %>
<%! String firstName, lastName; %>
<%! MathContext context = new MathContext(4); %>
Outputting Data with JSP Expressions
An expression element contains a valid scripting language expression that is evaluated, converted to a String, and inserted into the response output at the current location. Unlike scriptlets, expressions do not require a semicolon to terminate the statement.
Syntax format:
<%= expression %>
XML equivalent:
<jsp:expression>
expression
</jsp:expression>
Example displaying the current server time:
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<html>
<head>
<meta charset="utf-8">
<title>Time Display</title>
</head>
<body>
<h3>Current Time: <%= new java.util.Date().toString() %></h3>
</body>
</html>
Commenting in JSP
JSP offers specific comment syntax to hide logic from the client or to document code. It is important to distinguish between JSP comments (hidden from client) and HTML comments (visible in source code).
Syntax comparison:
| Syntax | Description |
|---|---|
<%-- comment --%> |
JSP Comment: Ignored completely during compilation and not sent to the client. |
<!-- comment --%> |
HTML Comment: Visible to the client when viewing page source. |
Configuring Pages with Directives
Directives provide global settings for the JSP page, such as importing packages, setting error pages, or including external files.
General syntax:
<%@ directive attribute="value" %>
Common directive types:
| Directive | Purpose |
|---|---|
<%@ page ... %> |
Configures page-specific attributes like language, session, or error handling. |
<%@ include ... %> |
Statically inserts the content of another file during translation. |
<%@ taglib ... %> |
Declares a tag library for use in the JSP. |
Using JSP Actions (Standard Actions)
JSP Actions utilize XML-style tags to control the behavior of the servlet engine. They are used for tasks like including dynamic resources, working with JavaBeans, or forwarding requests.
Syntax:
<jsp:action_name attribute="value" />
Key action tags:
| Action | Function |
|---|---|
jsp:include |
Includes the response of a resource at runtime. |
jsp:useBean |
Instantiates or locates a JavaBean. |
jsp:setProperty |
Sets properties of a JavaBean. |
jsp:getProperty |
Retrieves a property value from a JavaBean and outputs it. |
jsp:forward |
Forwards the request to another resource. |
Implicit Objects in JSP
JSP provides several pre-defined variables, known as implicit objects, which are automatically available in scriptlets and expressions without needing explicit declaration.
| Object | Type / Description |
|---|---|
request |
HttpServletRequest: Represents the client's request. |
response |
HttpServletResponse: Used to send a response to the client. |
out |
JspWriter: The output stream for sending content to the client. |
session |
HttpSession: Menages user session data. |
application |
ServletContext: Represents the web application context. |
pageContext |
PageContext: Provides access to all namespaces and attributes. |
Flow Control and Java Logic
Since JSP pages are ultimately translated into Servlets, you can embed standard Java control flow statements (like if, for, and while) directly within the page using scriptlets.