I am trying to generate a csv file using a jsp file this its code:
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%-- Set the content type --%>
<%@ page contentType="text/csv"%>
<c:forEach items="${chartData}" var="chartDataEntry" varStatus="status">
${chartDataEntry.date}, ${chartDataEntry.open}, ${chartDataEntry.high}, ${chartDataEntry.low}, ${chartDataEntry.close}, ${chartDataEntry.volume}
</c:forEach>
Its quite simple, and it works fine. The problem is that I need to insert a breakline after each row, but I can’t get it working. I have tried with usuals: \n \r\n and so on.
Any ideas?
In a JSP there are too much environmental factors which can malform this. The JSP itself also emits some “unintentional” whitespace and other characters. Also, the
trimSpacessetting may eat some whitespace/newlines. It’s generally considered a poor practice to (ab)use JSP for something else than plain HTML.Rather use a servlet instead for a robust and consistent CSV output: