session和cookie实现用户自动登录和退出清除缓存

您所在的位置:网站首页 skype怎么取消自动登录功能 session和cookie实现用户自动登录和退出清除缓存

session和cookie实现用户自动登录和退出清除缓存

2024-07-15 15:55| 来源: 网络整理| 查看: 265

我们需要实现下图功能 在这里插入图片描述

一,编写用户请求资源路径 @WebServlet("/indexServlet") //使用注解方式提供资源路径 public class god1 extends HttpServlet { @Override protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { HttpSession session = req.getSession(false); //判断有没有session没有的话就跳转到登录界面 if (session==null){ resp.sendRedirect("login.html"); //前端登录界面 } }

这里我们先实现没有账号登录,我们首次登录账号情况功能实现

二,编写前端登录页面 DOCTYPE html> Title //提交表单后将url请求传输到该资源路径 用户名 密码

效果如下 在这里插入图片描述

注意:前端表单的提交地址为"login"资源路径

三,编写login资源路径 @WebServlet("/login") public class god2 extends HttpServlet { @Override protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { String username = req.getParameter("username"); //通过HttpServletRequest 对象的getParameter方法 String password = req.getParameter("password"); HttpSession session = req.getSession(true); //强制创建一个session if(username.equals("god")&&password.equals("skr")){ //(判断用户名和密码是否正确)这里我用的是固定的用户名和密码 session.setAttribute("username","god"); //(给session添加对象属性,便于待会判断是否自动登录) resp.sendRedirect("index"); //重定向,跳转到index资源路径,也就是我们项目的主界面 }else { resp.setStatus(403); //设置错误信息 resp.setContentType("text/html; charset=utf8"); //设置返回的文字编码格式 resp.getWriter().write("登陆失败, 用户名或者密码错误");//提示词 } } } 四,编写index(主界面)资源 @WebServlet("/index") public class god3 extends HttpServlet { @Override protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { HttpSession session = req.getSession(false); //获取session对象 if(session == null){ //如果这个session为null,说明他的attribute里面没有对象属性 resp.setStatus(403); //报错 resp.setContentType("text/html; charset=utf8"); resp.getWriter().write("您尚未登录, 不能访问主页!"); return; } String username = (String)session.getAttribute("username"); //获取登录用户名 resp.setContentType("text/html; charset=utf8"); resp.getWriter().write("欢迎来到主页! " + username); //设置登录成功提示 String html = "" + "" + "" + " " + " 主界面" + " " + "" + "" + ""+ ""+ ""+ "" ; resp.getWriter().append(html); } }

最后面那段html代码是一个退出按钮,也就是退出当前登录的账号

5,退出功能实现

退出功能不是简单的关闭页面,同时要清除缓存的session和cookie值 代码如下

@WebServlet("/logout") public class god4 extends HttpServlet { @Override protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { HttpSession session = req.getSession(false); if (session != null) { session.invalidate(); // 销毁 Session Cookie cookie = new Cookie("JSESSIONID", ""); // 同时销毁 浏览器的 Cookie 数据 cookie.setMaxAge(0); resp.addCookie(cookie); } resp.sendRedirect("login.html"); //重定向到登录界面 } } 六,自动登录功能实现

还是我们的第一个用户请求资源路径indexServlet中加一些代码

@WebServlet("/indexServlet") public class god1 extends HttpServlet { @Override protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { HttpSession session = req.getSession(false); //判断有没有session没有的话就跳转到登录界面 if (session==null){ resp.sendRedirect("login.html"); }else { String username = (String) session.getAttribute("username"); //这里是验证我们的session的对象属性中是否成功存储了god账号 if (username.equals("god")){ resp.sendRedirect("index"); //验证成功直接跳转到项目主界面,不用登录 } } } } 七,测试 7.1 登录功能测试

运行tomcat,进入资源路径 浏览器进入 http://localhost:8080/indexServlet 在这里插入图片描述 注意地址栏,资源路径改变,说明当前god账号没有登录,重定向成功 输入我们的账号密码 god skr 在这里插入图片描述 登录成功

7.2 自动登录功能实现

在god账号登录的同时 我们在打开一个网页进入http://localhost:8080/indexServlet 你会发现你不同登录,直接就进入到主界面了 在这里插入图片描述

7.3退出功能测试

点击退出按钮 在这里插入图片描述 重定向到登录界面 此时我们再回到第一次打开的项目主界面(index资源路径) 在这里插入图片描述 因为我们把god账号的session和cookie信息抹除了,god账号的所有页面都不能用了



【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3