一、Session超时管理 Demo5.java 二、Session实现用户登录
在实际网站用户登陆后,在浏览其他页面过程中,可能中间会有其他事情停顿,一段时间内没有对该网站页面再次发送请求访问,待到一定时间过后(即保存登陆用户信息的session超时),当该用户再次点击其他请求链接的时候,就会返回到登陆界面,要求用户重新登陆。本案例将模拟简单的登陆然后session超时跳转到再次登陆界面。
设计思路(实现原理)
1)创建User用户实体类,有username和password两个属性,
2)创建Demo4类,Demo4类提供用户登陆跳转方法,
3)创建Demo5类,Demo5类模拟session超时失效,然后再次跳转到登陆页面
4)创建login.html登陆页面,
5)在web.xml中配置初始化参数和类的映射
6)通过浏览器访问login.html,并进行相关操作,查看页面效果。
启动Tomcat服务器,在地址栏中输入https://localhost:8080/chapter05/ login.html,访问浏览器显示的结果如下图所示:
在输入框随机输入用户名和密码,如”张三”、”123”,然后提交,浏览器显示的结果如下图所示:
在上图中显示用户登陆成功,并将用户放在了session中管理,再点击上图中的查看手机商品链接,短暂延迟后浏览器显示的结果如下所示:
同时观察Eclipse项目控制台可以发现报错Session already invalidated(session已经失效),信息如下:
实现代码如下:
Demo4.javaimport java.io.IOException; import java.io.PrintWriter; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpSession; /** * Servlet implementation class Demo4 */ public class Demo4 extends HttpServlet { private static final long serialVersionUID = 1L; /** * @see HttpServlet#HttpServlet() */ public Demo4() { super(); // TODO Auto-generated constructor stub } /** * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response) */ protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub response.setContentType("text/html;charset=utf-8"); PrintWriter writer = response.getWriter(); request.setCharacterEncoding("UTF-8"); //解决读取html页面的中文乱码 String username = request.getParameter("username"); String password = request.getParameter("password"); HttpSession session = request.getSession(); session.setAttribute("username", username); session.setAttribute("password", password); //将数据储存于session中 writer.println("您已登录,欢迎您," + username + "!"); writer.write("<a href = 'https://localhost:8080/chapter05/Demo5'>产看手机商品</a>"); } /** * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response) */ protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub doGet(request, response); } }
import java.io.IOException; import java.io.PrintWriter; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpSession; /** * Servlet implementation class Demo5 */ public class Demo5 extends HttpServlet { private static final long serialVersionUID = 1L; /** * @see HttpServlet#HttpServlet() */ public Demo5() { super(); // TODO Auto-generated constructor stub } /** * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response) */ protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub response.setContentType("text/html;charset=utf-8"); PrintWriter writer = response.getWriter(); HttpSession session = request.getSession(); session.invalidate(); //强制清空 try { //让当前的线程睡眠2秒钟,模拟网络延迟 Thread.sleep(2 * 1000); } catch (InterruptedException e) { e.printStackTrace(); } try { session.getAttribute("username"); } catch(IllegalStateException e) { e.printStackTrace(); } //找到错误并抛出 writer.println("您还没有登录,请"); writer.write("<a href = 'login.html'>登录</a>"); System.out.println("异常:getAttribute: Session already invalidated"); } /** * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response) */ protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub doGet(request, response); } }
需求分析
在实际运用中的用户登陆网站中,多提供有记住密码和自动登陆等功能,方便同一用户短时间内不用再输入用户名和密码等繁琐信息可以快捷登陆。本案例将模拟用户自动登陆功能。
设计思路(实现原理)
1)创建login1.html页面,编写用户登陆界面
2)创建Demo6类, 编写用户登陆判断逻辑处理和自动登陆业务,
3)在web.xml中配置初始化参数和类的映射
4)通过浏览器访问login1.html,进行相关操作,查看页面效果。
启动Tomcat服务器,在地址栏中输入https://localhost:8080/chapter05/ login1.html,访问浏览器显示的结果如下图所示:
在输入框随机输入用户名,如“张三”,第一次不选中自动登陆框,然后点击提交按钮,浏览器显示的结果如下图所示:
则用户就会登陆网站了,当点击上图页面中的退出按钮就又返回到了初始登陆页面,如下所示:
可以在不输入用户名的情况下点击提交按钮,查看浏览器页面出现以下结果:
提醒用户输入用户名(因为还没有选中自动登陆功能),2秒后自动跳转到登陆页面,让用户必须登陆才可以;
第二次,在登陆界面随机输入用户名,如”zhangsan”,在勾选自动登陆框,点击提交按钮,浏览器显示结果如下:
则用户就会登陆网站了,当点击上图页面中的退出按钮就又返回到了初始登陆页面,这次不用输入用户名(必须在退出后的5秒内,因为代码中设置cookie的有效期是5秒钟),再次直接点击提价按钮,浏览器显示结果如下(用户实现了自动登陆功能):
实现代码如下:
Demo6.javaimport java.io.IOException; import java.io.PrintWriter; import javax.servlet.ServletException; import javax.servlet.http.Cookie; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; /** * Servlet implementation class Demo6 */ public class Demo6 extends HttpServlet { private static final long serialVersionUID = 1L; /** * @see HttpServlet#HttpServlet() */ public Demo6() { super(); // TODO Auto-generated constructor stub } /** * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response) */ protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub response.setContentType("text/html;charset=utf-8"); PrintWriter writer = response.getWriter(); request.setCharacterEncoding("UTF-8"); String username = request.getParameter("username"); String ck1 = request.getParameter("ck1"); Cookie[] cookies = request.getCookies(); if(username.equals("") && cookies == null) { writer.println("请输入用户名登录!"); response.setHeader("Refresh", "2; URL = https://localhost:8080/chapter05/login1.html"); } else { writer.println("您已登录,欢迎您!"); writer.write("<a href = https://localhost:8080/chapter05/login1.html>退出</a>"); if(ck1 != null) { Cookie cookie = new Cookie("username", username); cookie.setMaxAge(5); //有效期5秒 response.addCookie(cookie); } } } /** * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response) */ protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub doGet(request, response); } }
本网页所有视频内容由 imoviebox边看边下-网页视频下载, iurlBox网页地址收藏管理器 下载并得到。
ImovieBox网页视频下载器 下载地址: ImovieBox网页视频下载器-最新版本下载
本文章由: imapbox邮箱云存储,邮箱网盘,ImageBox 图片批量下载器,网页图片批量下载专家,网页图片批量下载器,获取到文章图片,imoviebox网页视频批量下载器,下载视频内容,为您提供.
阅读和此文章类似的: 全球云计算