zoukankan      html  css  js  c++  java
  • jsp课程笔记之session(二)

    注销session及共享session案例
    在这里插入图片描述
    login.jsp

    <%@ page language="java" contentType="text/html; charset=UTF-8"
        pageEncoding="UTF-8"%>
    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>Insert title here</title>
    </head>
    <body>
    	<form action="check.jsp" method="post">
    		用户名:<input type="text" name="uname"><br/>
    		密码:<input type="password" name="upwd"><br/>
    		<input type="submit" value="登陆"><br/>
    	</form>
    </body>
    </html>
    

    check.jsp

    <%@ page language="java" contentType="text/html; charset=UTF-8"
        pageEncoding="UTF-8"%>
    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>Insert title here</title>
    </head>
    <body>
    		<%
    			request.setCharacterEncoding("utf-8") ;
    			String name = request.getParameter("uname");
    			String pwd = request.getParameter("upwd");
    			if(name.equals("zs") && pwd.equals("abc")){//假设 zs abc
    				//只有登录成功,session中才会存在uname /upwd
    				session.setAttribute("uname", name)		 ;
    				session.setAttribute("upwd", pwd)		;
    				System.out.println("sessionId"+session.getId());
    				
    				//Cookie cookie = new Cookie("uname" ,namxe);
    				//response.addCookie(cookie) ;
    				//服务端在第一次响应客户端时,会发送一个 JSESSIONID的cookie
    				
    				//session.setMaxInactiveInterval(10) ;
    				
    				request.getRequestDispatcher("welcome.jsp").forward(request, response) ;
    				
    			
    			}else{
    				//登录失败
    				response.sendRedirect("login.jsp") ;
    			}
    		
    		%>
    </body>
    </html>
    

    welocame.jsp

    <%@ page language="java" contentType="text/html; charset=UTF-8"
        pageEncoding="UTF-8"%>
    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>Insert title here</title>
    </head>
    <body>
    	欢迎您:
    		<%
    			String name = (String)session.getAttribute("uname") ;
    			//如果 用户没有登录,而是直接 通过地址栏 访问welcome.jsp,则必然获取到的name是null
    			if(name!=null){
    				out.print(name);
    				
    				
    				System.out.println();
    		%>
    			<a href="invalidate.jsp">注销</a>
    		<%
    				
    			}else{//如果没有登录,应该跳转登录页面
    				response.sendRedirect("login.jsp");
    			}
    			
    		
    		%>
    </body>
    </html>
    

    a.jsp

    <%@ page language="java" contentType="text/html; charset=UTF-8"
        pageEncoding="UTF-8"%>
    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>Insert title here</title>
    </head>
    <body>
    		<%
    
    			out.print(session.getAttribute("uname"));
    			Cookie[] cookies = request.getCookies();
    			for(Cookie cookie:cookies){
    				if(cookie.getName().equals("JSESSIONID")){
    					System.out.print("JSESSIONID"+cookie.getValue());
    				}
    			}
    		
    		
    		%>
    </body>
    </html>
    

    通过登录和直接访问a.jsp在控制台分别输出
    在这里插入图片描述
    cookie和session的区别:

    sessioncookie
    保存的位置服务端客户端
    安全性较安全较不安全
    保存的内容ObjectString

    在这里插入图片描述
    登录后点击 注销 跳回到登录页面
    在这里插入图片描述
    再次访问a.jsp时
    在这里插入图片描述
    登录之后不点击注销,直接访问a.jsp页面
    在这里插入图片描述

  • 相关阅读:
    Jabref安装及使用教程
    均方误差与均方根误差
    费马大定理
    JabRef设置
    申请内存时底层发生了什么?
    什么是Pipeline?
    基于三维点云场景的语义及实例分割
    华为 MateView 系列显示器新品
    C#-WinForm跨线程修改UI界面
    C# 代码生成二维码方法及代码示例(QRCoder)
  • 原文地址:https://www.cnblogs.com/xdr630/p/15255107.html
Copyright © 2011-2022 走看看