生活中的会话:例子:打电话 ==> 拨号 ==>等待接通 ==>接通 ==>聊天 ==>挂断
开发中的会话:浏览器与服务器进行多次交互 多次请求与响应
Cookie是客户端会话对象
开始:随着浏览器打开发送第一次请求,表示会话开始
结束:随着浏览器的关闭 表示会话结束
用于客户端与服务器进行数据传输
数据的共享
Cookie 与 Session

Cookie一般与Session搭配使用
Cookie用于储存用户的信息(不是很重要的信息) 保存到浏览器中
A.Cookie 是以键值对来进行存储
B.Cookie 存储数据的长度不能超过4kb
C.Cookie默认的生命周期 是随着浏览器的开始而开始 随着浏览器关闭而关闭
D.Cookie是将数据保存在浏览器中 存储的都是一些不重要的信息






需求:

- <%--
- Created by IntelliJ IDEA.
- User: 86182
- Date: 2022/9/6
- Time: 9:45
- To change this template use File | Settings | File Templates.
- --%>
- <%@ page contentType="text/html;charset=UTF-8" language="java" %>
- <html>
- <head>
- <title>$Title$title>
- head>
- <body>
- <%
- //获取cookie数据
- Cookie[] cookies = request.getCookies();
- //对数组进行非空验证
- if (cookies != null && cookies.length > 0){
- for (Cookie c : cookies){
- if ("uname".equals(c.getName())){
- pageContext.setAttribute("uname",c.getValue());
- }else if ("upwd".equals(c.getName())){
- pageContext.setAttribute("upwd",c.getValue());
- }
- }
- }
- %>
- <form action="${pageContext.request.contextPath}/userServlet" method="post">
- <p>
- 用户名:<input type="text" name="uname" value="${uname}">
- p>
- <p>
- 密码:<input type="password" name="upwd" value="${upwd}">
- p>
- <p>
- <input type="submit" value="提交">
- p>
- form>
- body>
- html>
userServlet类:
- package com.qf.servlet;
-
- 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;
-
- @WebServlet(name = "userServlet",urlPatterns = "/userServlet")
- public class UserServlet extends HttpServlet {
- @Override
- protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
- //获取前端传输的数据
- String uname = req.getParameter("uname");
- String upwd = req.getParameter("upwd");
- //判断是否登录成功
- if ("admin".equals(uname) && "123456".equals(upwd)){
- //将数据保存到cookie中
- Cookie cookieName = new Cookie("uname",uname);
- //设置cookie保存时间 秒为单位
- cookieName.setMaxAge(60*60);
- Cookie cookieUpwd = new Cookie("upwd",upwd);
- cookieUpwd.setMaxAge(60*60);
- //将cookie对象添加到响应对象中
- resp.addCookie(cookieName);
- resp.addCookie(cookieUpwd);
- }else {
- //登录失败 重新跳转到登录页面
- req.getRequestDispatcher("index.jsp").forward(req,resp);
- }
- }
- }
Session是服务器会话对象
Session是四大作用域对象
Sesssion的唯一标识是sessionId 这个session保存在Cookie中 每次重新打开浏览器 都会创建一个唯一的sessionId
Session与Cookie搭配使用








需求:

a.jsp
- <%--
- Created by IntelliJ IDEA.
- User: 86182
- Date: 2022/9/6
- Time: 11:27
- To change this template use File | Settings | File Templates.
- --%>
- <%@ page contentType="text/html;charset=UTF-8" language="java" %>
- <html>
- <head>
- <title>Titletitle>
- head>
- <body>
- <form action="${pageContext.request.contextPath}/demoServlet" method="post">
- <p style="color: red">
- ${msg}
- p>
- <p>
- 用户名:<input type="text" name="uname">
- p>
- <p>
- 密码:<input type="password" name="upwd">
- p>
- <p>
- <input type="submit" value="登录">
- p>
- form>
- body>
- html>
b.jsp
- <%--
- Created by IntelliJ IDEA.
- User: 86182
- Date: 2022/9/6
- Time: 11:33
- To change this template use File | Settings | File Templates.
- --%>
- <%@ page contentType="text/html;charset=UTF-8" language="java" %>
- <html>
- <head>
- <title>Titletitle>
- head>
- <body>
- <%
- //验证session是否为空
- Object uname = session.getAttribute("uname");
- if (uname==null||"".equals(uname)){
- //提示信息
- request.setAttribute("msg","非法登录,小心腿打断");
- //转发
- request.getRequestDispatcher("a.jsp").forward(request,response);
- return;
- }
- %>
- <h1>欢迎来到VIP洗脚城,用户:${uname}h1>
- body>
- html>
- package com.qf.servlet;
-
- import javax.servlet.ServletException;
- import javax.servlet.annotation.WebServlet;
- import javax.servlet.http.HttpServlet;
- import javax.servlet.http.HttpServletRequest;
- import javax.servlet.http.HttpServletResponse;
- import javax.servlet.http.HttpSession;
- import java.io.IOException;
- @WebServlet(name = "demoServlet",urlPatterns = "/demoServlet")
- public class DemoServlet extends HttpServlet {
- @Override
- protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
- //获取前端传递的数据
- String uname = req.getParameter("uname");
- String upwd = req.getParameter("upwd");
- //验证是否登录成功
- if ("admin".equals(uname)&&"123456".equals(upwd)){
- //将数据保存到session中
- HttpSession session = req.getSession();
- session.setAttribute("uname",uname);
- //重定向
- resp.sendRedirect("b.jsp");
- }else {
- //登录失败
- req.setAttribute("msg","登录失败");
- req.getRequestDispatcher("a.jsp").forward(req,resp);
- }
- }
- }
date ==> 年 月 日
time ==> 时 分 秒
datetime ==> 年 月 日 时 分 秒
1. Date Calendar
2.对时间格式化类 SimpleDateFormat
Date parse(String text) ==> 将字符串转换为
Date public StringBuffer format(Date date) ==> 将Date转换为指定格式的字符串
My97日期控件 下载 & 更新日志 My97Datepicker Download & Changelog
step01 下载资源 并解压

step02 copy素材到web目录下

step03 在页面中使用
- <%--
- Created by IntelliJ IDEA.
- User: 86182
- Date: 2022/9/6
- Time: 14:37
- To change this template use File | Settings | File Templates.
- --%>
- <%@ page contentType="text/html;charset=UTF-8" language="java" %>
- <html>
- <head>
- <title>Titletitle>
- <script src="${pageContext.request.contextPath}/My97DatePicker/jquery-1.8.3.js">script>
- <script src="${pageContext.request.contextPath}/My97DatePicker/WdatePicker.js">script>
- head>
- <body>
- <input type="text" id="d241" onclick="WdatePicker({dateFmt:'yyyy-MM-dd HH:mm:ss'})" class="Wdate" style="width:300px"/>
- body>
- html>


step01 修改配置文件server.xml


- req.setCharacterEncoding("UTF-8");
- resp.setContentType("text/html;charset=utf-8");
- resp.setCharacterEncoding("UTF-8");