1. 程式人生 > >jsp頁面獲取session中值的方式

jsp頁面獲取session中值的方式

jsp頁面獲取session值

java程式碼

 @RequestMapping(value = "/chkUser",method = RequestMethod.POST,produces = "application/json;charset=UTF-8")
    public String chkUserInfo(HttpServletRequest request,String userName, String userPwd){
        if(StringUtils.isBlank(userName)||StringUtils.isBlank(userPwd))
            return
"false"; UserEntity entity = chkLoginService.chkUserService(userName,userPwd); if(entity==null) return "false"; request.getSession().setAttribute("userEntity",entity); return "true"; }

方法一
jsp頁面
使用 request.getSession().getAttribute(“**“) 方法
方法一使用getAttribute

  • 方法二

在jsp頁面 script中使用EL表示式獲取
var userEntity=’${sessionScope.userEntity.loginName}’;
說明
1、sessionScope指的是session的範圍,類似還有requestScope,pageScope,contextScope
2、sessionScope整體的意思是獲得存放在session.setAttrbute(key,value)的值即session.getAttribute(key)