存储取Session的工具类


import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;


public class WebUtils {

    /**
     * 得到request
     * @return
     */
    public static HttpServletRequest getRequest(){
        ServletRequestAttributes requestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        HttpServletRequest request = requestAttributes.getRequest();
        return request;
    }

    /**
     * 得到session
     * @return
     */
    public static HttpSession getSession(){
        return getRequest().getSession();
    }

    public static ServletRequestAttributes getServletRequestAttributes() {
        return (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
    }

    /**
     * 得到当前线程的请求对象
     * @return
     */
    public static HttpServletRequest getHttpServletRequest() {
        return getServletRequestAttributes().getRequest();
    }

    /**
     * 得到session对象
     */
    public static HttpSession getHttpSession() {
        return getHttpServletRequest().getSession();
    }

}

使用方式

//往Session存值,以key-value形式存储
WebUtils.getSession().setAttribute("key","value");
//从Seesion中,按照key取出value值
WebUtils.getSession().getAttribute("key");
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要在页面中实现hutool工具类的图形验证码,你需要完成以下步骤: 1. 在你的项目中引入hutool工具类库,可以通过Maven或Gradle等构建工具来实现。 2. 在你的页面中添加一个img标签,用于显示验证码图片。 3. 在后端代码中,使用hutool工具类库生成验证码,并将验证码存储Session中,以便在验证时使用。 4. 将生成的验证码图片以Base64编码的形式返回给前端页面,以便显示。 下面是一个示例代码,演示了如何在页面中实现hutool工具类的图形验证码: ``` <!-- 页面中添加一个img标签用于显示验证码 --> <img id="captchaImg" src="" /> <!-- 在页面中添加一个按钮,用于刷新验证码 --> <button onclick="refreshCaptcha()">刷新验证码</button> <script> // 刷新验证码 function refreshCaptcha() { // 发送Ajax请求获取新的验证码 $.ajax({ url: "/captcha", type: "get", success: function(data) { // 将Base64编码的验证码图片显示在img标签中 $("#captchaImg").attr("src", "data:image/jpeg;base64," + data); } }); } </script> // 后端代码 // 生成验证码 CaptchaUtil.createLineCaptcha(width, height, codeCount, lineCount); // 将验证码存储Session中 HttpUtil.getSession().setAttribute("captcha", captcha.getCode()); // 将Base64编码的验证码图片返回给前端页面 return Base64.encode(captcha.getImageBytes()); ``` 注意:以上代码仅为示例,实际使用时需要根据自己的项目情况进行修改。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值