title: How to simply return JSON from a JSP
body: <p>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)?</p>
  
  <pre><code>[
     {"label":"item 1", "value":"item 1", "id": 1},
     {"label":"item 2", "value":"item 2", "id": 2},
     {"label":"item 3", "value":"item 1", "id": 3}
  ];
  </code></pre>
  
  <p>I tried </p>
  
  <pre><code>&lt;%-- Set the content type header with the JSP directive --%&gt;
  &lt;%@ page contentType="application/json" %&gt;
  
  &lt;%-- Set the content disposition header --%&gt;
  &lt;%
     // Returns all employees (active and terminated) as json.
     response.setContentType("application/json");
     response.setHeader("Content-Disposition", "inline");
  %&gt;
  
  &lt;%@ page language="java"%&gt;
  &lt;%@ page import="java.sql.*"%&gt;
  &lt;%@ page import="java.util.*"%&gt;
  &lt;%@ page import="java.text.*"%&gt;
  &lt;%@ page import="javax.servlet.http.*"%&gt;
  &lt;%@ page import="oracle.apps.fnd.common.WebAppsContext"%&gt;
  &lt;%@ page import="oracle.apps.fnd.common.WebRequestUtil"%&gt;
  &lt;%@ page import="oracle.apps.fnd.common.ResourceStore"%&gt;
  &lt;%@ page import="oracle.apps.fnd.common.VersionInfo"%&gt;
  
  [
     {"label":"item 1", "value":"item 1", "id": 1},
     {"label":"item 2", "value":"item 2", "id": 2},
     {"label":"item 3", "value":"item 1", "id": 3}
  ];
  </code></pre>
  
  <p>but it does not seem to work, since my jquery autocomplete does not work with it.</p>
  
  <p>Here's part of the autocomplete code:</p>
  
  <pre><code>&lt;html&gt;
  &lt;head&gt;
        $(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;
           }
        }
  
     &lt;/script&gt;
  &lt;/head&gt;
  &lt;body class='fdlbod'&gt;
     &lt;div style='padding-left : 20px'&gt;
        &lt;textarea id="holdtext" style="display:none;"&gt;&lt;/textarea&gt;
        &lt;form id="autocomplete" name="autocomplete"&gt;
  &lt;%
        out.println("Customer Name:&amp;nbsp;");
        out.println("&lt;input type='text' value='' name='cust_input' id='cust_input' size='80' onkeypress='handleKeyPress(event,this.form)' /&gt;");
  %&gt;
        &lt;/form&gt;
     &lt;/div&gt;
  &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>