JSP Code Formatting

General Guidelines

  • Use two spaces for indentation instead of tabs.
  • Save all files with UTF-8 encoding.
  • Ensure content is encoded in UTF-8.

For JSP pages, use JSTL (JavaServer Pages Standard Tag Library) to separate logic from markup. Keep HTML clean by moving code into custom tags or Java Beans when possible. For Bloomreach-specific functionality, use HST (Hippo Site Toolkit) tags.

When writing expressions in JSTL, use the following operators:

  • Use div instead of / for division.
  • Use mod instead of % for modulo (remainder).
  • Use eq instead of == to test for equality.
  • Use ne instead of != to test for inequality.
  • Use lt instead of < to test for less than.
  • Use gt instead of > to test for greater than.
  • Use le instead of <= to test for less than or equal.
  • Use ge instead of >= to test for greater than or equal.
  • Use and instead of && for logical AND.
  • Use or instead of || for logical OR.
  • Use not instead of ! for unary boolean complement.
  • Use empty to test for emptiness.

Output Escaping

Always escape output to prevent cross-site scripting (XSS) vulnerabilities and to ensure valid XHTML.

Code ExampleOutput
<c:out value="Bed & breakfast"/>Bed &amp; breakfast
<c:out value="<script>alert('hoi');</script>"/>&lt;script&gt;alert(&quot;hoi&quot;)&lt;/script&gt;

Internationalization

Do not hardcode text in JSP files. Always use a resource bundle for all display text.

Example:

<fmt:message key="your.message.key" var="submitValue"> <input type="submit" id="zoek-submit" name="submit" value="${submitValue}"/>

Use the fmt:formatDate tag to display dates. Specify date patterns in the resource bundle, and reference them in the tag.

Example:

<jsp:useBean id="now" class="java.util.Date" /> <fmt:message key="date.format.key" var="myDatePattern"> <fmt:formatDate value="${now}" pattern="${myDatePattern}"/>

EclipseHTMLEditor is an Eclipse plugin that supports editing JSP files.

You can use JettyLauncher to run your JSP site within Eclipse.

Refactoring

To support refactoring in JSP files, declare all variables with the following syntax:

<%--@elvariable id="document" type="org.myproject.MyBean"--%>

Always specify the concrete type for each variable. This enables IDEs such as IntelliJ IDEA to refactor EL expressions (for example, updating ${document.name} to ${document.title} if you rename the getName method to getTitle in your bean).

Share Feedback
Page: /about/platform-development/jsp-code-formatting
Section: About
Category *