首页 > 其他 > 详细

Session笔记

时间:2018-01-22 20:57:05      阅读:262      评论:0      收藏:0      [点我收藏+]

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);
    }
}

 

Session笔记

原文:https://www.cnblogs.com/jepson6669/p/8330989.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!