title: Send post request to another server using ajax (cross-domain) and use jsp code as a proxy
body: <p>I need to get json data from the company server. The interface they give to me is to send a post request with the proper params. And then I will get json data.</p>
  
  <p>I use ajax to send a post request to local server and use jsp to send the post to the company server.
  and I put the .html and .jsp in the same file named test. Then put the test file in the webapps file under Tomcat.
  But all I receive is httpstatus 500 error
  I tried to send the post in the java program, and I got the right answer.</p>
  
  <p>here is my code.</p>
  
  <p>Is there anything wrong with the code, or with the configuration? I'm a newer in web development and some mistakes may be obvious, but I try so much and can't get what I want.</p>
  
  <pre><code>[code=JScript][/code]
  &lt;SCRIPT LANGUAGE="JavaScript" type="text/javascript"&gt;
  function loadJson()
  {
  var xmlhttp = false;
  if (window.XMLHttpRequest)
    {// code for IE7+, Firefox, Chrome, Opera, Safari
    xmlhttp=new XMLHttpRequest();
    if (XMLHttpRequest.overrideMimeType)
    {
    XMLHttpRequest.overrideMimeType("text/xml");
    }
    }
  else
    {// code for IE6, IE5
    xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
    }
  if(!xmlhttp)
   {
  window.alert("can't create object");
  return false;
  }   
  xmlhttp.onreadystatechange=function()
    {
    if (xmlhttp.readyState==4 &amp;&amp; xmlhttp.status==200)
    {
    document.getElementById("myDiv").innerHTML=xmlhttp.responseText;
    }
    else
  document.getElementById("myDiv").innerHTML=xmlhttp.status+"-"+xmlhttp.readyState;
    }
  xmlhttp.open("POST","proxy.jsp",true);
  xmlhttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded");   
    xmlhttp.send("url=http://aqueduc.kelcode.com/proc/gw.php&amp;requestName=getIntervention&amp;uid=UID_GATEWAY&amp;startDate=1292223600&amp;endDate=1408330000");
  }
  &lt;/script&gt;
  
  [code=HTML][/code]
  &lt;!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"         "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"&gt;
  &lt;html xmlns="http://www.w3.org/1999/xhtml"&gt;
  &lt;head&gt;
  &lt;meta http-equiv="Content-Type" content="text/html; charset=utf-8" /&gt;
  &lt;title&gt;test&lt;/title&gt;
  &lt;/head&gt;
  &lt;body&gt;
  &lt;button type="button" onclick="loadJson()"&gt;request data&lt;/button&gt;
  &lt;/body&gt;
  &lt;/html&gt;
  
  proxy.jsp
  [code=Java][/code]
  &lt;%@ page import="java.net.*,java.util.*,java.lang.*,java.io.*"%&gt;
  &lt;%@ page contentType="text/xml;charset=gb2312"%&gt;
  &lt;%   
  String url = null;
  StringBuffer params = new StringBuffer();
  Enumeration enu = request.getParameterNames();
  int total = 0;
  while (enu.hasMoreElements()) {
  String paramName=(String)enu.nextElement();
  if(paramName.equals("url")){
    url=request.getParameter(paramName);
  }else{
    if(total==0){
        params.append(paramName).append("=").append(URLEncoder.encode(request.getParameter(paramName), "UTF-8"));
    } else {
    params.append("&amp;").append(paramName).append("=").append(URLEncoder.encode(request.getParameter(paramName), "UTF-8"));
    }
    ++total;
  }
  }
  String para = params.toString();
  //out.println(url);
  if(url != null){
  // POST request
  URL connect = new URL(url.toString());
  URLConnection connection = connect.openConnection();
  connection.setDoOutput(true);
  connection.setDoInput(true);
  connection.setRequestMethod("POST");
  connection.setRequestProperty("Content-type","application/x-www-form-urlencoded");
  connection.connect();
  DataOutputStream os = new DataOutputStream(connnection.getOutputStream());
  os.write("requestName=getIntervention&amp;uid=UID_GATEWAY&amp;startDate=1292223600&amp;endDate=1408330000");
  os.flush();
  os.close();
  
  BufferedReader reader = new BufferedReader(new  InputStreamReader(connection.getInputStream()));
  String line;
  while((line = reader.readLine()) != null){
    out.println(line);
  }
  reader.close();
  }
  %&gt;
  </code></pre>