目录
Cookie
1.概念:客户端会话技术,将数据保存到客户端
2.快速入门:
3.实现原理
5. Cookie的特点和作用
例子:
6.案例:记住上一次访问时间
Session
1.概念:服务器端会话技术,在一-次会话的多次请求间共享数据,将数据保存在服务器端的对象中。HttpSession
2.快速入门:
3.原理
4.细节:
5. session的特点
AJAX
1.概念: ASynchronous JavaScript And XML
2.实现方式
JSON
概念: JavaScript object Notation
JSON语法
获取数据
JSON数据与Java对象互相转换
基于响应头set- cooki e和请求头cookie实现
1.一次可不可以发送多个cookie?
2. cookie在 浏览器中保存多长时间?
3. cookie能不能存中文?
4.,cookie获取范围多大?
1. cookie存储数据在客户端浏览器
2.浏览器对于单个cookie的大小有限制(4kb)以及对同-个域名下的总cookie数量也有限制(20个)
作用:
@WebServlet("/servletCookie")
public class ServletCookie extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//1.创建cookie对象
Cookie cookie = new Cookie("msg","hello#发送了什么");
//2.发送cookie
response.addCookie(cookie);
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
this.doPost(request,response);
}
}
@WebServlet("/servletCookie2")
public class ServletCookie2 extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//3.获取cookie
Cookie[] cookies = request.getCookies();
//获取数据,遍历cookie
if(cookies != null){
for(Cookie item : cookies){
String name = item.getName();
String con = item.getValue();
System.out.println(name+":"+con);
}
}
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
this.doPost(request, response);
}
}
1.需求:
2.分析:
Servlet代码:
package cn.cast.cookie;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
@WebServlet("/cookieTest")
public class CookieTest extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//设置响应的消息体的数据格式和编码
response.setContentType("text/html;charset=utf-8");
//获取所有cookie
Cookie[] cookies = request.getCookies();
boolean flag = false; //没有cookie为last time
//遍历cookies数组
if(cookies != null && cookies.length > 0){
for(Cookie cookie : cookies){
String name = cookie.getName();
//判断cookie名称是否是lasttime
if("lasttime".equals(name)){
flag = true;
//不是第一次访问,响应数据
//设置cookie的value
//获取当前时间的字符串,重新设置Cookie的值,重新发送cookie
Date date = new Date();
SimpleDateFormat sdf = new SimpleDateFormat( "yyy年M月dd日HH:mm:ss");
String str_date = sdf.format (date);
cookie. setValue(str_date);
//设置cookie的存活时间
// cookie.setMaxAge(60 * 60 * 24 * 30);//一个月
response.addCookie(cookie);
//获取cookie数据
String value = cookie.getValue();
response.getWriter().write("欢迎回来,您上次访问时间为:"+value+"
");
//跳出循环
break;
}
}
}
if(cookies == null || cookies.length == 0 || flag == false){
//设置cookie的value
//获取当前时间的字符串,重新设置Cookie的值,重新发送cookie
Date date = new Date();
SimpleDateFormat sdf = new SimpleDateFormat( "yyy年M月dd日HH:mm:ss");
String str_date = sdf.format (date);
Cookie cookie = new Cookie("lasttime",str_date);
//设置cookie的存活时间
cookie.setMaxAge(60 * 60 * 24 * 30);//一个月
response.addCookie(cookie);
response.getWriter().write("您好,这是您第一次访问,您本次访问时间为:"+str_date+"
");
}
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
this.doPost(request, response);
}
}
效果:
session是依赖于cookie的
1.当客户端关闭后,服务器不关闭,两次获取session是否为同一个?
Cookie c = new Cookie("JSESSIONID" , session.getId());
c.setMaxAge(60*60);
response.addCookie(c);
2.客户端不关闭,服务器关闭后,两次获取的session是同- -个吗?
3. session什么时候被销毁?
1. session用于存储一次会话的多次请求的数据,存在服务器端
2. session可以存储任意类型,任意大小的数据
session与cookie的区别: .
@WebServlet("/Sessiondemo")
public class Sessiondemo extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//1.获取session
HttpSession session = request.getSession();
System. out . println(session);
//期望客户端关闭后,session也能相同
Cookie c = new Cookie("JSESSIONID" , session.getId());
c.setMaxAge(60*60);
response.addCookie(c);
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
this.doPost(request, response);
}
}
异步的JavaScript和XML
Ajax是一种在无需重新加载整个网页的情况下,能够更新部分网页的技术。
提升用户的体验
1.原生的JS实现方式(了解)
function loadXMLDoc()
{
var xmlhttp;
if (window.XMLHttpRequest)
{
// IE7+, Firefox, Chrome, Opera, Safari 浏览器执行代码
xmlhttp=new XMLHttpRequest();
}
else
{
// IE6, IE5 浏览器执行代码
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
//true异步请求,false同步请求
xmlhttp.open("GET","ajaxServlet?name=t854",true);
xmlhttp.send();
//接收并处理来自服务器的响应结果
xmlhttp.onreadystatechange=function()
{
if (xmlhttp.readyState==4 && xmlhttp.status==200)
{
//获取响应
var responseText = xmlhttp.responseText;
console.log(responseText)
// document.getElementById("myDiv").innerHTML=xmlhttp.responseText;
}
}
}
@WebServlet("/ajaxServlet")
public class AjaxServlet extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
ArrayList list= new ArrayList();
list.add("aaa");
list.add("bbb");
list.add("ccc");
//获取请求参数
String name = request.getParameter("name");
//打印name
System.out.println(name+"**");
//响应
// response.getWriter().write("hello :" + name);
response.getWriter().write("hello :" + list);
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
this.doPost(request,response);
}
}
2. JQeury实现方式
JavaScript对象表示法
JSON是存储和交换文本信息的语法。类似XML。
JSON比XML更小、更快,更易解析。
键用引号(单双都行)引起来,也可以不使用引号
值得取值类型:
{
"book": [
{
"id":"01",
"language": "Java",
"edition": "third",
"author": "Herbert Schildt"
},
{
"id":"07",
"language": "C++",
"edition": "second"
"author": "E.Balagurusamy"
}]
}
1. json对象.键名
2. json对象[ "键名"]
3.数组对象[索引]
JSON解析器
Java对象转json字符串
JSON字符串转为Java对象
注解:
1. @JsonIgnore :排除属性。
2. @JsonFormat :属性值得格式化
需要的Jackson包
下载jar地址:https://mvnrepository.com/artifact/com.fasterxml.jackson.core
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.junit.Test;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
public class JacksonTest {
//java对象转为json字符串
@Test
public void test1() throws IOException {
//1.创建person对象
Pserson s1 = new Pserson("曹操",25,true);
Pserson s2 = new Pserson("刘备",35,true);
Pserson s3 = new Pserson("孙权",21,true);
ArrayList list = new ArrayList<>();
list.add(s1);
list.add(s2);
list.add(s3);
//2.创建jackson核心对象 ObjectMapper
ObjectMapper mapper = new ObjectMapper();
//3.转换
/*
转换方法:
writeValue
writeValueAsString(obj) :将对象转为json字符串
* */
String json1 = mapper.writeValueAsString(list);
System.out.println(json1);
//writeValue,将数据写到d://a. txt文件中
// mapper.writeValue(new File("d://a.txt"),json1);
}
}
输出结果为json形式的字符串
一起学习,一起进步 -.- ,如有错误,可以发评论