0

I am producing a JSON string from a database query in JSP but it always has unprintable characters in it and I don't understand why!

The JSP to produce the JSON is below:

  String user = "username"; // set a username
  String password = "password"; // set a password
  Class.forName("org.firebirdsql.jdbc.FBDriver"); // JDBC for firebird a.k.a. Jaybird
  String DB = "jdbc:firebirdsql://123.123.123.123:3050/C:\\db.fdb";

  JSONArray obj=new JSONArray(); //Creating the json object

    Connection connection = DriverManager.getConnection(DB, user, password);              
    Statement statement = connection.createStatement();

  int i=0;
  Class.forName("org.firebirdsql.jdbc.FBDriver"); // JDBC for firebird a.k.a. Jaybird
  String query = "SELECT ses.sessionid, ses.datetime, ses.guid, ses.staffid FROM session ses ORDER by ses.datetime";
  ResultSet resultset = statement.executeQuery(query);

  while (resultset.next())
    {
    JSONObject j = new JSONObject();
    j.put("SessionID", resultset.getString("sessionid"));
    j.put("DateTime", resultset.getString("datetime"));
    j.put("GUID", resultset.getString("guid"));
    j.put("StaffID", resultset.getString("staffid"));
    obj.add(i, j);
    i++; // Counter for indexing the JSONArray
  }

  resultset.close();
  statement.close();
  connection.close();

And this is the code I am using in PHP to display:

    echo '*'.$json.'*<br>';
    echo strlen($json).'<br>';
    $json = preg_replace('/[\x00-\x1F\x80-\xFF]/', '', $json);
    echo '*'.$json.'*<br>';
    echo strlen($json).'<br>';

which shows:

* [{"SessionID":"850","DateTime":"2011-10-03 14:21:37.0","GUID":"51e71c19-ca13-4053-bd95-2addb5ba69f6","StaffID":"804"}] *
146
*[{"SessionID":"850","DateTime":"2011-10-03 14:21:37.0","GUID":"51e71c19-ca13-4053-bd95-2addb5ba69f6","StaffID":"804"}]*
118

So a difference of 28 unprintable characters - mostly at the beginning. How are they getting there and how can I get rid of them in the JSP?

Thanks

williamsdb
  • 1,054
  • 2
  • 17
  • 29

1 Answers1

1

Because JSP is as being a view technology part of the HTTP response. Everything outside <% %> is sent to the response as well, including whitespace and newlines (rightclick the HTML page produced by PHP, do View Source and you'll see those newlines yourself as well, it are those newlines which you account to "unprintable" characters).

Remove any whitespace and newlines outside <% %> or, better, use a servlet instead. You can find some kickoff examples of a JSON-producing Servlet in the answer to How to use Servlets and Ajax?

Community
  • 1
  • 1
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • Thanks for the response. There isn't that much whitespace and most is coming from the "@page import"'s which I have no control over. Is a servlet the only way as that means re-writing all my code. – williamsdb Oct 03 '11 at 13:10
  • Just remove any whitespace between `%>` and `<%` including newlines. You don't need to rewrite all code. Just copypasting into `doGet()` method is fine. You only need to prepare `out` yourself as `response.getWriter()`. Writing Java code in a JSP file instead of a Java class is a poor practice anyway. Just never do that. See also http://stackoverflow.com/questions/3177733/how-to-avoid-java-code-in-jsp-files – BalusC Oct 03 '11 at 13:14