Web 基础13 Session技术简介

2018-03-30  本文已影响6人  小熊先生很不开心

1.1 Session 简介

1.1.1 cookie和session的区别

1.1.2 实现原理

session实现的原理.png

1.2 Session方法

1.2.1 细节



//获取session对象,如果客户端没有对应的session对象,则创建新的session对象并返回
HttpSession session = request.getSession();

//设置数据
session.setAttribute("username", "wangwu");

//获取数据
session.getAttribute("username");

//删除数据
session.removeAttribute("username");
    

共享对象

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        /*
         * 使用session对象实现数据的共享
         *              域对象,整个会话
         */
        
        //获取session对象
        //获取session对象,如果客户端没有对应的session对象,则创建新的session对象并返回
        HttpSession session = request.getSession();
        
        //获取session对象
        //参数是true,如果客户端没有对应的session对象,则创建新的session对象并返回
        //参数是false,如果客户端没有对应的session对象,则返回null
        //request.getSession(create);
        
        //设置数据
        session.setAttribute("username", "wangwu");
        
        //删除数据
        //session.removeAttribute("username");
        
        //获取数据
        Object obj = session.getAttribute("username");
        System.out.println("SessionDemo2:" + obj);
        
    }
    
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //获取session对象
        HttpSession session = request.getSession();
        //获取数据
        Object obj = session.getAttribute("username");
        System.out.println("SessionDemo3:" + obj);
    
    }
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //获取session对象
        HttpSession session = request.getSession();
        //设置数据
        //session.setAttribute("username", "lisi");
        User user = new User("itheima","qwerty");
        session.setAttribute("user", user);
    }
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //获取session对象
        HttpSession session = request.getSession();
        //获取数据
        //Object obj = session.getAttribute("username");
        //System.out.println(obj);
        
        Object obj = session.getAttribute("user");
        System.out.println(obj);
    }

1.3 案例详解

1.3.1 初始化数据

创建User对象
用户类 User
模拟数据库 UserDB

public class User implements Serializable {
    private String username;
    private String password;
    
    public User() {
        super();
        // TODO Auto-generated constructor stub
    }
    public User(String username, String password) {
        super();
        this.username = username;
        this.password = password;
    }
    public String getUsername() {
        return username;
    }
    public void setUsername(String username) {
        this.username = username;
    }
    public String getPassword() {
        return password;
    }
    public void setPassword(String password) {
        this.password = password;
    }
    @Override
    public String toString() {
        return "User [username=" + username + ", password=" + password + "]";
    }
    
    
    
}

初始化数据

public class UserDB {
    private static List<User> users = new ArrayList<User>();
    
    static {
        users.add(new User("itcast","123456"));
        users.add(new User("itheima","qwerty"));
        users.add(new User("zhangsan","password"));
    }
    
    public static List<User> getUsers() {
        return users;
    }
}

登录页面

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //设置响应的类型和编码
        response.setContentType("text/html;charset=UTF-8");
        //获取输出流对象
        PrintWriter out = response.getWriter();
        
        //实现登陆页面
        out.println("<html>");
        
        out.println("<head>");
        out.println("<title>登陆页面</title>");
        out.println("</head>");
        
        out.println("<body>");
        out.println("<form action='/mySession/LoginServlet' method='post' >");
        out.println("用户名:<input type='text' name='username' />");
        out.println("<br />");
        out.println("密码:<input type='password' name='password' />");
        out.println("<br />");
        out.println("<input type='submit' value='登陆' />");
        out.println("</form>");
        out.println("</body>");
        
        out.println("</html>");
    }

登录功能

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //使用session实现用户登陆
        //设置响应的类型和编码
        response.setContentType("text/html;charset=UTF-8");
        //获取输出流对象
        PrintWriter out = response.getWriter();
        
        //获取用户名
        String username = request.getParameter("username");
        //获取密码
        String password = request.getParameter("password");
        
        //获取所有的用户数据
        List<User> users = UserDB.getUsers();
        //遍历集合
        for (User user : users) {
            //判断用户所输入的用户名和密码是否正确
            if(user.getUsername().equals(username) && user.getPassword().equals(password)) {
                out.println("登陆成功!3秒后跳转到首页!");
                response.setHeader("Refresh", "3;URL=/mySession/IndexUIServlet");
                
                //用户名和密码正确,登陆成功后将用户名添加到session对象中
                HttpSession session = request.getSession();
                session.setAttribute("username", username);
                
                return;
            }
        }
        
        out.println("用户名或者密码不正确!3秒后跳转到登陆页面!");
        response.setHeader("Refresh", "3;URL=/mySession/LoginUIServlet");
    }

注销功能

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //设置响应的类型和编码
        response.setContentType("text/html;charset=UTF-8");
        //获取输出流对象
        PrintWriter out = response.getWriter();
        
        //获取session对象
        HttpSession session = request.getSession();
        
        //删除数据,删除用户在session中的数据
        //session.removeAttribute("username");

        //销毁session对象
        session.invalidate();
        
        out.println("注销成功!3秒后跳转到首页!");
        response.setHeader("Refresh", "3;URL=/mySession/IndexUIServlet");
        
    }

根据session内容响应

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //设置响应的类型和编码
        response.setContentType("text/html;charset=UTF-8");
        //获取输出流对象
        PrintWriter out = response.getWriter();
        //获取session对象
        HttpSession session = request.getSession();
        //获取用户的登陆状态
        Object username = session.getAttribute("username");
        
        //实现首页
        out.println("<html>");
        
        out.println("<head>");
        out.println("<title>首页</title>");
        out.println("</head>");
        
        out.println("<body>");
        out.println("<h1>itheima</h1>");
        if(username == null) {
            //username为null,说明session中没有用户的数据,用户没有登陆成功
            out.println("<a href='/mySession/LoginUIServlet' >登陆</a>");
        }
        else {
            //username不为null,session中有用户的数据,用户已经登陆成功了
            out.println("欢迎您" + username);
            out.println("<a href='/mySession/LogoutServlet'>注销</a>");
        }
        out.println("<hr />");
        out.println("</body>");
        
        out.println("</html>");
    }

1.3 Session的补充

1.3.1 session对象的生命周期

1.3.2 钝化

上一篇 下一篇

猜你喜欢

热点阅读