@Controller
@SessionAttributes( {"user"})
@RequestMapping("/")
public class Logincontroller {
@Autowired
public Loginservice loginservice;
@RequestMapping(method = RequestMethod.GET)
public ModelAndView getPage1() {
ModelAndView view = new ModelAndView("login");
return view;
}
@Autowired(required = true)
@Qualifier(value="Userprofileservice")
public Userprofileservice userprofile;
@RequestMapping(value = "/processLogin", method = RequestMethod.POST)
public ModelAndView processLogin(HttpServletRequest request,
HttpServletResponse response) {
String userName=request.getParameter("username");
String password=request.getParameter("password");
if (loginservice.checklogin(userName,password)) {
/* ModelAndView success = new ModelAndView("success");*/
ModelAndView success = new ModelAndView("success");
List<Users_main> user = userprofile.userdetails(userName);
success.addObject("username", userName);
success.addObject("user",user);
return success;
} else {
ModelAndView error = new ModelAndView("error");
return error;
}
}
}
This is my controller code. My idea is to keep success.jsp page after successful login & keep logged in even after refresh also. But when I hit "/" it is going to login. How can redirect it to success.jsp page even after refresh in same session? I tried with session attribute in controller but it didn't seems to be working
You can directly use HttpSession which is comes from javax.servlet
api.
For creating new session you can call
HttpSession session = request.getSession(true);
This will return a new session object. So put any object or value in the session in such that,
session.setAttribute("user", user);
Assuming that your user
object is returned from service layer.
Now you can access the user object anytime (if session timeout doesn't occur) in the whole project by
HttpSession session = request.getSession(false);
Here we set boolean false
confirm that if any session are still alive in the application context for the request, then returns it. Now accessing the session value by
User user = (User)session.getAttribute("user");
So modify your controller with that and try again the code below,
@RequestMapping(value = "/processLogin", method = RequestMethod.POST)
public ModelAndView processLogin(HttpServletRequest request,HttpServletResponse response) {
String userName=request.getParameter("username");
String password=request.getParameter("password");
if (loginservice.checklogin(userName,password)) {
/* ModelAndView success = new ModelAndView("success");*/
HttpSession session = request.getSession(true);
ModelAndView success = new ModelAndView("success");
List<Users_main> user = userprofile.userdetails(userName);
success.addObject("username", userName);
success.addObject("user",user);
session.setAttribute("user",user);
return success;
} else {
ModelAndView error = new ModelAndView("error");
return error;
}
}
Let me know the status.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.