您现在的位置是:首页 > 技术教程 正文

Session详解,学习 Session对象一篇文章就够了

admin 阅读: 2024-03-28
后台-插件-广告管理-内容页头部广告(手机)

目录

1 Session概述

2 Session原理

3 Session使用

3.1 获取Session

3.2 Session保存数据

3.3 Session获取数据

3.4 Session移除数据

4 Session与Request应用区别

4.1 Session和request存储数据

4.2 获取session和request中的值

4.3 session和request区别效果

5 Session的声明周期

5.1 Session有效时间设置

5.2 session销毁

.6 浏览器禁用Cookie解决方案(了解)

6.1 浏览器禁用Cookie的后果

6.2 URL重写

6.3 实现URL重写

7 Session实战权限验证

7.1 创建管理员表manager并添加数据

7.2 创建Web项目

7.3 基础环境搭建

7.4 登录页面

7.5 LoginMgrController

7.6 ShowAllManagerController

7.7 ShowAllManagerJsp

8 Session实战保存验证码

8.1 创建验证码

8.2 登录页面

8.3 LoginMgrController

8.4 ShowAllManagerController

今天的分享就到此结束了

创作不易点赞评论互关三连


1 Session概述

(1)Session用于记录用户的状态。Session指的是一段时间内,单个客户端与Web服务器的一连串相关的交互过程。

(2)在一个Session中,客户可能会多次请求访问同一个资源,也有可能请求访问各种不同的服务器资源。

(3)Session是由服务器端创建的

2 Session原理

(1)Session会为每一次会话分配一个Session对象

(2)同一个浏览器发起的多次请求,同属于一次会话(Session)

(3)首次使用到Session时,服务器会自动创建Session,并创建Cookie存储SessionId发送回客户端

3 Session使用

Session作用域:拥有存储数据的空间,作用范围是一次会话有效

  • 一次会话是使用同一浏览器发送的多次请求。一旦浏览器关闭,则结束会话

  • 可以将数据存入Session中,在一次会话的任意位置进行获取

  • 可传递任何数据(基本数据类型、对象、集合、数组)

3.1 获取Session

Session是服务器端自动创建的,通过request对象获取

  1. package com.cxyzxc.www.servlet01;
  2. import javax.servlet.*;
  3. import javax.servlet.http.*;
  4. import javax.servlet.annotation.*;
  5. import java.io.IOException;
  6. @WebServlet(name = "SessionServlet01", value = "/SessionServlet01")
  7. public class SessionServlet01 extends HttpServlet {
  8. @Override
  9. protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  10. //设置请求参数的编码格式,这种方式对get请求方式无效
  11. request.setCharacterEncoding("UTF-8");
  12. //设置响应编码格式为UTF-8
  13. response.setContentType("text/html;charset=UTF-8");
  14. //获取Session对象,首次使用到Session时,服务器会自动创建Session,并创建Cookie存储SessionId发送回客户端
  15. HttpSession session = request.getSession();
  16. System.out.println("ID:" + session.getId());//唯一标记
  17. }
  18. @Override
  19. protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  20. doGet(request, response);
  21. }
  22. }

3.2 Session保存数据

使用setArrtibute(属性名,Object)保存数据到session中

  1. package com.cxyzxc.www.servlet01;
  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 = "SessionServlet02", value = "/SessionServlet02")
  10. public class SessionServlet02 extends HttpServlet {
  11. @Override
  12. protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  13. //设置请求参数的编码格式,这种方式对get请求方式无效
  14. request.setCharacterEncoding("UTF-8");
  15. //设置响应编码格式为UTF-8
  16. response.setContentType("text/html;charset=UTF-8");
  17. //获取Session对象,首次使用到Session时,服务器会自动创建Session,并创建Cookie存储SessionId发送回客户端
  18. HttpSession session = request.getSession();
  19. //将数据存储以键值对的形式到session对象中,可传递任何数据(基本数据类型、对象、集合、数组)
  20. session.setAttribute("username","张三");
  21. }
  22. @Override
  23. protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  24. doGet(request, response);
  25. }
  26. }

 

3.3 Session获取数据

(1)使用getAttribute("属性名");获取session中数据。

(2)先访问SessionServlet02将数据存储到session对象中,然后通过GetSessionValueServlet01请求获取session中的数据

  1. package com.cxyzxc.www.servlet01;
  2. import javax.servlet.*;
  3. import javax.servlet.http.*;
  4. import javax.servlet.annotation.*;
  5. import java.io.IOException;
  6. @WebServlet(name = "GetSessionValueServlet01", value = "/GetSessionValueServlet01")
  7. public class GetSessionValueServlet01 extends HttpServlet {
  8. @Override
  9. protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  10. //设置请求参数的编码格式,这种方式对get请求方式无效
  11. request.setCharacterEncoding("UTF-8");
  12. //设置响应编码格式为UTF-8
  13. response.setContentType("text/html;charset=UTF-8");
  14. //获取Session对象
  15. HttpSession session = request.getSession();
  16. //获取session对象中的值,获取的值是Object类型,转换为其对应的类型
  17. String username = (String) session.getAttribute("username");
  18. System.out.println("session对象中存储的username值:" + username);
  19. }
  20. @Override
  21. protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  22. doGet(request, response);
  23. }
  24. }

3.4 Session移除数据

(1)使用removeAttribute("属性名");从session中删除数据

(2)向请求SessionServlet02向session对象中存储数据,然后访问GetSessionValueServlet01可以获取session对象中的值,再访问SessionServlet03移除session对象中存储的数据,最后访问GetSessionValueServlet01获取session对象中的值为null

  1. package com.cxyzxc.www.servlet01;
  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 = "SessionServlet03", value = "/SessionServlet03")
  10. public class SessionServlet03 extends HttpServlet {
  11. @Override
  12. protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  13. //设置请求参数的编码格式,这种方式对get请求方式无效
  14. request.setCharacterEncoding("UTF-8");
  15. //设置响应编码格式为UTF-8
  16. response.setContentType("text/html;charset=UTF-8");
  17. //获取Session对象,首次使用到Session时,服务器会自动创建Session,并创建Cookie存储SessionId发送回客户端
  18. HttpSession session = request.getSession();
  19. //通过键移除session作用域中的值
  20. session.removeAttribute("username");
  21. }
  22. @Override
  23. protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  24. doGet(request, response);
  25. }
  26. }

 

4 Session与Request应用区别

(1)request是一次请求有效,请求改变,则request改变

(2)session是一次会话有效,浏览器改变,则session改变

4.1 Session和request存储数据

  1. package com.cxyzxc.www.servlet01;
  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 = "SessionServlet04", value = "/SessionServlet04")
  10. public class SessionServlet04 extends HttpServlet {
  11. @Override
  12. protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  13. //设置请求参数的编码格式,这种方式对get请求方式无效
  14. request.setCharacterEncoding("UTF-8");
  15. //设置响应编码格式为UTF-8
  16. response.setContentType("text/html;charset=UTF-8");
  17. //获取Session对象
  18. HttpSession session = request.getSession();
  19. //使用session存储数据
  20. session.setAttribute("username","zhangsan");
  21. //使用request存储数据
  22. request.setAttribute("password","123456");
  23. //重定向
  24. response.sendRedirect("/webProject10_war_exploded/GetSessionValueServlet01");
  25. }
  26. @Override
  27. protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  28. doGet(request, response);
  29. }
  30. }

 

4.2 获取session和request中的值

  1. package com.cxyzxc.www.servlet01;
  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 = "GetSessionValueServlet01", value = "/GetSessionValueServlet01")
  10. public class GetSessionAndRequestValueServlet01 extends HttpServlet {
  11. @Override
  12. protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  13. //设置请求参数的编码格式,这种方式对get请求方式无效
  14. request.setCharacterEncoding("UTF-8");
  15. //设置响应编码格式为UTF-8
  16. response.setContentType("text/html;charset=UTF-8");
  17. //获取Session对象
  18. HttpSession session = request.getSession();
  19. //获取session对象中的值,获取的值是Object类型,转换为其对应的类型
  20. String username = (String) session.getAttribute("username");
  21. //获取request对象中的值,获取的值是Object类型,转换为其对应的类型
  22. String password = (String) request.getAttribute("password");
  23. System.out.println("session对象中存储的username值:" + username);
  24. System.out.println("request对象中存储的password值:" + password);
  25. }
  26. @Override
  27. protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  28. doGet(request, response);
  29. }
  30. }

4.3 session和request区别效果

5 Session的声明周期

  1. 开始

    第一次使用到Session的请求产生,则创建Session

  2. 结束

    • 浏览器关闭,则失效

    • Session超时,则失效

      session.setMaxInactiveInterval(seconds);//设置最大有效时间(单位:秒)

    • 手工销毁,则失效

      session.invalidate();//登录退出,销毁

5.1 Session有效时间设置

SessionServlet05类设置session有效期为20秒,先通过请求SessionServlet05类将session存储在,然后在20秒内第一次在GetSessionValueServlet02获取sessionID值,与SessionServlet05类中输出的id值一致,过20秒后在GetSessionValueServlet02类中输出的sessionID值不一致了

 

  1. package com.cxyzxc.www.servlet01;
  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 = "SessionServlet05", value = "/SessionServlet05")
  10. public class SessionServlet05 extends HttpServlet {
  11. @Override
  12. protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  13. //设置请求参数的编码格式,这种方式对get请求方式无效
  14. request.setCharacterEncoding("UTF-8");
  15. //设置响应编码格式为UTF-8
  16. response.setContentType("text/html;charset=UTF-8");
  17. //获取Session对象,首次使用到Session时,服务器会自动创建Session,并创建Cookie存储SessionId发送回客户端
  18. HttpSession session = request.getSession();
  19. //设置session有效期,时间单位为秒
  20. session.setMaxInactiveInterval(20);
  21. //输出sessionid值
  22. System.out.println("SessionServlet05类中输出ID:"+session.getId());
  23. }
  24. @Override
  25. protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  26. doGet(request, response);
  27. }
  28. }

5.2 session销毁

先使用GetSessionValueServlet03类获取session的id值,然后使用GetSessionValueServlet04类获取session的id值,两个类获取的id值一致,在GetSessionValueServlet04类中输出id值后销毁了session,然后再在GetSessionValueServlet03类中获取id值,就不一致了,就是服务器新建的session对象了

5.2.1 GetSessionValueServlet03类

  1. package com.cxyzxc.www.servlet01;
  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 = "GetSessionValueServlet03", value = "/GetSessionValueServlet03")
  10. public class GetSessionValueServlet03 extends HttpServlet {
  11. @Override
  12. protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  13. //设置请求参数的编码格式,这种方式对get请求方式无效
  14. request.setCharacterEncoding("UTF-8");
  15. //设置响应编码格式为UTF-8
  16. response.setContentType("text/html;charset=UTF-8");
  17. //获取Session对象
  18. HttpSession session = request.getSession();
  19. //输出sessionid值
  20. System.out.println("GetSessionValueServlet03类中输出ID:"+session.getId());
  21. }
  22. @Override
  23. protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  24. doGet(request, response);
  25. }
  26. }

 

5.2.2 GetSessionValueServlet04类

  1. package com.cxyzxc.www.servlet01;
  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 = "GetSessionValueServlet04", value = "/GetSessionValueServlet04")
  10. public class GetSessionValueServlet04 extends HttpServlet {
  11. @Override
  12. protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  13. //设置请求参数的编码格式,这种方式对get请求方式无效
  14. request.setCharacterEncoding("UTF-8");
  15. //设置响应编码格式为UTF-8
  16. response.setContentType("text/html;charset=UTF-8");
  17. //获取Session对象
  18. HttpSession session = request.getSession();
  19. //输出sessionid值
  20. System.out.println("GetSessionValueServlet04类中输出ID:"+session.getId());
  21. //销毁session
  22. session.invalidate();
  23. }
  24. @Override
  25. protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  26. doGet(request, response);
  27. }
  28. }

.6 浏览器禁用Cookie解决方案(了解)

6.1 浏览器禁用Cookie的后果

服务器在默认情况下,会使用Cookie的方式将sessionID发送给浏览器,如果用户禁止Cookie,则sessionID不会被浏览器保存,此时,服务器可以使用URL重写这样的方式来发送sessionID

 

多次请求GetSessionValueServlet05类输出的session的id值都不相同,并且在网站的Cookie对象中没有session的id值存在

 

6.2 URL重写

浏览器在访问服务器上的某个地址时,不再使用原来的那个地址,而是使用经过改写的地址(即在原来的地址后面加上了sessionID)

6.3 实现URL重写

response.encodeRedirectURL(String url)生成重写的URL

6.3.1 重写URL

  1. package com.cxyzxc.www.servlet01;
  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 = "GetSessionValueServlet06", value = "/GetSessionValueServlet06")
  10. public class GetSessionValueServlet06 extends HttpServlet {
  11. @Override
  12. protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  13. //设置请求参数的编码格式,这种方式对get请求方式无效
  14. request.setCharacterEncoding("UTF-8");
  15. //设置响应编码格式为UTF-8
  16. response.setContentType("text/html;charset=UTF-8");
  17. //获取Session对象
  18. HttpSession session = request.getSession();
  19. //输出sessionid值
  20. System.out.println("GetSessionValueServlet06类中输出ID:"+session.getId());
  21. //重写URL追加session值
  22. String newURL = response.encodeURL("/webProject10_war_exploded/GetSessionValueServlet07");
  23. System.out.println("重写后的URL:"+newURL);
  24. //重定向
  25. response.sendRedirect(newURL);
  26. }
  27. @Override
  28. protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  29. doGet(request, response);
  30. }
  31. }

 

6.3.1 获取session

  1. package com.cxyzxc.www.servlet01;
  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 = "GetSessionValueServlet07", value = "/GetSessionValueServlet07")
  10. public class GetSessionValueServlet07 extends HttpServlet {
  11. @Override
  12. protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  13. //设置请求参数的编码格式,这种方式对get请求方式无效
  14. request.setCharacterEncoding("UTF-8");
  15. //设置响应编码格式为UTF-8
  16. response.setContentType("text/html;charset=UTF-8");
  17. //获取Session对象
  18. HttpSession session = request.getSession();
  19. //输出sessionid值
  20. System.out.println("GetSessionValueServlet07类中输出ID:"+session.getId());
  21. }
  22. @Override
  23. protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  24. doGet(request, response);
  25. }
  26. }

7 Session实战权限验证

 

7.1 创建管理员表manager并添加数据

 

7.2 创建Web项目

创建Web项目,导入相关jar包

  • commons-dbutils-1.7.jar

  • druid-1.1.5.jar

  • mysql-connector-java-5.1.25-bin.jar

  • servlet-api.jar

7.3 基础环境搭建

在项目下创建包目录结构如下

  • com.cxyzxc.www.controller包:调用业务逻辑Servlet

  • com.cxyzxc.www.dao包:数据访问层接口

  • com.cxyzxc.www.dao.impl包:数据访问层接口实现类

  • com.cxyzxc.www.entity包:实体类

  • com.cxyzxc.www.jsp包:打印显示页面Servlet

  • com.cxyzxc.www.service包:业务逻辑层接口

  • com.cxyzxc.www.service.impl包:业务逻辑层接口实现类

  • com.cxyzxc.www.utils包:工具类

  • database.properties:数据库连接及连接池配置文件

7.4 登录页面

7.4.1 login.html

  1. "en">
  2. "UTF-8">
  3. 管理员登录页面
  4. "text/css" rel="stylesheet" href="css/login.css"/>
  5. "LoginMgrController" method="post">
  6. 账号:"text" name="username"/>
  7. 密码:"password" name="password"/>
  8. "submit" value="登录"/>

 

7.4.2 login.css

  1. * {
  2. margin: 0;
  3. padding: 0;
  4. }
  5. div {
  6. width: 400px;
  7. background-color: #ccc;
  8. margin: 30px auto;
  9. padding-top: 30px;
  10. text-align: center;
  11. }
  12. p {
  13. margin-top: 10px;
  14. }
  15. input {
  16. outline: none;
  17. }

7.5 LoginMgrController

  1. package com.cxyzxc.www.controller;
  2. import com.cxyzxc.www.entity.Manager;
  3. import com.cxyzxc.www.service.ManagerService;
  4. import com.cxyzxc.www.service.impl.ManagerServiceImpl;
  5. import javax.servlet.*;
  6. import javax.servlet.http.*;
  7. import javax.servlet.annotation.*;
  8. import java.io.IOException;
  9. @WebServlet(name = "LoginMgrController", value = "/LoginMgrController")
  10. public class LoginMgrController extends HttpServlet {
  11. @Override
  12. protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  13. //1、处理乱码
  14. request.setCharacterEncoding("UTF-8");
  15. response.setContentType("text/html;charset=UTF-8");
  16. //2、收参(获取客户端发送过来的请求数据)
  17. String username = request.getParameter("username");
  18. String password = request.getParameter("password");
  19. //3、调用业务方法
  20. ManagerService managerService = new ManagerServiceImpl();
  21. Manager manager = managerService.login(username, password);
  22. //4、处理结果,根据结果做不同的跳转
  23. if (manager != null) {//manager不为null,说明账号和密码正确,登录成功
  24. //将获取的账号和密码信息存储在session中
  25. HttpSession session = request.getSession();
  26. session.setAttribute("manager", manager);
  27. //跳转到显示所有管理员信息的Servlet
  28. response.sendRedirect("/managerProject01_war_exploded/ShowAllManagerController");
  29. } else {//manager为null。说明账号或者密码错误,登录失败
  30. response.sendRedirect("/managerProject01_war_exploded/login.html");
  31. }
  32. }
  33. @Override
  34. protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  35. doGet(request, response);
  36. }
  37. }

7.6 ShowAllManagerController

  1. package com.cxyzxc.www.controller;
  2. import com.cxyzxc.www.entity.Manager;
  3. import com.cxyzxc.www.service.ManagerService;
  4. import com.cxyzxc.www.service.impl.ManagerServiceImpl;
  5. import javax.servlet.*;
  6. import javax.servlet.http.*;
  7. import javax.servlet.annotation.*;
  8. import java.io.IOException;
  9. import java.util.List;
  10. @WebServlet(name = "ShowAllManagerController", value = "/ShowAllManagerController")
  11. public class ShowAllManagerController extends HttpServlet {
  12. @Override
  13. protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  14. //1、处理乱码
  15. request.setCharacterEncoding("UTF-8");
  16. response.setContentType("text/html;charset=UTF-8");
  17. //通过HttpSession完成权限控制
  18. HttpSession session =request.getSession();
  19. //获取session中存储的值
  20. Manager manager = (Manager)session.getAttribute("manager");
  21. //判断获取的值
  22. if(manager!=null){
  23. //调用业务,只做业务,业务与显示分离
  24. ManagerService managerService = new ManagerServiceImpl();
  25. List managers =managerService.showAllManager();
  26. //将获取的数据存储在request作用域中
  27. request.setAttribute("managers",managers);
  28. //转发,跳转到显示结果的Servlet
  29. request.getRequestDispatcher("/ShowAllManagerJsp").forward(request,response);
  30. }else{
  31. //说明没有登录,要先去登录才能进行显示
  32. response.sendRedirect("/managerProject01_war_exploded/login.html");
  33. }
  34. }
  35. @Override
  36. protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  37. doGet(request, response);
  38. }
  39. }

7.7 ShowAllManagerJsp

  1. package com.cxyzxc.www.jsp;
  2. import com.cxyzxc.www.entity.Manager;
  3. import javax.servlet.*;
  4. import javax.servlet.http.*;
  5. import javax.servlet.annotation.*;
  6. import java.io.IOException;
  7. import java.io.PrintWriter;
  8. import java.util.List;
  9. @WebServlet(name = "ShowAllManagerJsp", value = "/ShowAllManagerJsp")
  10. public class ShowAllManagerJsp extends HttpServlet {
  11. @Override
  12. protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  13. //1、处理乱码
  14. request.setCharacterEncoding("UTF-8");
  15. response.setContentType("text/html;charset=UTF-8");
  16. //2、获取数据
  17. List managers = (List) request.getAttribute("managers");
  18. //获取输出流
  19. PrintWriter printWriter = response.getWriter();
  20. if (managers.size() != 0) {
  21. printWriter.println("");
  22. printWriter.println("");
  23. printWriter.println("所有管理员");
  24. printWriter.println("");
  25. printWriter.println("");
  26. printWriter.println("");
  27. printWriter.println("
  28. ");
  29. printWriter.println("
  30. ");
  31. printWriter.println("
  32. ");
  33. printWriter.println("
  34. ");
  35. printWriter.println("
  36. ");
  37. printWriter.println("
  38. ");
  39. for (int i = 0; i < managers.size(); i++) {
  40. printWriter.println("
  41. ");
  42. printWriter.println("
  43. ");
  44. printWriter.println("
  45. ");
  46. printWriter.println("
  47. ");
  48. printWriter.println("
  49. ");
  50. printWriter.println("
  51. ");
  52. }
  53. printWriter.println("
  54. 序号账号密码操作
    " + (i + 1) + "" + managers.get(i).getUsername() + "" + managers.get(i).getPassword() + "修改 删除
    "
    );
  55. printWriter.println("");
  56. } else {
  57. printWriter.println("

    数据库中没有数据查询

    "
    );
  58. }
  59. }
  60. @Override
  61. protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  62. doGet(request, response);
  63. }
  64. }

8 Session实战保存验证码

生成验证码的方式有很多种,可以使用随机数的方式实现,也可以使用ValidateCode类来实现(需要导入ValidateCode.jar包)。在这里,我们学习使用ValidateCode类来生成验证码

8.1 创建验证码

  1. package com.cxyzxc.www.controller;
  2. import cn.dsna.util.images.ValidateCode;
  3. import javax.servlet.*;
  4. import javax.servlet.http.*;
  5. import javax.servlet.annotation.*;
  6. import java.io.IOException;
  7. /**
  8. * 此Servlet的作用是生成验证码并将生成的验证码存储到session中、发送到页面中显示
  9. */
  10. @WebServlet(name = "VerificationServlet", value = "/VerificationServlet")
  11. public class VerificationServlet extends HttpServlet {
  12. @Override
  13. protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  14. //设置验证码规格
  15. ValidateCode validateCode = new ValidateCode(200, 20, 4, 10);
  16. //获取验证码
  17. String code = validateCode.getCode();
  18. //将验证码存储在session中
  19. HttpSession session = request.getSession();
  20. session.setAttribute("code",code);
  21. //将验证码输出到客户端
  22. validateCode.write(response.getOutputStream());
  23. }
  24. @Override
  25. protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  26. doGet(request, response);
  27. }
  28. }

8.2 登录页面

  1. "en">
  2. "UTF-8">
  3. 管理员登录页面
  4. "text/css" rel="stylesheet" href="css/login.css"/>
  5. "LoginMgrController" method="post">
  6. 账号:"text" name="username"/>
  7. 密码:"password" name="password"/>
  8. 验证码:"text" name="verification"/>
  9. "/managerProject02_war_exploded/VerificationServlet"%20/>
  10. %20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20

  11. %20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20

  12. %20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20"submit"%20value="登录"/>
  13. %20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20

  14. %20%20%20%20%20%20%20%20%20%20%20%20
  15. %20%20%20%20%20%20%20%20
  16. %20%20%20%20

8.3 LoginMgrController

  1. package com.cxyzxc.www.controller;
  2. import com.cxyzxc.www.entity.Manager;
  3. import com.cxyzxc.www.service.ManagerService;
  4. import com.cxyzxc.www.service.impl.ManagerServiceImpl;
  5. import javax.servlet.ServletException;
  6. import javax.servlet.annotation.WebServlet;
  7. import javax.servlet.http.HttpServlet;
  8. import javax.servlet.http.HttpServletRequest;
  9. import javax.servlet.http.HttpServletResponse;
  10. import javax.servlet.http.HttpSession;
  11. import java.io.IOException;
  12. @WebServlet(name = "LoginMgrController", value = "/LoginMgrController")
  13. public class LoginMgrController extends HttpServlet {
  14. @Override
  15. protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  16. //1、处理乱码
  17. request.setCharacterEncoding("UTF-8");
  18. response.setContentType("text/html;charset=UTF-8");
  19. //2、收参(获取客户端发送过来的请求数据)
  20. String username = request.getParameter("username");
  21. String password = request.getParameter("password");
  22. String inputCodde = request.getParameter("verification");
  23. //获取session中的验证码
  24. HttpSession session = request.getSession();
  25. String codes = (String) session.getAttribute("code");
  26. if (!inputCodde.isEmpty() && inputCodde.equalsIgnoreCase(codes)) {
  27. //3、调用业务方法
  28. ManagerService managerService = new ManagerServiceImpl();
  29. Manager manager = managerService.login(username, password);
  30. //4、处理结果,根据结果做不同的跳转
  31. if (manager != null) {//manager不为null,说明账号和密码正确,登录成功
  32. //将获取的账号和密码信息存储在session中
  33. HttpSession session2 = request.getSession();
  34. session2.setAttribute("manager", manager);
  35. //跳转到显示所有管理员信息的Servlet
  36. response.sendRedirect("/managerProject02_war_exploded/ShowAllManagerController");
  37. } else {//manager为null。说明账号或者密码错误,登录失败
  38. response.sendRedirect("/managerProject02_war_exploded/login.html");
  39. }
  40. } else {//验证码不对,重新登录
  41. response.sendRedirect("/managerProject02_war_exploded/login.html");
  42. }
  43. }
  44. @Override
  45. protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  46. doGet(request, response);
  47. }
  48. }

8.4 ShowAllManagerController

  1. package com.cxyzxc.www.controller;
  2. import com.cxyzxc.www.entity.Manager;
  3. import com.cxyzxc.www.service.ManagerService;
  4. import com.cxyzxc.www.service.impl.ManagerServiceImpl;
  5. import javax.servlet.ServletException;
  6. import javax.servlet.annotation.WebServlet;
  7. import javax.servlet.http.HttpServlet;
  8. import javax.servlet.http.HttpServletRequest;
  9. import javax.servlet.http.HttpServletResponse;
  10. import javax.servlet.http.HttpSession;
  11. import java.io.IOException;
  12. import java.util.List;
  13. @WebServlet(name = "ShowAllManagerController", value = "/ShowAllManagerController")
  14. public class ShowAllManagerController extends HttpServlet {
  15. @Override
  16. protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  17. //通过HttpSession完成权限控制
  18. HttpSession session =request.getSession();
  19. //获取session中存储的值
  20. Manager manager = (Manager)session.getAttribute("manager");
  21. //判断获取的值
  22. if(manager!=null){
  23. //调用业务,只做业务,业务与显示分离
  24. ManagerService managerService = new ManagerServiceImpl();
  25. List managers =managerService.showAllManager();
  26. //将获取的数据存储在request作用域中
  27. request.setAttribute("managers",managers);
  28. //转发,跳转到显示结果的Servlet
  29. request.getRequestDispatcher("/ShowAllManagerJsp").forward(request,response);
  30. }else{
  31. //说明没有登录,要先去登录才能进行显示
  32. response.sendRedirect("/managerProject02_war_exploded/login.html");
  33. }
  34. }
  35. @Override
  36. protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  37. doGet(request, response);
  38. }
  39. }

今天的分享就到此结束了

创作不易点赞评论互关三连

 

 

标签:
声明

1.本站遵循行业规范,任何转载的稿件都会明确标注作者和来源;2.本站的原创文章,请转载时务必注明文章作者和来源,不尊重原创的行为我们将追究责任;3.作者投稿可能会经我们编辑修改或补充。

在线投稿:投稿 站长QQ:1888636

后台-插件-广告管理-内容页尾部广告(手机)
关注我们

扫一扫关注我们,了解最新精彩内容

搜索
排行榜