• day15学习总结


    一、会话

    简介:

            生活中的会话:例子:打电话 ==> 拨号 ==>等待接通 ==>接通 ==>聊天 ==>挂断

            开发中的会话:浏览器与服务器进行多次交互 多次请求与响应

            Cookie是客户端会话对象

    会话的生命周期:

            开始:随着浏览器打开发送第一次请求,表示会话开始

            结束:随着浏览器的关闭 表示会话结束

    作用:

            用于客户端与服务器进行数据传输

            数据的共享

    会话技术:

            Cookie  与  Session

    二、Cookie会话

    简介:  

            Cookie一般与Session搭配使用

            Cookie用于储存用户的信息(不是很重要的信息) 保存到浏览器中

    Cookie的特点:

            A.Cookie 是以键值对来进行存储

            B.Cookie 存储数据的长度不能超过4kb

            C.Cookie默认的生命周期 是随着浏览器的开始而开始 随着浏览器关闭而关闭

            D.Cookie是将数据保存在浏览器中 存储的都是一些不重要的信息 

    使用场景:

            登录的时候保存用户名与密码在浏览器中:

            智能推送:

    查看当前网站Cookie:

            

            

    查看所有的Cookie:

    删除所有的Cookie:

    案例:

            需求:

            jsp页面

    1. <%--
    2. Created by IntelliJ IDEA.
    3. User: 86182
    4. Date: 2022/9/6
    5. Time: 9:45
    6. To change this template use File | Settings | File Templates.
    7. --%>
    8. <%@ page contentType="text/html;charset=UTF-8" language="java" %>
    9. <html>
    10. <head>
    11. <title>$Title$title>
    12. head>
    13. <body>
    14. <%
    15. //获取cookie数据
    16. Cookie[] cookies = request.getCookies();
    17. //对数组进行非空验证
    18. if (cookies != null && cookies.length > 0){
    19. for (Cookie c : cookies){
    20. if ("uname".equals(c.getName())){
    21. pageContext.setAttribute("uname",c.getValue());
    22. }else if ("upwd".equals(c.getName())){
    23. pageContext.setAttribute("upwd",c.getValue());
    24. }
    25. }
    26. }
    27. %>
    28. <form action="${pageContext.request.contextPath}/userServlet" method="post">
    29. <p>
    30. 用户名:<input type="text" name="uname" value="${uname}">
    31. p>
    32. <p>
    33. 密码:<input type="password" name="upwd" value="${upwd}">
    34. p>
    35. <p>
    36. <input type="submit" value="提交">
    37. p>
    38. form>
    39. body>
    40. html>

            userServlet类:

    1. package com.qf.servlet;
    2. import javax.servlet.ServletException;
    3. import javax.servlet.annotation.WebServlet;
    4. import javax.servlet.http.Cookie;
    5. import javax.servlet.http.HttpServlet;
    6. import javax.servlet.http.HttpServletRequest;
    7. import javax.servlet.http.HttpServletResponse;
    8. import java.io.IOException;
    9. @WebServlet(name = "userServlet",urlPatterns = "/userServlet")
    10. public class UserServlet extends HttpServlet {
    11. @Override
    12. protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    13. //获取前端传输的数据
    14. String uname = req.getParameter("uname");
    15. String upwd = req.getParameter("upwd");
    16. //判断是否登录成功
    17. if ("admin".equals(uname) && "123456".equals(upwd)){
    18. //将数据保存到cookie中
    19. Cookie cookieName = new Cookie("uname",uname);
    20. //设置cookie保存时间 秒为单位
    21. cookieName.setMaxAge(60*60);
    22. Cookie cookieUpwd = new Cookie("upwd",upwd);
    23. cookieUpwd.setMaxAge(60*60);
    24. //将cookie对象添加到响应对象中
    25. resp.addCookie(cookieName);
    26. resp.addCookie(cookieUpwd);
    27. }else {
    28. //登录失败 重新跳转到登录页面
    29. req.getRequestDispatcher("index.jsp").forward(req,resp);
    30. }
    31. }
    32. }

     

    三、Session会话

    简介:

             Session是服务器会话对象

            Session是四大作用域对象

            Sesssion的唯一标识是sessionId 这个session保存在Cookie中 每次重新打开浏览器 都会创建一个唯一的sessionId

            Session与Cookie搭配使用

    Session原理:

            

    Session 使用场景:

            登录成功之后显示用户名:

            银行项目的登录验证

            政府内部项目登录验证

            页面访问的验证

    获取Session:

            jsp中获取:因为是内置对象

            在Servlet中获取:通过请求对象获取

    设置session失效的时间:

            在web.xml中设置:以分钟为单位 

            使用java设置:以秒为单位

    清空sesssion值:

    Cookie 与Session比较:

    案例:

            需求:

            jsp页面:

                    a.jsp

    1. <%--
    2. Created by IntelliJ IDEA.
    3. User: 86182
    4. Date: 2022/9/6
    5. Time: 11:27
    6. To change this template use File | Settings | File Templates.
    7. --%>
    8. <%@ page contentType="text/html;charset=UTF-8" language="java" %>
    9. <html>
    10. <head>
    11. <title>Titletitle>
    12. head>
    13. <body>
    14. <form action="${pageContext.request.contextPath}/demoServlet" method="post">
    15. <p style="color: red">
    16. ${msg}
    17. p>
    18. <p>
    19. 用户名:<input type="text" name="uname">
    20. p>
    21. <p>
    22. 密码:<input type="password" name="upwd">
    23. p>
    24. <p>
    25. <input type="submit" value="登录">
    26. p>
    27. form>
    28. body>
    29. html>

                     b.jsp

    1. <%--
    2. Created by IntelliJ IDEA.
    3. User: 86182
    4. Date: 2022/9/6
    5. Time: 11:33
    6. To change this template use File | Settings | File Templates.
    7. --%>
    8. <%@ page contentType="text/html;charset=UTF-8" language="java" %>
    9. <html>
    10. <head>
    11. <title>Titletitle>
    12. head>
    13. <body>
    14. <%
    15. //验证session是否为空
    16. Object uname = session.getAttribute("uname");
    17. if (uname==null||"".equals(uname)){
    18. //提示信息
    19. request.setAttribute("msg","非法登录,小心腿打断");
    20. //转发
    21. request.getRequestDispatcher("a.jsp").forward(request,response);
    22. return;
    23. }
    24. %>
    25. <h1>欢迎来到VIP洗脚城,用户:${uname}h1>
    26. body>
    27. html>

            demoServlet类:

    1. package com.qf.servlet;
    2. import javax.servlet.ServletException;
    3. import javax.servlet.annotation.WebServlet;
    4. import javax.servlet.http.HttpServlet;
    5. import javax.servlet.http.HttpServletRequest;
    6. import javax.servlet.http.HttpServletResponse;
    7. import javax.servlet.http.HttpSession;
    8. import java.io.IOException;
    9. @WebServlet(name = "demoServlet",urlPatterns = "/demoServlet")
    10. public class DemoServlet extends HttpServlet {
    11. @Override
    12. protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    13. //获取前端传递的数据
    14. String uname = req.getParameter("uname");
    15. String upwd = req.getParameter("upwd");
    16. //验证是否登录成功
    17. if ("admin".equals(uname)&&"123456".equals(upwd)){
    18. //将数据保存到session中
    19. HttpSession session = req.getSession();
    20. session.setAttribute("uname",uname);
    21. //重定向
    22. resp.sendRedirect("b.jsp");
    23. }else {
    24. //登录失败
    25. req.setAttribute("msg","登录失败");
    26. req.getRequestDispatcher("a.jsp").forward(req,resp);
    27. }
    28. }
    29. }

    四、时间处理

    MySQL中的时间:

            date ==> 年 月 日

            time ==> 时 分 秒

            datetime ==> 年 月 日 时 分 秒 

    java中的时间:

            1. Date           Calendar

            2.对时间格式化类 SimpleDateFormat

                    Date parse(String text) ==> 将字符串转换为

                    Date public StringBuffer format(Date date) ==> 将Date转换为指定格式的字符串

    前端时间处理-My97 DatePicker:

            下载地址:

                    My97日期控件 下载 & 更新日志 My97Datepicker Download & Changelog

            使用:

    step01 下载资源 并解压

     

    step02 copy素材到web目录下

    step03 在页面中使用

    1. <%--
    2. Created by IntelliJ IDEA.
    3. User: 86182
    4. Date: 2022/9/6
    5. Time: 14:37
    6. To change this template use File | Settings | File Templates.
    7. --%>
    8. <%@ page contentType="text/html;charset=UTF-8" language="java" %>
    9. <html>
    10. <head>
    11. <title>Titletitle>
    12. <script src="${pageContext.request.contextPath}/My97DatePicker/jquery-1.8.3.js">script>
    13. <script src="${pageContext.request.contextPath}/My97DatePicker/WdatePicker.js">script>
    14. head>
    15. <body>
    16. <input type="text" id="d241" onclick="WdatePicker({dateFmt:'yyyy-MM-dd HH:mm:ss'})" class="Wdate" style="width:300px"/>
    17. body>
    18. html>

    五、解决乱码

    数据库编码格式:

            

    配置文件:

    Tomcat:

            step01 修改配置文件server.xml

     

     

    Servlet:

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

     

     

     

     

     

     

     

     

     

     

     

     

     

     

  • 相关阅读:
    【刷爆LeetCode】七月算法集训(20)二叉搜索树
    【第67题】JAVA高级技术-多线程1(查看线程的运行状态)
    【花雕动手做】有趣好玩的音乐可视化系列小项目(14)---水杯水瓶灯
    安装Vmware workstations 16 Microsoft Visual C++ 2019 x86 Minimum Runtime报错解决办法
    选择排序算法(C++版)
    隧道技术的三种应用场景(IPv6,多播,VPN)
    CMMI2.0之我见-技术解决方案TS&产品集成PI
    破防了,原来这才是机房运维的正确方法
    线代 | 目标120以上的综合提高题
    JXLS2同一个sheet多个表格循环覆盖下面表格数据问题
  • 原文地址:https://blog.csdn.net/qq_53884348/article/details/126719465