Filter拦截器
- 精准拦截:/index.jsp:只有访问index.jsp时才会被拦截
- 目录拦截:/user/*:访问/user下的所有资源,都会被拦截
- 后缀名拦截:*.jsp:访问后缀名为jsp的资源,都会被拦截
- 拦截所有:/*:访问所有资源,都会被拦截
Ajax
异步的js和xml
1、通过AJAX可以给服务器发送请求,服务器将数据直接响应回给浏览器
2、在不更新网页的情况下,与服务器进行数据交换
Axios
简化ajax的书写
使用方法:
第一步:引入 axios 的 js 文件
<script src="js/axios-0.18.0.js"></script>
第二步:使用axios 发送请求,并获取响应结果
function loadDoc() {
//get请求
axios({
method: "get",
url: "/day12-filter-axios-case/ajaxServlet",
data: "username=man"
}).then(function (resp) {
// console.log(resp);
console.log(resp.data);
// alert(resp.data)
})
//post请求
axios({
method: "post",
url: "/day12-filter-axios-case/ajaxServlet",
data: "username=man"
}).then(function (resp) {
// console.log(resp);
console.log(resp.data);
// alert(resp.data)
})
}
get请求和post请求区别有两点:
1:get请求参数是params,而post请求的参数是data。
2:get请求的参数在发送请求时会拼接到url后面,而post的参数再请求体中。
简写get请求和post的请求
1.如果只有一个参数,可以省略()小括号
2.如果方法体只有一行代码,可以省略{},retunrn,分号
细节一:访问路径
如果是在webapp目录下,那么访问页面时所在的目录为虚拟目录
细节二:箭头函数代理匿名函数
类似lambda表达式
JSON
javaScript对象表示法,存储或者交换文本信息的语法
基本格式
var 变量名 = '{"key":value,"key":value,...}';
var 变量名 = '{"key"=value&"key"=value,...}';
JSON
串的键要求必须使用双引号括起来,而值根据要表示的类型确定。value 的数据类型分为如下
- 数字(整数或浮点数)
- 字符串(使用双引号括起来)
- 逻辑值(true或者false)
- 数组(在方括号中)
- 对象(在花括号中)
- null
示例:
var jsonStr = '{"name":"zhangsan","age":23,"addr":["北京","上海","西安"]}'
//前提:添加jackson-databind依赖
//json字符串转java对象 后端接受
//格式1:var 变量名 = '{"key"=value&"key"=value,...}';
request.getParameter("");//单个
request.getParameterMap("");//多个
//格式2:var 变量名 = '{key:value,key:value,...}';
ObjectMapper objectMapper=new ObjectMapper();
objectMapper.readValue(request.geteader(),类型.class;
//java对象转json字符串 响应数据
ObjectMapper objectMapper=new ObjectMapper();
String json=objectMapper.writeValue(response.getWriter().write().java对象);