• [原创]java WEB学习笔记29:Cookie Demo 之自动登录


    本博客为原创:综合 尚硅谷(http://www.atguigu.com)的系统教程(深表感谢)和 网络上的现有资源(博客,文档,图书等),资源的出处我会标明

    本博客的目的:①总结自己的学习过程,相当于学习笔记 ②将自己的经验分享给大家,相互学习,互相交流,不可商用

    内容难免出现问题,欢迎指正,交流,探讨,可以留言,也可以通过以下方式联系。

    本人互联网技术爱好者,互联网技术发烧友

    微博:伊直都在0221

    QQ:951226918

    -----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

    1. 自动登录 的需求

      ① 不需要填写用户名和密码等信息,可以自动登录到系统

      ②  login.jsp  hello.jsp

      

    login.jsp

     1 <%@ page language="java" contentType="text/html; charset=UTF-8"
     2     pageEncoding="UTF-8"%>
     3 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
     4 <html>
     5 <head>
     6 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
     7 <title>Insert title here</title>
     8 </head>
     9 <body>
    10 
    11 
    12     <form action="index.jsp">
    13         name:<input type="text" name="name"/>
    14         <input type="submit" value="submit"/>
    15     
    16     
    17     </form>
    18 
    19 </body>
    20 </html>

    hello.jsp

     1 <%@ page language="java" contentType="text/html; charset=UTF-8"
     2     pageEncoding="UTF-8"%>
     3 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
     4 <html>
     5 <head>
     6 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
     7 <title>自动登陆测试</title>
     8 </head>
     9 <body>
    10 
    11     <%
    12         //1.若可以获取到请求参数 name, 则打印出欢迎信息。把登录信息存储到 Cookie 中,并设置 Cookie 的最大时效为 30S
    13         String name  = request.getParameter("name");
    14         if(name != null && !name.trim().equals("")){
    15             
    16             //创建并且发送cookie
    17             Cookie cookie = new Cookie("loginName",name);
    18             cookie.setMaxAge(30);
    19             response.addCookie(cookie);
    20         }else{
    21         //2.从 Cookie 中读取用户信息,若存在则打印欢迎信息
    22             Cookie[] cookies = request.getCookies();
    23             if(cookies != null && cookies.length > 0){
    24                 for(Cookie cookie : cookies){
    25                     String cookieName = cookie.getName();
    26                     if("loginName".equals(cookieName)){
    27                         String value  = cookie.getValue();
    28                         name = value;
    29                         
    30                     }
    31                 }
    32             }
    33             
    34             
    35         }
    36         if(name != null && !name.trim().equals("")){
    37             out.print("欢迎登陆" + name);
    38         }else{
    39         //3.若既没有请求参数,也没有 Cookie,则重定向到 login.jsp
    40             request.getRequestDispatcher("/app-1/login.jsp");
    41         }
    42         
    43 
    44     
    45     
    46     
    47     %>
    48 </body>
    49 </html>
  • 相关阅读:
    数据包发送
    linux 进程调度3
    linux 进程调度2
    linux 进程调度1
    进程间通信:信号
    fork vfork clone学习
    跳表
    【转】Linux内存管理综述
    如何优雅的写出链表代码
    This function or variable may be unsafe Consider using xxx instead
  • 原文地址:https://www.cnblogs.com/jasonHome/p/5544420.html
Copyright © 2020-2023  润新知