Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to simply return JSON from a JSP

Tags:

json

jsp

Can anyone give me an example of how to return the following json simply from a jsp without any external libraries (except the ones that come standard with Oracle Java)?

[
   {"label":"item 1", "value":"item 1", "id": 1},
   {"label":"item 2", "value":"item 2", "id": 2},
   {"label":"item 3", "value":"item 1", "id": 3}
];

I tried

<%-- Set the content type header with the JSP directive --%>
<%@ page contentType="application/json" %>

<%-- Set the content disposition header --%>
<%
   // Returns all employees (active and terminated) as json.
   response.setContentType("application/json");
   response.setHeader("Content-Disposition", "inline");
%>

<%@ page language="java"%>
<%@ page import="java.sql.*"%>
<%@ page import="java.util.*"%>
<%@ page import="java.text.*"%>
<%@ page import="javax.servlet.http.*"%>
<%@ page import="oracle.apps.fnd.common.WebAppsContext"%>
<%@ page import="oracle.apps.fnd.common.WebRequestUtil"%>
<%@ page import="oracle.apps.fnd.common.ResourceStore"%>
<%@ page import="oracle.apps.fnd.common.VersionInfo"%>

[
   {"label":"item 1", "value":"item 1", "id": 1},
   {"label":"item 2", "value":"item 2", "id": 2},
   {"label":"item 3", "value":"item 1", "id": 3}
];

but it does not seem to work, since my jquery autocomplete does not work with it.

Here's part of the autocomplete code:

<html>
<head>
      $(function() {
         var cust_ac = $("#autocomplete input#cust_input").autocomplete({
            source:         "xxpay_json_emp.jsp",
            change:         function (event, ui) { alert(ui.item.id); },
            width:          500,
            max:            3000,
            selectFirst:    false,
            delay:          250,
            minChars:       3,
            matchContains:  1,
            scroll:         false,
            scrollHeight:   200,
            maxItemsToShow: 20
        });
        $('#autocomplete').submit(function() {
           return false;   //  Cancel submit button on form.
        });
      });

      function handleKeyPress(e, form)
      {
         var key = e.keyCode || e.which;

         if (key == 13)
         {
            e.cancelBubble = true;
            e.returnValue = false;
         }
      }

   </script>
</head>
<body class='fdlbod'>
   <div style='padding-left : 20px'>
      <textarea id="holdtext" style="display:none;"></textarea>
      <form id="autocomplete" name="autocomplete">
<%
      out.println("Customer Name:&nbsp;");
      out.println("<input type='text' value='' name='cust_input' id='cust_input' size='80' onkeypress='handleKeyPress(event,this.form)' />");
%>
      </form>
   </div>
</body>
</html>
like image 273
Superdooperhero Avatar asked Feb 03 '12 07:02

Superdooperhero


People also ask

Can we return JSON object in Java?

With java is the same, only that you need to declare a new array with the JSONArray class and add the items with add . The JSONArray can convert a JSON text into a Java object. The toString method converts to JSON text, and that's what we are going to send to Javascript.

How do I return Jsonresponse?

To return JSON from the server, you must include the JSON data in the body of the HTTP response message and provide a "Content-Type: application/json" response header. The Content-Type response header allows the client to interpret the data in the response body correctly.

What is JsonResult?

What is JsonResult ? JsonResult is one of the type of MVC action result type which returns the data back to the view or the browser in the form of JSON (JavaScript Object notation format). In this article we will learn about JsonResult by taking scenario to bind view using the JSON Data .

How to generate JSON response from a link in JSP?

Here a link is available in jsp page on link click it is calling /jsonResponse url where JsonResponse’s doGet () method is get called. To generate json response we are setting response.setContentType ("application/json"); that is used to set response as application/json. Here response is reference variable of HttpServletResponse.

How to return a JSON response from a Java Servlet?

How to Return a JSON Response from a Java Servlet 1 Create a Maven Web Application. Let's create a simple maven web application in eclipse IDE using https://www. 2 Add Maven Dependencies. 3 Creating a Java POJO Entity - User.java. 4 Creating Servlet and Return JSON Response - UserServlet.java. 5 Demo. More ...

How to send data from Gson to JSP in Java?

We send this data to jsp using Gson Object. The method gson.toJson (student); is converting object to json string then we are passing json data to PrintWriter object to write in browser This class contains student data like id, name , gender ,address, mobile number and multiple subjects mark.

How to convert an object to JSON in servlet?

4. Creating Servlet and Return JSON Response - UserServlet.java A quick sample for converting an object to JSON representation with Gson would be: String employeeJsonString = new Gson (). toJson (employee); For producing a JSON response the content type should be application/json: 5. Demo


1 Answers

Did you try to invoke the page yourself from a web browser? Is the output what you expected? Also, use Firebug or Chrome Debugger to inspect the response headers/payload and verify that everything is correct.

Update I think I nailed it - take that damned semi-colon away.

like image 137
Alessandro Santini Avatar answered Oct 03 '22 18:10

Alessandro Santini