JSP Code Formatting

General Guidelines

  • Use two spaces instead of tabs
  • Save your files as UTF-8!
  • Use UTF-8 for the content

For the JSP pages use JSTL. Try to keep the HTML as clean as possible, so put your code in a custom tag or in a Java Bean. For Hippo specific tasks use the HST (Hippo Site Toolkit) tags.

  • use div instead of / for division
  • use mod instead of % for Modulo (remainder)
  • use eq instead of == for Test for equality
  • use ne instead of != for Test for inequality
  • use lt instead of < for Test for less than
  • use gt instead of > for Test for greater than
  • use le instead of <= for Test for less than or equal
  • use gt instead of >= for Test for greater than or equal
  • use and instead of && for Test for logical AND
  • use or instead of || for Test for logical OR
  • use not instead of ! forvUnary Boolean complement
  • use empty for Test for emptyness

Output Escaping

Escape all output. We don’t want XSS and we do want valid XHTML.

code

result

<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;

Internationalisation

Never put text in your code always use a resource bundle for that.
Example:

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

Always use the format tag for showing dates. Always use patterns in the tag but put the patterns in the resource bundle.
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 a useful Eclipse plugin for editing JSP files.

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

Refactoring

To enable refactoring of code in JSP files always declare all variables in JSP files with the following code

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

For type use the concrete type of variable. Benefit: if you used ${document.name} in the JSP and refactor the getName method of your bean to getTitle, Intellij will also refactor the statement in the JSP to ${document.title}.

Did you find this page helpful?
How could this documentation serve you better?
On this page
    Did you find this page helpful?
    How could this documentation serve you better?