jstl foreach omit an element in last record

Just use LoopTagStatus#isLast(). <c:forEach items=”${fileList}” var=”current” varStatus=”loop”> { id: 1001, data: [ “<c:out value=”${current.fileName}” />”, “<c:out value=”${current.path}” />”, “<c:out value=”${current.size}” />”, “<c:out value=”${current.type}” />” ] }<c:if test=”${!loop.last}”>,</c:if> </c:forEach> You can also use the conditional operator in EL instead of <c:if>: ${!loop.last ? ‘,’ : ”}

Convert and format a Date in JSP

In JSP, you’d normally like to use JSTL <fmt:formatDate> for this. You can of course also throw in a scriptlet with SimpleDateFormat, but scriptlets are strongly discouraged since 2003. Assuming that ${bean.date} returns java.util.Date, here’s how you can use it: <%@ taglib prefix=”fmt” uri=”http://java.sun.com/jsp/jstl/fmt” %> … <fmt:formatDate value=”${bean.date}” pattern=”yyyy-MM-dd HH:mm:ss” /> If you’re actually using … Read more

Declaring functions in JSP?

You need to enclose that in <%! %> as follows: <%! public String getQuarter(int i){ String quarter; switch(i){ case 1: quarter = “Winter”; break; case 2: quarter = “Spring”; break; case 3: quarter = “Summer I”; break; case 4: quarter = “Summer II”; break; case 5: quarter = “Fall”; break; default: quarter = “ERROR”; } … Read more

Hidden features of JSP/Servlet [closed]

Note: I find it hard to think of any “hidden features” for JSP/Servlet. In my opinion “best practices” is a better wording and I can think of any of them. It also really depends on your experience with JSP/Servlet. After years of developing you don’t see those “hidden features” anymore. At any way, I’ll list … Read more

How to use relative paths without including the context root name?

If your actual concern is the dynamicness of the webapp context (the “AppName” part), then just retrieve it dynamically by HttpServletRequest#getContextPath(). <head> <link rel=”stylesheet” href=”https://stackoverflow.com/questions/4764405/${pageContext.request.contextPath}/templates/style/main.css” /> <script src=”${pageContext.request.contextPath}/templates/js/main.js”></script> <script>var base = “${pageContext.request.contextPath}”;</script> </head> <body> <a href=”${pageContext.request.contextPath}/pages/foo.jsp”>link</a> </body> If you want to set a base path for all relative links so that you don’t need to … Read more

JSP EL String concatenation [duplicate]

Using java string concatenation works better. #{var1 == 0 ? ‘hi’ : ‘hello’.concat(var2)} The benefit here is you can also pass this into a function, for instance #{myCode:assertFalse(myVar == “foo”, “bad myVar value: “.concat(myVar).concat(“, should be foo”))}

How to internationalize/localize a JSP/Servlet web application?

In case of a basic JSP/Servlet webapplication, the basic approach would be using JSTL fmt taglib in combination with resource bundles. Resource bundles contain key-value pairs where the key is a constant which is the same for all languages and the value differs per language. Resource bundles are usually properties files which are loaded by … Read more