title: Java servlet calling (authorize.net) sample JSP code returns blank page
body: <p>I need to use Authorize.net AIM method to charge credit cards online. As a starting point, I've created a java servlet to call Authorize.net's sample JSP code. I'm a Java newbie and this is my first experience using JSP. </p>
  
  <p>The servlet compiles fine, but when I access the servlet in a browser, it returns a blank screen. I was expecting to see the output generated by sample.jsp (even though it's not a real transaction). Anyone know why it's not showing up?</p>
  
  <p>I'm guessing it's got something to do with the servlet using the variable <code>res</code> and the JSP file simply using <code>out</code> to write to the screen. Don't I need something like <code>PrintWriter out = res.getWriter();</code> in the JSP file? Or, how does variable <code>res</code> get passed into the JSP file such that the JSP file can write the browser, etc? I'm not clear on this. Can someone give me a sanity check?</p>
  
  <p>-------- Java Servlet -------</p>
  
  <pre><code>public class Payment extends HttpServlet {
  
    @Override
    public void init(ServletConfig config) throws ServletException {
      super.init(config);
    }
  
    protected void doGet(HttpServletRequest req, HttpServletResponse res)
      throws ServletException, IOException {
  
      try {
        // call sample.jsp
        req.getRequestDispatcher("/sample.jsp").include(req,res); 
  
      } catch (Exception e) {
          ...
      } finally {  
          ...
      }
    }
  }
  </code></pre>
  
  <p>-------- sample.jsp from Authorize.net; sits in same directory as Payment.class ---------</p>
  
  <pre><code>&lt;!-- This sample code is designed to connect to Authorize.net using the AIM method.
    For API documentation or additional sample code, please visit: http://developer.authorize.net  --&gt;
  
  &lt;!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"&gt;
  &lt;HTML lang='en'&gt;
  &lt;HEAD&gt;
      &lt;TITLE&gt; Sample AIM Implementation &lt;/TITLE&gt;
  &lt;/HEAD&gt;
  
  &lt;BODY&gt;
  &lt;P&gt; This sample code is designed to generate a post using Authorize.net
  Advanced Integration Method (AIM) and display the results of this post to
  the screen. &lt;/P&gt;
  &lt;P&gt; For details on how this is accomplished, please review the readme file,
  the comments in the sample code, and the Authorize.net AIM API documentation
  found at http://developer.authorize.net &lt;/P&gt;
  &lt;HR /&gt;
  
  &lt;%@ page import="java.util.*" %&gt;
  &lt;%@ page import="java.net.*" %&gt;
  &lt;%@ page import="java.io.*" %&gt;
  &lt;%@ page import="javax.net.ssl.*" %&gt;
  &lt;%@ page import="java.net.URLEncoder" %&gt;
  &lt;%
  
  // By default, this sample code is designed to post to our test server for
  // developer accounts: https://test.authorize.net/gateway/transact.dll
  // for real accounts (even in test mode), please make sure that you are
  // posting to: https://secure.authorize.net/gateway/transact.dll
  URL post_url = new URL("https://test.authorize.net/gateway/transact.dll");
  
  Hashtable post_values = new Hashtable();
  
  // the API Login ID and Transaction Key must be replaced with valid values
  post_values.put ("x_login", "API_LOGIN_ID");
  post_values.put ("x_tran_key", "TRANSACTION_KEY");
  
  post_values.put ("x_version", "3.1");
  post_values.put ("x_delim_data", "TRUE");
  post_values.put ("x_delim_char", "|");
  post_values.put ("x_relay_response", "FALSE");
  
  post_values.put ("x_type", "AUTH_CAPTURE");
  post_values.put ("x_method", "CC");
  post_values.put ("x_card_num", "4111111111111111");
  post_values.put ("x_exp_date", "0115");
  
  post_values.put ("x_amount", "19.99");
  post_values.put ("x_description", "Sample Transaction");
  
  post_values.put ("x_first_name", "John");
  post_values.put ("x_last_name", "Doe");
  post_values.put ("x_address", "1234 Street");
  post_values.put ("x_state", "WA");
  post_values.put ("x_zip", "98004");
  // Additional fields can be added here as outlined in the AIM integration
  // guide at: http://developer.authorize.net
  
  // This section takes the input fields and converts them to the proper format
  // for an http post.  For example: "x_login=username&amp;x_tran_key=a1B2c3D4"
  StringBuffer post_string = new StringBuffer();
  Enumeration keys = post_values.keys();
  while( keys.hasMoreElements() ) {
    String key = URLEncoder.encode(keys.nextElement().toString(),"UTF-8");
    String value = URLEncoder.encode(post_values.get(key).toString(),"UTF-8");
    post_string.append(key + "=" + value + "&amp;");
  }
  
  // The following section provides an example of how to add line item details to
  // the post string.  Because line items may consist of multiple values with the
  // same key/name, they cannot be simply added into the above array.
  //
  // This section is commented out by default.
  /*
  String[] line_items = {
      "item1&lt;|&gt;golf balls&lt;|&gt;&lt;|&gt;2&lt;|&gt;18.95&lt;|&gt;Y",
      "item2&lt;|&gt;golf bag&lt;|&gt;Wilson golf carry bag, red&lt;|&gt;1&lt;|&gt;39.99&lt;|&gt;Y",
      "item3&lt;|&gt;book&lt;|&gt;Golf for Dummies&lt;|&gt;1&lt;|&gt;21.99&lt;|&gt;Y"};
  
  for (int i = 0; i &lt; line_items.length; i++) {
    String value = line_items[i];
    post_string.append("&amp;x_line_item=" + URLEncoder.encode(value));
  }
  */
  
  // Open a URLConnection to the specified post url
  URLConnection connection = post_url.openConnection();
  connection.setDoOutput(true);
  connection.setUseCaches(false);
  
  // this line is not necessarily required but fixes a bug with some servers
  connection.setRequestProperty("Content-Type","application/x-www-form-urlencoded");
  
  // submit the post_string and close the connection
  DataOutputStream requestObject = new DataOutputStream( connection.getOutputStream() );
  requestObject.write(post_string.toString().getBytes());
  requestObject.flush();
  requestObject.close();
  
  // process and read the gateway response
  BufferedReader rawResponse = new BufferedReader(new InputStreamReader(connection.getInputStream()));
  String line;
  String responseData = rawResponse.readLine();
  rawResponse.close();                         // no more data
  
  // split the response into an array
  String [] responses = responseData.split("\\|");
  
  // The results are output to the screen in the form of an html numbered list.
  out.println("&lt;OL&gt;");
  for(Iterator iter=Arrays.asList(responses).iterator(); iter.hasNext();) {
      out.println("&lt;LI&gt;" + iter.next() + "&amp;nbsp;&lt;/LI&gt;");
  }
  out.println("&lt;/OL&gt;");
  // individual elements of the array could be accessed to read certain response
  // fields.  For example, response_array[0] would return the Response Code,
  // response_array[2] would return the Response Reason Code.
  // for a list of response fields, please review the AIM Implementation Guide
  %&gt;
  &lt;/BODY&gt;
  &lt;/HTML&gt;
  </code></pre>
  
  <p>UPDATE 1</p>
  
  <p>If I replace sample.jsp above with sampleJustHTML.jsp, it still gives a blank page.</p>
  
  <p>---------- sampleJustHTML.jsp -----------</p>
  
  <pre><code>&lt;!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"&gt;
  &lt;HTML lang='en'&gt;
  &lt;HEAD&gt;
      &lt;TITLE&gt; Sample AIM Implementation &lt;/TITLE&gt;
  &lt;/HEAD&gt;
  
  &lt;BODY&gt;
  &lt;P&gt; This sample code is designed to generate a post using Authorize.net
  Advanced Integration Method (AIM) and display the results of this post to
  the screen. &lt;/P&gt;
  
  &lt;/BODY&gt;
  &lt;/HTML&gt;
  </code></pre>