1

So I'm working on an old application and I'm forced to use JSP at the moment, which I'm extremely unfamiliar with (and unfamiliar with most aspects of web development tbh).

I'm generating a JSON Array using JSONSimple in a Java class method, and then invoking that method from a JSP on a GET request to get the JSON. Sounds simple enough.

I was able to get my JSON when accessing the URL in my browser. Now I'm trying to access my JSP page from another application, and that's where I'm having difficulty. The content type of my "JSON" was text/html, and not JSON, so I tried setting the content type to JSON in my JSP, and now nothing is showing up in browser, and my Spring application is giving me this error when I try to get JSON from the URL: Could not read document: No content to map due to end-of-input at....

Here's my JSP:

<%@ page import="com.company.Someclass" %>

<% if(request.getMethod().equals("GET")){
    response.setContentType("application/json");
    Someclass.getJSONArray();
}
else if(request.getMethod().equals("POST")){
    //todo
}

%>  

Any help would be greatly appreciated as I'm pretty lost at the moment. Thanks!

hip
  • 21
  • 3
  • possible duplicate of http://stackoverflow.com/questions/10595775/set-content-type-to-application-json-in-jsp-file – Iofacture Oct 25 '16 at 19:28
  • @Matt1776 tried the solution in that one already and no luck :/ – hip Oct 25 '16 at 20:24
  • Can you show how you used that solution ? Use a REST Client to test your URL (I like Postman on Chrome for that). – AxelH Oct 26 '16 at 14:37

1 Answers1

1

Fixed this myself. I'm able to get JSON in the browser, but having trouble reading it in my Spring application. but thats another battle. Here's my solution:

<%@ page import="com.company.Someclass" %>

<% String ret = "";
if(request.getMethod().equals("GET")){
    response.setContentType("application/json");
    Someclass.getJSONArray().toJSONString();
}
else if(request.getMethod().equals("POST")){
    //todo
}

%>  

<%= ret %>

the toJSONString() is a method of json-simple's JSONArray class

hip
  • 21
  • 3
  • basically I was returning a JSON Object rather than a String, the browser and obviously my Spring didnt know what to do with it. – hip Oct 26 '16 at 16:20