后端写页面访问后端数据
1,新建文件ajax.jsp,在里面写入:
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!--以上代码从index。jsp复制过来 -->
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<script src="js/jquery-3.1.1.min.js"></script>
</head>
<body>
<div>
1111111111
</div>
<script>
$(function () {
var url = '<%=basePath%>';
//basepath从index。jsp复制过来
$.get(url+"/servlet/MyServlet",function (data,status) {
$("div").text(data);//text会把原来的文本替换为MyServlet里的内容
})
})
</script>
</body>
</html>
2,
访问http://localhost:8080/jquery/ajax.jsp,页面会显示hello,而不是ajax.jsp里面的内容