• Web开发-session介绍


    session介绍

    • session 可以被看作是一种缓冲区,用于在多个请求之间存储和传递用户数据。
    • 在 Web 应用程序中,session 通常用于存储用户登录信息、购物车数据、用户偏好设置等。
    • 当用户在应用程序中进行多个请求时,session 可以确保数据在这些请求之间保持一致性,从而提高应用程序的效率和用户体验。

    session使用场景

    • 验证码生成器通过将生成的验证码以特定名称作为键值对存储到session中。在生成验证码后,将验证码放入到request.get.session.set.attribute中,其中键的名称是传入的method参数,而值是验证码generator。

    session具体使用

    package hanshuhuan.test.controller.common;
    
    import hanshuhuan.test.util.CpachaUtil;
    
    import java.io.IOException;
    
    import javax.imageio.ImageIO;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    
    import org.slf4j.LoggerFactory;
    import org.springframework.stereotype.Controller;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RequestMethod;
    import org.springframework.web.bind.annotation.RequestParam;
    
    /**
     * 系统验证码公用控制器
     * @author shuhu
     *
     */
    @Controller
    @RequestMapping("/cpacha")
    public class CpachaController {
    
    	private org.slf4j.Logger log=LoggerFactory.getLogger(CpachaController.class);
    	/**
    	 * 通用验证码生成器
    	 * @param vcodeLength
    	 * @param fontSize
    	 * @param width
    	 * @param height
    	 * @param method
    	 * @param request
    	 * @param response
    	 */
    	@RequestMapping(value="/generate_cpacha",method=RequestMethod.GET)
    	public void generateCpacha(
    			@RequestParam(name="vl",defaultValue="4")Integer vcodeLength,//验证码长度
    			@RequestParam(name="fs",defaultValue="21")Integer fontSize,//验证码字体大小
    			@RequestParam(name="w",defaultValue="98")Integer width,//验证码图片宽度
    			@RequestParam(name="h",defaultValue="33")Integer height,//验证码图片高度
    			@RequestParam(name="method",defaultValue="admin_login")String method,//以此名称为键,存储到session中
    			HttpServletRequest request,
    			HttpServletResponse response){
    		CpachaUtil cpachaUtil=new CpachaUtil(vcodeLength,fontSize,width,height);		
    		String generatorVCode = cpachaUtil.generatorVCode();
    		//将生成的验证码放入session,后面验证验证码是否正确时使用
    		request.getSession().setAttribute(method, generatorVCode);
    		log.info("成功生成验证码,method="+method+",value="+generatorVCode);
    		try {
    			ImageIO.write(cpachaUtil.generatorRotateVCodeImage(generatorVCode, true), "gif", response.getOutputStream());
    		} catch (IOException e) {
    			// TODO Auto-generated catch block
    			e.printStackTrace();
    		}
    	}
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58

    需要注意的是

    • request.getSession().setAttribute方法设置的属性值仅在当前会话中有效。当客户端发起新的请求时,之前设置的属性值将不再可用。如果需要在多个请求之间保持属性值,可以考虑使用其他方法,例如将属性值存储在数据库或服务器端内存中。
  • 相关阅读:
    【小沐学Python】网络爬虫之urllib
    华为笔试面试题
    LyScript 实现Hook隐藏调试器
    Http客户端Feign
    剑指 Offer II 018. 有效的回文
    05节-51单片机-模块化编程
    在Windows系统中查找GitBash安装位置
    【UML】类图Class Diagram
    Kubernetes平台部署Grafana Loki Promtail系统
    DMSQL学习笔记
  • 原文地址:https://blog.csdn.net/weixin_45880844/article/details/133466801