jquery利用ajax方法调用远程 的对象

两数相加:

ajax.jsp

<%@ page language="java" import="java.util.*" pageEncoding="ISO-8859-1"%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">

<html>

<head>

<script type="text/javascript" src="scripts/jquery-1.3.1.js"></script>

<script>

  $(function(){

  $("#button").click(function(){

  $.ajax({

  type:"post",

  url:"MyServlet",

  dateType:"html",

  data:{"param1":$("#param1").val(),"param2":$("#param2").val()},

  success:function(returnedData){

  $("#result").val(returnedData);

  }

  });

  })

  })

  </script>

</head>

<body>

<input type="text" id="param1"></input>+

<input type="text" id="param2"></input>=

<input type="text" id="result"></input>

<input type="button" value="get" />

</body>

</html>

处理的servlet

package com.sun;

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;

public class MyServlet extends HttpServlet {

public void doPost(HttpServletRequest request, HttpServletResponse response)

throws ServletException, IOException {

int param1=Integer.parseInt(request.getParameter("param1"));

int param2=Integer.parseInt(request.getParameter("param2"));

response.setHeader("pragma", "no-cache");

response.setHeader("cache-control", "no-cache");

PrintWriter out = response.getWriter();

out.println(String.valueOf(param1+param2));

out.flush();

out.close();

}

}