title: Escape apostrophe as \' with c:out JSP
body: <p>I have an object field with person's last name.</p>
  
  <p>If I use <em>${person.lastName}</em>, I get <em>O'Brian</em></p>
  
  <p>If I use </p>
  
  <pre><code> &lt;c:out value="${person.lastName}"/&gt; 
  </code></pre>
  
  <p>I get O&#039;Brian</p>
  
  <p>Both outputs breaks the next jsp code in IE</p>
  
  <pre><code> &lt;a href="#" 
      class="delete" 
      onclick="if(confirm('&lt;c:out value="${application.lastName}"/&gt; ' + _('Are you sure you want to delete this application?'))) {deleteApplication('${application.identifier}')};return false;"&gt;&lt;bean:message key="application.delete"/&gt;&lt;/a&gt;
  </code></pre>
  
  <p>because it gets transformed to </p>
  
  <pre><code>    onclick="if(confirm('O&amp;#039;Brian '
  </code></pre>
  
  <p>or </p>
  
  <pre><code>    onclick="if(confirm('O'Brian '
  </code></pre>
  
  <p>I would need <em>O'Brian</em> to be escaped as <em>O\'Brian</em></p>
  
  <p>Any idea how to solve this issue?</p>
  
  <h1>SOLUTION</h1>
  
  <p>The most elegant solutions seems to use a simple Tag. </p>
  
  <pre><code>package view;
  
  import java.io.IOException;
  import javax.servlet.jsp.JspException;
  import javax.servlet.jsp.tagext.SimpleTagSupport;
  
  public class EscapeJS extends SimpleTagSupport {
      public String str;
  
      public void doTag() throws JspException, IOException {
          getJspContext().getOut().print(str.replaceAll("\'", "\\\\'"));
      }
  
      public String getStr() {
          return str;
      }
  
      public void setStr(String str) {
          this.str = str;
      }
  
  }
  </code></pre>
  
  <p>Then place in WEB-INF a utils.tld file: </p>
  
  <pre><code>&lt;!DOCTYPE taglib PUBLIC "-//Sun Microsystems, Inc.//DTD JSP Tag Library 1.1//EN" "http://java.sun.com/j2ee/dtds/web-jsptaglibrary_1_1.dtd"&gt;
  &lt;taglib&gt;
      &lt;tlibversion&gt;1.2&lt;/tlibversion&gt;
      &lt;jspversion&gt;1.1&lt;/jspversion&gt;
      &lt;shortname&gt;bean&lt;/shortname&gt;&lt;uri&gt;utilsTags&lt;/uri&gt;
      &lt;uri&gt;utilsTags&lt;/uri&gt;
      &lt;tag&gt;
          &lt;name&gt;escapeJS&lt;/name&gt;
          &lt;tagclass&gt;view.EscapeJS&lt;/tagclass&gt;
          &lt;bodycontent&gt;scriptless&lt;/bodycontent&gt;
          &lt;attribute&gt;
              &lt;name&gt;str&lt;/name&gt;
              &lt;required&gt;true&lt;/required&gt;       
              &lt;rtexprvalue&gt;true&lt;/rtexprvalue&gt; 
          &lt;/attribute&gt;                
      &lt;/tag&gt;      
  &lt;/taglib&gt;
  </code></pre>
  
  <p>Then inside your jsp:</p>
  
  <pre><code>&lt;%@ taglib prefix="utils" uri="utilsTags" %&gt;
  
  &lt;utils:escapeJS str="${application.firstName}"/&gt;
  </code></pre>