2

First, please let me say that I recognize that having html saved in MySQL db is not ideal. But that's the way it's been setup and I just have to accept it and work with the existing set up.

So here's my issue.

I have a table "messages" that has "message" and "time" columns.

These are some sample values just for demonstration purposes:

MySQL: "message" column:

You've received an appraisal from xyz re something in the amount of $1,2000.<br><a class='lnk' href='http://www.website.com/somepage.jsp?r=393'>View some page</a>

"time" column:

2015-12-16 15:51:33.0

I can retrieve the values in a java class and convert the resultset data into a jsonArray.

If I sysout the jsonArray, I can see the values from the table displayed more or less correctly.

System.out.println("jsonArray: " + jsonArray);

Output:

 jsonArray: [{"message":"You've received a message from xyz re something in the amount of $1,200.<br><a class='lnk' href='http://www.website.com/somepage.jsp?r=393'>View some page<\/a>","time":"2015-12-16 15:51:33.0"}]

I am struggling with two issues:

1) Returning html tags without being escaped in the output

Please notice the escaped closing tag: </a> in the jsonArray above. Is there a way to retrieve it without the escape?

2) Returning jsonArray in jsp properly formatted and escaped.

I would like the JSP to return the jsonArray but I am not having much luck getting things to work correctly More specifically, I tried the following two to output the jsonArray on my jsp page:

out.print(jsonArray);

out.println(jsonArray.toString());

But in both cases, the escaped closing tags seems to be causing issues. The jsp output looks like this:

enter image description here

And this is the code generated in the jsp.

[{"message":"You've received an appraisal from xyz re something in the amount of $1,2000.<br><a class="lnk" href="http://www.website.com/somepage.jsp?r=393">View some page&lt;\/a&gt;","time":"2015-12-16 15:51:33.0"}]

Please notice the

   &lt;\/a&gt;

Could anyone please give me some tips how to properly format html tags in a jsonArray that contains data fetched from a database?

Thank you!

5
  • Are you looking to escape your HTML? Commented Dec 17, 2015 at 1:55
  • @Jia Jian - I am looking to display the closing </a> without the escape <\/a>. I can't figure out what I need to do to have the "message" value in the jsonObject display </a> instead of <\/a> Commented Dec 17, 2015 at 2:30
  • Escape your starting <a> tag first, because it's messing up your JSON and making it invalid (use JSONLint to check). Any specific reasons for not escaping it? It seems like your JSON response is returning text/html instead of application/json, is that intended? Commented Dec 17, 2015 at 2:53
  • @Jia Jian - thanks for the tip. setting the response.setContentType("application/json") & response.setCharacterEncoding("utf-8") got me on the right track. Please, post your comment as an answer. Thanks! Commented Dec 18, 2015 at 13:07
  • posted as an answer! Commented Dec 18, 2015 at 13:21

1 Answer 1

1

Turning this into an answer.

Set your response type to application/json instead of text/html should do the trick.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.