• cookie的使用


    关于cookie的使用,实现30天免登陆,也就是记住密码的功能

    这是登录之后,网页内cookie的相关信息

    其次就是,在用户第一次登录时,如果选择了勾选自动登录按钮,cookie就会保存用户的信息,当用户再次登录时,就可以不用输入密码,自动登录了。

    代码的相关实现

    myServlet.java

    package com.my.deal;
    
    import java.io.IOException;
    import java.sql.Connection;
    import java.sql.DriverManager;
    import java.sql.ResultSet;
    import java.sql.SQLException;
    import java.sql.Statement;
    
    import javax.servlet.ServletException;
    import javax.servlet.annotation.WebServlet;
    import javax.servlet.http.Cookie;
    import javax.servlet.http.HttpServlet;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    
    /**
     * 登录界面
     */
    @WebServlet("/MyServlet")
    public class MyServlet extends HttpServlet {
        /**
         * 
         */
        private static final long serialVersionUID = 1L;
        private String driverName="com.mysql.jdbc.Driver";
        private String url="jdbc:mysql://localhost:3306/test?useSSL=false&serverTimezone=UTC";
        private String name="root";
        private String pwd="123456";
        protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            doPost(request, response);
            
        }
    
        protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            String username = request.getParameter("username");
            String password = request.getParameter("password");
            String flag = request.getParameter("isLogin");
    
            //设置页面编码格式
            response.setContentType("text/html;charset=UTF-8");
            String sql="select * from users where name='"+username+"' and password='"+password+"'";
            
            System.out.println(sql);
            Connection connection=null;
            Statement statement=null;
            ResultSet resultSet=null;
            try {
                Class.forName(driverName);//加载驱动
                connection = DriverManager.getConnection(url, name, pwd);//获取连接
                statement = connection.createStatement();
                resultSet = statement.executeQuery(sql);
                System.out.println(statement.toString());
                
                //查找以前登录过得信息
                Cookie[] cookie=request.getCookies();
                if(cookie!=null){
                    for(int i=0;i<cookie.length;i++) {
                        if(cookie[i].getName().equals("username1")) {
                            if(password!=null)
                            {
                                response.sendRedirect("/check2/success.html");
                                return;
                            }
                            
                        }                
                    }
                }
                
                //新账号登录,判断是否存储cookie
                if(username!=null&&password!=null) {
                    if(resultSet.next()) {
                        if("autoCheck".equals(flag)) {
                            Cookie nameCookie =new Cookie("username1",username);
                            nameCookie.setMaxAge(3600*24*30);
                            Cookie pwdCookie =new Cookie("password",password);
                            pwdCookie.setMaxAge(3600*24*30);
                            response.addCookie(nameCookie);
                            response.addCookie(pwdCookie);  
                        }
                    response.setContentType("text/html;charset=UTF-8");
                    response.getWriter().write("欢迎你:"+username+"登录成功来到首页");
                    response.getWriter().write("<br/>");
                    response.getWriter().write("你的密码为:"+password);
                    }
                }
                else {
                    response.getWriter().write("用户名或者密码错误");
                }
    
            } catch (ClassNotFoundException | SQLException e) {
                e.printStackTrace();
            }
            try {
                resultSet.close();
                statement.close();
                connection.close();
            } catch (SQLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    
    }
    View Code

    完整代码已上传云盘,请下载预览》》链接: https://pan.baidu.com/s/18nZXiVhKYusMrJCezvR4Iw 提取码: ix5q 复制这段内容后打开百度网盘手机App,操作更方便哦

  • 相关阅读:
    部署openstack的官网文档解读mysql的配置文件
    ubuntu14.04行更新软件包
    Ubuntu14.04上修改主机名
    ubuntu上修改root密码
    在ISE查看各个模块消耗的资源
    132
    Aurora 8B/10B、PCIe 2.0、SRIO 2.0三种协议比较
    NAND flash和NOR flash的区别详解
    FPGA三分频,五分频,奇数分频
    以太网之物理层
  • 原文地址:https://www.cnblogs.com/funnn24/p/10640084.html
Copyright © 2020-2023  润新知