JQuery中的 ajax 简单应用

jsp中的demo

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    
    <title>jQ ajax</title>
    
	<meta http-equiv="pragma" content="no-cache">
	<meta http-equiv="cache-control" content="no-cache">
	<meta http-equiv="expires" content="0">    
	<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
	<meta http-equiv="description" content="This is my page">
	<!--
	<link rel="stylesheet" type="text/css" href="styles.css">
	-->
	<script type="text/javascript" src="http://ajax.microsoft.com/ajax/jquery/jquery-1.4.min.js"></script>
    <script>
       $(function(){
    	   $("#but").click(function(){
    		  var sdata=$("form").serialize();    //把表单序列化
    		   var ajaxurl="aja.do?"+sdata; 
    		   $.get(ajaxurl,function(data){      //ajax的get方法
    			   $("input[name=res]").val(data);
    		   });
    	   });
       });
    </script>
  </head>
  
  <body>
    <form name="form1">
        <input type="text" name="num1"/>+
        <input type="text" name="num2"/>=
        <input type="text" name="res"/>
        <input type="button" id="but" />
    </form>
  </body>
</html>
java

package ajaxApply;

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

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

@WebServlet(name="aa",urlPatterns="/aja.do")
public class ajaxApply extends HttpServlet{
	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		req.setCharacterEncoding("utf-8");
		String n1=req.getParameter("num1");
		String n2=req.getParameter("num2");
		Integer in1 = new Integer(n1);        //转换类型
		Integer in2 = new Integer(n2);
		int res = in1+in2;
		PrintWriter pw=resp.getWriter();
		pw.print(res);
	}
	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		doGet(req,resp);

	}

}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值