AJAX在浏览器当中发送异步请求,请求之间是独立的,谁也不用等谁
类似于多线程并发,不会出现页面清空然后展示新的页面的效果
实现局部刷新(靠多种技术一起做到的,而不是一个技术叫ajax)
它是异步的(类似于多线程)
准备一个前端页面用来发送传统的请求方式
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<h3><a href="/OA/old/request">跳转页面</a></h3>
<form action="/OA/old/request" method="post">
<input type="submit" value="post提交">
</form>
<input type="button" value="提交script请求" onclick="sendRequest()">
<script type="text/javascript">
function sendRequest()
{
// window.location.href="/OA/old/request"
document.location.href="/OA/old/request"
}
</script>
</body>
</html>
准备一个后端Servlet来接受请求信息
package com.bjpowernode.javaweb.servlet;
import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
@WebServlet("/old/request")
public class oldRequest extends HttpServlet {
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//响应信息到浏览器
request.setCharacterEncoding("UTF-8");
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
//向浏览器响应信息
out.println("<h1>欢迎来到ajax</h1");
}
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
request.setCharacterEncoding("UTF-8");
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
//向浏览器响应信息
out.println("<h1>欢迎来到ajax</h1");
}
}
标签:old,jakarta,request,计算机网络,AJAX,------,import,servlet,页面
From: https://blog.51cto.com/u_16322355/8119456