servlet,jsp使用ajax进行数据交互

本文介绍如何在网页中通过Ajax技术与Servlet和JSP进行数据交互,特别是在用户点击特定ID(如1)时,实现无刷新的数据更新。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
<script type="text/javascript" src="js/jquery-2.1.0.js"></script>
<script type="text/javascript">
    $(document).ready(function(){
        $(".getid").click(function(){
            var id = $(this).parent().parent().find(".id").text();//获取当前点击的所属的id
            alert(id);
            $.ajax({
                data:"post", //传输方式
                datatype:"text",//传输的数据类型
                url:"test",  //路径
                data:{
                    gettext:id//把所得到的id传给url是test的servlet
                },
                success:function(data,textStatus){
                    alert("查询成功!");//传输成功所要做的事情
                    //window.location.href = "/web/admin/detail/orderDetail.jsp";跳转页面
                }
            });
        })

    });
</script>
</head>
<body>
  <table>
    <tr>
        <th>id</th>
        <th>姓名</th>
        <th>电话</th>
        <th>年级</th>
    </tr>
    <tr>
        <td class="id" >1</td>
        <td>name</td>
        <td>18988658561</td>
        <td>高三</td>
        <td><button class="getid">查询</button></td>
    </tr>
    <tr>
        <td class="id">2</td>
        <td>大庆</td>
        <td>18988658561</td>
        <td>高二</td>
        <td><button class="getid">查询</button></td>
    </tr>

  </table>
</body>
</html>


这是servlet页面

public class TestAjax extends HttpServlet {

    private static final long serialVersionUID = -1522958472611476563L;

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doPost(request, response);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String getId = request.getParameter("senId");
        System.out.println("在ajax拿到的值是:"+getId);
        //request.getRequestDispatcher("index.jsp");//页面转发,建议在ajax success那使用
    }
}

这里写图片描述
当我点击一个id为1的时候
这里写图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值