spring-07-Controller如何使用Session

@RequestMapping("/login3.do")
    public String checkLogin3(
            String username,
            String password,
            ModelMap model,
            HttpSession session){//加session參數
        if("root".equals(username) && "123".equals(password)){
            model.put("username", username);
            //給session賦值
            session.setAttribute("username", username);
            return "ok";
        }else{
            model.put("msg", "用戶名或密碼錯誤");
            return "login";
        }
    }

<%@page pageEncoding="utf-8"
contentType="text/html; charset=utf-8"%>
<html>
  <head></head>
  <body style = "font-size:30px;">
    <h2>${username}登錄成功</h2>
    session的內容:${sessionScope.username}
  </body>
</html>
發佈了33 篇原創文章 · 獲贊 2 · 訪問量 1萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章