保存数据到session:
-
@RequestMapping("/login")
-
//java项目 fhadmin.cn
-
public String login(HttpServletRequest request, @RequestParam("userName") String userName, @RequestParam("passWord") String passWord) {
-
String res = userService.check_userName(userName);
-
if (!res.equals("registed")) {
-
return "userName has not registed";
-
}
-
String str = userService.check_userName_and_passWord(userName, passWord);
-
if (str.equals("success")){
-
//第一步:获取session
-
HttpSession session = request.getSession();
-
//第二步:将想要保存到数据存入session中
-
session.setAttribute("userName",userName);
-
session.setAttribute("password",passWord);
-
//这样就完成了用户名和密码保存到session的操作
-
}
-
return str;
-
}
从session获取保存到数据:
-
@RequestMapping("/index")
-
//java项目 fhadmin.cn
-
public class IndexController {
-
-
@Autowired
-
HttpServletRequest request; //通过注解获取一个request
-
-
@RequestMapping("/getName")
-
public String getName() {
-
//通过request来获取保存在session中的用户名
-
String userName = (String) request.getSession().getAttribute("userName");
-
System.out.println(userName);
-
return userName;
-
}
-
}
阅读(910) | 评论(0) | 转发(0) |