JSP

Page Encoding
<%@ page language="java" pageEncoding="UTF-8" contentType="text/html; charset=UTF-8" %>

Use tag library
<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions"%>
${fn:length(clippingsViewData)}

Set value to a variable
<c:set var="salary" scope="session" value="${2000*2}" />
<c:set var="test" scope="application" value="Application Level Value" />
<c:set var="publicationId">${publication.id}</c:set> // this conver to string
<c:set var="publicationId" value="${publication.id}" /> // will be remain an integer
<c:set var="successUrl" value="http://${pageContext.request.serverName}${pageContext.request.contextPath}/${section.directoryPath}" />;
${sessionScope.salary}
${applicationScope.test}

Read from ApplicationResouce.properties file with parameter

In jsp file:

<fmt:message key="my.message.key">
    <fmt:param value="${the.first.place.holder}"/>
    <fmt:param value="${the.second.place.holder}"/>
</fmt:message>

In ApplicationResouce.properties file:

my.message.key=You have param {0} and {1}

To prevent the conversion, escapeXml should be explicitly set to false as follows
<c:out value="${product.listPrice}" escapeXml="false" />

Use bean
<jsp:useBean id="commentsList" type="java.util.List" scope="request" />

If Statement
<c:if test="${not empty fieldValue}">
    <c:set target="${fieldMap}" property="${field.name}" value="${fieldValue}" />
</c:if>

If-ElseIf Statement
<c:choose>
    <c:when test="${not empty profilePictures.items and .content.articleTypeName == 'avatar'}">
        <c:set target="${comments}" property="avatarImageUrl" value="${profilePictures.original}" />
    </c:when>
    <c:otherwise>
        <c:set target="${comments}" property="avatarImageUrl" value="${skinUrl}gfx/default-avatar.jpg" />
    </c:otherwise>
</c:choose>

For loop
<c:forEach items="${xyz}" varStatus="loop">  
    [${loop.index}]: ${xyz[loop.index]}<br/>  
</c:forEach>

jsp
Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-ShareAlike 3.0 License