目录
Listen
1.概念:Listener表示监听器,是JavaWeb三大组件(Servlet、Filter、Listener)之一
2.监听器可以监听就是在application,session,request三个对象创建、销毁或者往其中添加修改删除属性时自动执行代码的功能组件
3.Listener分类:JavaWeb中提供了8个监听器
4.ServletContextListener使用
@WebListener
public class Listener implements ServletContextListener {
@Override
public void contextInitialized(ServletContextEvent sce) {
/* This method is called when the servlet context is initialized(when the Web application is deployed). */
System.out.println("ServletContextListener...");
}
@Override
public void contextDestroyed(ServletContextEvent sce) {
/* This method is called when the servlet Context is undeployed or Application Server shuts down. */
}
}
---------------------------------------------------------------------------------------------------------------------------------
AJAX
一:简介
1.概念:AJAX(Asynchronous JavaScript And XML):异步的JavaScript的XML
2.AJAX作用:
①与服务器进行数据交换:通过AJAX可以给服务器发送请求,并获取服务器响应的数据
使用了AJAX和服务器进行通信,就可以使用HTML+AJAX来替换JSP页面了
②异步交互:可以在不重新加载整个页面的情况下,与服务器交换数据并更新部分网页的技术,如:搜索联想、用户名是否可用校验,等等...
3.同步和异步:
二:AJAX快速入门
<body>
<script>
var xmlhttp;
if (window.XMLHttpRequest){
xmlhttp = new XMLHttpRequest();
}else {
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.open("GET", "http://localhost:8080/JSP/AjaxServlet");
xmlhttp.send();
xmlhttp.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
alert(xmlhttp.responseText);
}
}
</script>
</body>
三:Axios快速入门
<body>
<script src="js/axios-0.18.0.js"></script>
<script>
axios({
method:"get",
url:"http://localhost:8080/JSP/AxiosServlet?username=zhangsan"
}).then(function (resp){
alert(resp.data);
})
axios({
method:"post",
url:"http://localhost:8080/JSP/AxiosServlet",
data:"username=zhangsan"
}).then(function (resp){
alert(resp.data);
})
</script>
</body>
四:Axios请求方式别名
为了方便起见,Axios已经为所有支持的请求方式提供了别名。
get(url):发起GET方式请求
post(url):发起POST方式请求
1.发送get请求
axios.get("http://localhost:8080/JSP/AxiosServlet?username=zhangsan").then(function (resp){
alert(resp.data);
})
2.发送post请求
axios.post("http://localhost:8080/JSP/AxiosServlet","username=zhangsan").then(function (resp){
alert(resp.data);
})
五:JSON
1.概念:JavaScript Object Notation。JavaScript对象表示法
2.由于其语法简单,层次结构鲜明,现多用于作为数据载体,在网络中进行数据传输
3.基础语法
①定义:
var 变量名 = {"key1":value1,
"key2":value2,
...
};
②示例:
<body>
<script>
//定义json
var json = {
"name":"zhangsan",
"age":23,
"address":["北京","上海","南京"]
}
//获取json
alert(json.name)
</script>
</body>
③获取数据:
变量名.key
json.name
④value的数据类型为:
(1)数字(整数或浮点数)
(2)字符串(在双引号中)
(3)逻辑值(true或false)
(4)数组(在方括号中)
(5)对象(在花括号中)
(6)null
4.JSON数据和Java对象转换
①请求数据:JSON字符串转换为Java对象
②响应数据:Java对象转为JSON字符串
③Fastjson是阿里巴巴提供的一个Java语言编写的高性能功能完善的JSON库,是目前Java语言中最快的JSON库,可以实现Java对象和JSON字符串的互相转换
④使用:
public static void main(String[] args) {
User user = new User();
user.setUsername("zhangsan");
user.setPassword("123");
user.setId(1);
String jsonString = JSON.toJSONString(user);
System.out.println(jsonString);
User user1 = JSON.parseObject("{\"id\":1,\"password\":\"123\",\"username\":\"zhangsan\"}", User.class);
System.out.println(user1);
}