<p>I have a list of bean objects passed into my JSP page, and one of them is a comment field. This field may contain newlines, and I want to replace them with semicolons using JSTL, so that the field can be displayed in a text input. I have found one solution, but it's not very elegant. I'll post below as a possibility.</p>

[{'answer_id': 58060, 'author': 'parkerfath', 'author_id': 6027, 'author_profile': 'https://Stackoverflow.com/users/6027', 'pm_score': 7, 'selected': True, 'text': '<p>Here is a solution I found. It doesn\'t seem very elegant, though:</p>\n\n<pre><code>&lt;%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %&gt;\n&lt;% pageContext.setAttribute("newLineChar", "\\n"); %&gt;\n\n${fn:replace(item.comments, newLineChar, "; ")}\n</code></pre>\n'}, {'answer_id': 58096, 'author': 'Brian Matthews', 'author_id': 1969, 'author_profile': 'https://Stackoverflow.com/users/1969', 'pm_score': 2, 'selected': False, 'text': '<p>You should be able to do it with fn:replace.</p>\n\n<p>You will need to import the tag library into your JSP with the following declaration:</p>\n\n<pre>\n&lt;%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %&gt;\n</pre>\n\n<p>Then you can use the following expression to replace occurrences of newline in ${data} with a semicolon:</p>\n\n<pre>\n${fn:replace(data, "\\n", ";")}\n</pre>\n\n<p>The documentation is not great on this stuff and I have not had the opportunity to test it.</p>\n'}, {'answer_id': 58105, 'author': 'Walter Rumsby', 'author_id': 1654, 'author_profile': 'https://Stackoverflow.com/users/1654', 'pm_score': 0, 'selected': False, 'text': '<p>You could write your own JSP function to do the replacement.</p>\n\n<p>This means you\'d end up with something like:</p>\n\n<pre><code>&lt;%@ taglib prefix="ns" uri="..." %&gt;\n...\n${ns:replace(data)}\n</code></pre>\n\n<p>Where <code>ns</code> is a namespace prefix you define and <code>replace</code> is your JSP function.</p>\n\n<p>These functions are pretty easy to implement (they\'re just a static method) although I can\'t seem to find a good reference for writing these at the moment.</p>\n'}, {'answer_id': 58109, 'author': 'Geekygecko', 'author_id': 6009, 'author_profile': 'https://Stackoverflow.com/users/6009', 'pm_score': 2, 'selected': False, 'text': '<p>You could create your own JSP function. \n<a href="http://java.sun.com/j2ee/1.4/docs/tutorial/doc/JSPTags6.html" rel="nofollow noreferrer">http://java.sun.com/j2ee/1.4/docs/tutorial/doc/JSPTags6.html</a></p>\n\n<p>This is roughly what you need to do.</p>\n\n<p>Create a tag library descriptor file <br/>\n/src/META-INF/sf.tld</p>\n\n<pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;\n&lt;taglib version="2.0" xmlns="http://java.sun.com/xml/ns/j2ee" \n  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" \n  xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee web-jsptaglibrary_2_0.xsd"&gt;\n  &lt;tlib-version&gt;1.0&lt;/tlib-version&gt;\n  &lt;short-name&gt;sf&lt;/short-name&gt;\n  &lt;uri&gt;http://www.stackoverflow.com&lt;/uri&gt;\n  &lt;function&gt;\n    &lt;name&gt;clean&lt;/name&gt;\n    &lt;function-class&gt;com.stackoverflow.web.tag.function.TagUtils&lt;/function-class&gt;\n    &lt;function-signature&gt;\n      java.lang.String clean(java.lang.String)\n    &lt;/function-signature&gt;\n  &lt;/function&gt;\n&lt;/taglib&gt;\n</code></pre>\n\n<p>Create a Java class for the functions logic.<br/>\ncom.stackoverflow.web.tag.function.TagUtils</p>\n\n<pre><code>package com.stackoverflow.web.tag.function;\n\nimport javax.servlet.jsp.tagext.TagSupport;\n\npublic class TagUtils extends TagSupport {\n  public static String clean(String comment) {\n    return comment.replaceAll("\\n", "; ");\n  }\n}\n</code></pre>\n\n<p>In your JSP you can access your function in the following way.</p>\n\n<pre><code>&lt;%@ taglib prefix="sf" uri="http://www.stackoverflow.com"%&gt;\n${sf:clean(item.comments)}\n</code></pre>\n'}, {'answer_id': 539976, 'author': 'Community', 'author_id': -1, 'author_profile': 'https://Stackoverflow.com/users/-1', 'pm_score': 0, 'selected': False, 'text': '<p>In the value while setting the var, press ENTER between the double quotes.</p>\n\n<p></p>\n\n<p>${fn:replace(data, newLineChar, ";")}</p>\n'}, {'answer_id': 733909, 'author': 'bousch', 'author_id': 89037, 'author_profile': 'https://Stackoverflow.com/users/89037', 'pm_score': 3, 'selected': False, 'text': '<p>This solution is more elegant than your own solution which is setting the pagecontext attribute directly. You should use the <code>&lt;c:set&gt;</code> tag for this:</p>\n\n<pre><code>&lt;%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %&gt;\n&lt;%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %&gt;\n\n&lt;c:set var="newLine" value="\\n"/&gt;\n${fn:replace(data, newLine, "; ")}\n</code></pre>\n\n<p>BTW: <code>${fn:replace(data, "\\n", ";")}</code> does NOT work.</p>\n'}, {'answer_id': 920681, 'author': 'Matt', 'author_id': 113719, 'author_profile': 'https://Stackoverflow.com/users/113719', 'pm_score': 2, 'selected': False, 'text': '<p>\\n does not represent the newline character in an EL expression.</p>\n\n<p>The solution which sets a <code>pageContext</code> attribute to the newline character and then uses it with JSTL\'s <code>fn:replace</code> function does work.</p>\n\n<p>However, I prefer to use the Jakarta String Tab Library to solve this problem:</p>\n\n<pre><code>&lt;%@ taglib prefix="str" uri="http://jakarta.apache.org/taglibs/string-1.1" %&gt;\n...\n&lt;str:replace var="result" replace="~n" with=";" newlineToken="~n"&gt;\nText containing newlines\n&lt;/str:replace&gt;\n...\n</code></pre>\n\n<p>You can use whatever you want for the newlineToken; "~n" is unlikely to show up in the text I\'m doing the replacement on, so it was a reasonable choice for me.</p>\n'}, {'answer_id': 1690888, 'author': 'Community', 'author_id': -1, 'author_profile': 'https://Stackoverflow.com/users/-1', 'pm_score': 2, 'selected': False, 'text': '<p>This does not work for me:</p>\n\n<pre><code>&lt;c:set var="newline" value="\\n"/&gt;\n${fn:replace(data, newLine, "; ")}\n</code></pre>\n\n<p>This does:</p>\n\n<pre><code>&lt;% pageContext.setAttribute("newLineChar", "\\n"); %&gt; \n${fn:replace(item.comments, newLineChar, "; ")}\n</code></pre>\n'}, {'answer_id': 1690942, 'author': 'BalusC', 'author_id': 157882, 'author_profile': 'https://Stackoverflow.com/users/157882', 'pm_score': 4, 'selected': False, 'text': "<p>Just use <code>fn:replace()</code> function to replace <code>\\n</code> by <code>;</code>.</p>\n\n<pre><code>${fn:replace(data, '\\n', ';')}\n</code></pre>\n\n<p>In case you're using Apache's EL implementation instead of Oracle's EL reference implementation (i.e. when you're using Tomcat, TomEE, JBoss, etc instead of GlassFish, Payara, WildFly, WebSphere, etc), then you need to re-escape the backslash.</p>\n\n<pre><code>${fn:replace(data, '\\\\n', ';')}\n</code></pre>\n"}, {'answer_id': 2554822, 'author': 'wheresrhys', 'author_id': 87739, 'author_profile': 'https://Stackoverflow.com/users/87739', 'pm_score': 0, 'selected': False, 'text': '<p>For the record, I came across this post while tackling this problem:  </p>\n\n<p>A multi-line string in JSTL gets added as the title attribute of a textarea. Javascript then adds this as the default text of the textarea. In order to clear this text on focus the value needs to equal the title... but fails as many text-editors put \\r\\n instead of \\n. So the follownig will get rid of the unwanted \\r:</p>\n\n<pre><code>&lt;% pageContext.setAttribute("newLineChar", "\\r"); %&gt; \n&lt;c:set var="textAreaDefault" value="${fn:replace(textAreaDefault, newLineChar, \'\')}" /&gt;\n</code></pre>\n'}, {'answer_id': 4044412, 'author': 'brunohop', 'author_id': 490275, 'author_profile': 'https://Stackoverflow.com/users/490275', 'pm_score': 1, 'selected': False, 'text': '<p>More easily:</p>\n\n<pre><code>&lt;str:replace var="your_Var_replaced" replace="\\n" with="Your ney caracter" newlineToken="\\n"&gt;${your_Var_to_replaced}&lt;/str:replace&gt;  \n</code></pre>\n'}, {'answer_id': 6960566, 'author': 'Leonid', 'author_id': 264834, 'author_profile': 'https://Stackoverflow.com/users/264834', 'pm_score': 2, 'selected': False, 'text': '<p>If what you really need is a \\n symbol you can use the advice from <a href="http://www.coderanch.com/t/283187/JSP/java/New-Line-character-JSTL-set#1298039" rel="nofollow">here</a>:</p>\n\n<pre><code>${fn:replace(text, "\n", "&lt;br/&gt;")}\n</code></pre>\n\n<p>or</p>\n\n<pre><code>&lt;c:set var="nl" value="\n" /&gt;&lt;%-- this is a new line --%&gt;\n</code></pre>\n\n<p>This includes the new line in your string literal.</p>\n'}, {'answer_id': 7124466, 'author': 'andy', 'author_id': 846977, 'author_profile': 'https://Stackoverflow.com/users/846977', 'pm_score': 4, 'selected': False, 'text': '<p>This is similar to the accepted answer (because it is using Java to represent the newline rather than EL) but here the &lt;c:set/&gt; element is used to set the attribute:</p>\n\n<pre><code>&lt;c:set var="newline" value="&lt;%= \\"\\n\\" %&gt;" /&gt;\n${fn:replace(myAddress, newline, "&lt;br /&gt;")}\n</code></pre>\n\n<p>The following snippet also works, but the second line of the &lt;c:set/&gt; element cannot be indented (and may look uglier):</p>\n\n<pre><code>    &lt;c:set var="newline" value="\n" /&gt;&lt;!--this line can\'t be indented --&gt;\n    ${fn:replace(myAddress, newline, "&lt;br /&gt;")}\n</code></pre>\n'}, {'answer_id': 31520021, 'author': 'Thomas Meyer', 'author_id': 5135628, 'author_profile': 'https://Stackoverflow.com/users/5135628', 'pm_score': 2, 'selected': False, 'text': '<p>This is a valid solution for the JSP EL:</p>\n\n<pre><code>"${fn:split(string1, Character.valueOf(10))}"\n</code></pre>\n'}]
['https://Stackoverflow.com/questions/58054', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/6027/']