java - Sessions in Spring MVC -
i know how sessions gets used in spring mvc retain value until user logged in.
somewhere found model.addattribute("session_name",username);
creating session & access ${username}
in jsp page. real way web can define & access username variable in pages?
how can check if session variable name username
exists? in jsp used use
if(null == session.getattribute("username")){ // user not logged in. }else{ // user logged in. }
how can check in spring mvc ?
session created spring container when need put in arguments of controllers method , injected. user login example
@requestmapping(value=("login"),method=requestmethod.post) public string login(@requestparam("name")string name,@requestparam("pass")string pass,@requestparam("gname")string gname,httpsession session ) { user u1=new user(); u1.setname(name); u1.setgname(gname); u1.setpass(pass); user u2=dao.findbyid(name); if(u2!=null) { if(u1.getpass().equals(u2.getpass())&&u1.getgname().equals(u2.getgname())) { session.setattribute("user", u1); return "welcome"; } else { return "error"; } } else { return "error"; } }
now can in jsp page following way:
if(session.getattribute("user")!=null) { user user=(user)session.getattribute("user"); } else { out.print("log in please"); }
Comments
Post a Comment