基于IDEA和Tomcat的 Ajax— —Servlet 交互

AJAX引擎工作原理

在这里插入图片描述

环境:

IDE:IntelliJ IDEA 2019.2.4 x64
服务器:Tomcat 8.5.60

代码:

JSP

<%--
  Created by IntelliJ IDEA.
  User: Admini
  Date: 2020/12/19
  Time: 19:44
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
</head>
<body>
<form>
    <span>学号:</span><input type="text" id="sno" name="sno"/>
    <input type="button" id="btn" value="查询"/>
</form>
<div id="res"></div>
</body>
<script type="text/javascript">
    function GetXmlHttpObject() {
        var XMLHttp=null;
        try{ XMLHttp=new XMLHttpRequest(); }
        catch(e){ try{ XMLHttp=new ActiveXObject("Msxml2.XMLHTTP"); }
        catch(e){ XMLHttp=new ActiveXObject("Microsoft.XMLHTTP"); }
        }
        return XMLHttp;
    }

    function requestFun(){
       var XMLHttp=GetXmlHttpObject();

       var sno=document.getElementById("sno").value;
       var url="${pageContext.request.contextPath}/queryServlet";                       //添加动态前缀
       var postStr="sno="+sno;

       XMLHttp.open("POST",url,true);
       XMLHttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded");    //设置头信息
       XMLHttp.send(postStr);                                                           //向服务器发送HTTP请求(内容-参数)
       XMLHttp.onreadystatechange=function(){                                           //定义响应处理函数
           // document.getElementById("res").innerText=XMLHttp.responseText;
           document.getElementById("res").innerText=this.responseText;
       }

    }

    document.getElementById("btn").onclick=requestFun;

</script>
</html>

servlet

package servlet;

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

@WebServlet(name = "QueryServlet",urlPatterns = {"/queryServlet"})
public class QueryServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        request.setCharacterEncoding("UTF-8");
        response.setCharacterEncoding("utf-8");
        response.setContentType("text/html;charset=utf-8");

        String sno=request.getParameter("sno");
        String res="hello,"+sno;
        System.out.println(sno);
        if(sno!=null){
            response.getWriter().write(res);
        }
        else
            response.getWriter().write("学号为空");
    }


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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值