ajax+json

来源:互联网 发布:网络剧十宗罪疑点 编辑:程序博客网 时间:2024/05/24 03:19

<%@ page language="java" pageEncoding="utf-8"%>

<html>
  <head> 
    <title>Ajax + json</title>
    <script language="javascript" src="json.js"></script>
</head>
 
  <body>
    <input type="text" value="funid" id="funid"><br>
    <input type="text" value="serviceid" id="serviceid"><br>
    <select id="neid">
     <option value="107196197">107196197</option>
     <option value="107196198">107196198</option>
     <option value="107196197">107196199</option>
    </select><br>
   <input type="button" value="ajax test" onclick="myClick()">
    <table>
     <tr>
      <td>
       <div id="result"></div>
      </td>
     </tr>
    </table>
  </body>
</html>

<script type="text/javascript">
<!--
var xmlrequest;

function createXMLHttpRequest()
{
 if(window.XMLHttpRequest)
 {
  xmlrequest = new XMLHttpRequest();
 }
 else if(window.ActiveXObject)
 {
  try
  {
   xmlrequest = new ActiveXObject("Msxml2.XMLHTTP");
  }
  catch(e)
  {
  }
 }
}

//发送请求
function myClick()
{
 createXMLHttpRequest();
 var param = createParam();
 var uri = "servlet/Operation";
 //escape()
 xmlrequest.open("POST", uri, true);
 xmlrequest.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
 xmlrequest.onreadystatechange = praseResult;
 alert("begin");
 xmlrequest.send(param);
}


//回调函数
function praseResult()
{
 if(xmlrequest.readyState == 4)
 {
  if(xmlrequest.status == 200)
  {
   var data = xmlrequest.responseText;//eval();
   document.getElementById("result").innerHTML = data; 
  }
 }

}
//自定义对象
function Info(funid, neid, serviceid)
{
 this.funid = funid;
 this.neid = neid;
 this.serviceid = serviceid;
}

function createParam()
{
 var funid = document.getElementById("funid").value;
 var neid = document.getElementById("neid").value;
 var serviceid = document.getElementById("serviceid").value;
 
 //
 var info = new Info(funid, neid, serviceid);
 //alert(info.toJSONString());
 return JSON.stringify(info);
 
 //构建JSON格式的参数
 //var param = {funid:"kk123", neid:"107196197", serviceid:"dbproxy"};
 //alert(param);
 //return param;
}


//-->
</script>

 

package com.huawei.usau;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.json.JSONException;
import org.json.JSONObject;

public class Operation extends HttpServlet {

 /**
  * The doGet method of the servlet. <br>
  *
  * This method is called when a form has its tag value method equals to get.
  *
  * @param request the request send by the client to the server
  * @param response the response send by the server to the client
  * @throws ServletException if an error occurred
  * @throws IOException if an error occurred
  */
 public void doGet(HttpServletRequest request, HttpServletResponse response)
   throws ServletException, IOException {
  doPost(request, response);
 }

 /**
  * The doPost method of the servlet. <br>
  *
  * This method is called when a form has its tag value method equals to post.
  *
  * @param request the request send by the client to the server
  * @param response the response send by the server to the client
  * @throws ServletException if an error occurred
  * @throws IOException if an error occurred
  */
 public void doPost(HttpServletRequest request, HttpServletResponse response)
   throws ServletException, IOException {
  
  JSONObject jsonObject = null;
  String responseText = null;
  String json = readJSONString(request);
  
  try
  {
   jsonObject = new JSONObject(json);
   //jsonObject = JSONObject.fromObject(json);
   responseText = "账号 " + jsonObject.getString("funid") + " 名称 " + jsonObject.getString("neid") + " 余额 " + jsonObject.getString("serviceid");

  }
  catch (JSONException e)
  {
   e.printStackTrace();
  }
  response.setCharacterEncoding("UTF-8");
  response.setContentType("text/xml");
  
  PrintWriter out = response.getWriter();
  out.print(responseText);
  out.flush();
  out.close();
  
  /*//////////////////////////////////////////////////////
  String param = (String) request.getParameter("param");
  JSONObject jSONObject = new JSONObject();

  response.setContentType("text/html");
  PrintWriter out = response.getWriter();
  out.print("    This is ");
  out.print(this.getClass());
  out.println(param);
  out.println(", using the POST method");
  //out.println(param);
  out.flush();
  out.close();
  */
 }

 /**
  * 获取客户端传递过来的JSON数据
  * @param request
  * @return
  */
 private String readJSONString(HttpServletRequest request)
 {
  StringBuffer json = new StringBuffer();
  String line = null;

  try
  {
   BufferedReader reader = request.getReader();
   while((line = reader.readLine()) != null)
   {
    json.append(line);
   }
  }
  catch(Exception e)
  {
   System.out.println(e.toString());
  }

  return json.toString();
 }

}

原创粉丝点击