Session笔记

1 Session的创建

package p7.session;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

public class CreateSessionServlet extends HttpServlet {

    public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        /* 
        * request.getSession()方法内部会判断 该客户端是否在服务器端已经存在 session
        * 如果该客户端在此服务器不存在 session 那么就会创建一个新的 session 对象
        * 如果该客户端在此服务器已经存在 session 获得已经存在的该 session 返回
        */
        // 1  创建属于该客户端(会话)的私有的 session 区域
        HttpSession session = request.getSession();
        
        // 2 向session域中存入数据
        session.setAttribute("name", "jerry");
        
        // 3 获取该session对象的JSESSIONID编号
        String id = session.getId();
        
        //解决乱码问题
        response.setContentType("text/html;charset=UTF-8");
        response.getWriter().write("你的JSESSIONID是:"+id);
    }

    public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }

}

2 从Session中获取数据

package p7.session;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

public class GetDataFromSessionServlet extends HttpServlet {

    public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        // 从 session 中获得存储的数据
        // 已经存在了,拿着的是客户端携带过来的JSESSIONID去找对应的区域。
        HttpSession session = request.getSession();
        
        // 从Session域中获取数据
        Object attribute = session.getAttribute("name");
        
        // 解决乱码   输出。
        response.setContentType("text/html;charset=UTF-8");
        response.getWriter().write(attribute+"");
        
    }

    public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }

}

3 JSESSIONID的持久化操作

package p7.session;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

public class CreateSessionServlet extends HttpServlet {

    public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        /* 
        * request.getSession()方法内部会判断 该客户端是否在服务器端已经存在 session
        * 如果该客户端在此服务器不存在 session 那么就会创建一个新的 session 对象
        * 如果该客户端在此服务器已经存在 session 获得已经存在的该 session 返回
        */
        // 1  创建属于该客户端(会话)的私有的 session 区域
        HttpSession session = request.getSession();
        
        // 2 向session域中存入数据
        session.setAttribute("name", "李四");
        
        // 3 获取该session对象的JSESSIONID编号
        String id = session.getId();
        
        // 4 手动创建一个存储 JSESSIONID 的 Cookie 为该 cookie 设置持久化时间
        Cookie cookie = new Cookie("JSESSIONID",id);
        cookie.setMaxAge(6000);
        cookie.setPath("/WEBTest/");
        
        
        //解决乱码问题
        response.setContentType("text/html;charset=UTF-8");
        response.getWriter().write("你的JSESSIONID是:"+id);
    }

    public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }

}

4 Session的生命周期

5 案例-验证码检验功能实现和总结

package login;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class LoginServlet extends HttpServlet {

    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        
        request.setCharacterEncoding("UTF-8");
        
        //验证码校验
        //获得页面输入的验证
        String checkCode_client = request.getParameter("checkCode");
        //获得生成图片的文字的验证码
        String checkCode_session = (String) request.getSession().getAttribute("checkcode_session");
        //比对页面的和生成图片的文字的验证码是否一致
        if(!checkCode_session.equals(checkCode_client)){
            request.setAttribute("loginInfo", "您的验证码不正确");
            request.getRequestDispatcher("/login.jsp").forward(request, response);
            return;
        }
        
        
        //获得页面的用户名和密码进行数据库的校验
        //......
        
    }

    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        doGet(request, response);
    }
}

 

posted @ 2018-01-22 20:29  Jepson6669  阅读(151)  评论(0编辑  收藏  举报