Alan
Alan

Reputation: 822

replacing HTML special characters in JSP

I've created a class named ServletUtils which has a method in it that replaces all HTML special characters and looks like this:

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;

public class ServletUtils
{
  public static String replaceHTMLSpecialChars(String str)
  {
    StringBuffer buffer = new StringBuffer();
    char temp;
    for(int i = 0; i < str.length(); i++)
    {
      temp = str.charAt(i);
      if(temp == '<')
      {
          buffer.append("&lt;");
      }
      else if(temp == '>')
      {
          buffer.append("&gt;");
      }
      else if(temp == '"')
      {
          buffer.append("&quot;");
      }
      else if(temp == '&')
      {
          buffer.append("&amp;");
      }
      else
      {
          buffer.append(temp);
      }
    }
    return buffer.toString();
  }

  public static String replaceHTMLSpecialCharsFromFile(String filepath)
  {
    StringBuffer buffer = new StringBuffer();
    try
    {
      File file = new File(filepath);
      if(file.exists())
      {
        BufferedReader br = new BufferedReader(new FileReader(file));
        String line;
        while ((line = br.readLine()) != null) {
        buffer.append(line + "\n");
        }
        br.close();             
      }
      else
      {
        System.out.println("File doesn't exist: " + filepath);
      }
    }
    catch(FileNotFoundException fnfe){fnfe.printStackTrace();}
    catch(IOException io){io.printStackTrace();}

    return replaceHTMLSpecialChars(buffer.toString());
  }

  public static void main(String[] args)
  {
    System.out.println(ServletUtils.replaceHTMLSpecialCharsFromFile("C:\\Program Files\\Apache Software Foundation\\Tomcat 7.0\\webapps\\jed_1.1\\jsp\\server_processing.jsp"));
  }

}

Testing it with the main method certainly works, but from a JSP page, when I call:

<textarea rows="25" cols="110" readonly="readonly">
<%
  String contextPath = request.getServletContext().getRealPath("/");
  ServletUtils.replaceHTMLSpecialCharsFromFile(contextPath + "jsp\\server_processing.jsp");
%>
</textarea>

I get a blank textarea. I know the ServletUtils methods work. So why am I not able to load the textarea in the JSP page with the results? Please advise.

Upvotes: 0

Views: 3409

Answers (3)

Alan
Alan

Reputation: 822

Ok, I figured it out. All I had to do was use the JSP out like so:

<textarea rows="25" cols="110" readonly="readonly">
<%
  out.print(ServletUtils.replaceHTMLSpecialCharsFromFile(contextPath + "jsp\\server_processing.jsp"));
%>

</textarea>

Upvotes: -1

Braj
Braj

Reputation: 46871

Try with JSTL fn:escapeXml() Function that escapes characters that can be interpreted as XML markup. There is no need to replace the HTML special characters.

<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %>
...

<c:set var="text" value="<html><head><body></body></head></html>"/>
<textarea>${fn:escapeXml(text)}</textarea>

output:

enter image description here

Upvotes: 1

Chris Martin
Chris Martin

Reputation: 30756

The last line in your scriptlet does evaluate to the string you want, but nothing is printing the string.

The JSTL core out tag does what you want, and will also do the HTML entity escapes for you (so you don't need the replaceHTMLSpecialChars method).

<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>

<%
String contextPath = request.getServletContext().getRealPath("/");
String text = contextPath + "jsp\\server_processing.jsp";
%>

<textarea rows="25" cols="110" readonly="readonly">
    <c:out value="${text}"/>
</textarea>

Upvotes: 2

Related Questions