0

I am facing a problem regarding to the Httpsession that I implementing in the Spring MVC project.

First of all, after the user successfully login, I will take the Httpsession object in loginAuthentication controller and set attribute with the name and value I want. (Shown in following figure).

A.java controller file,

@RequestMapping(value="login-authentication", method = RequestMethod.POST)
    public String authentication(@Valid @ModelAttribute("systemAccount") SystemAccount systemAccount,
                                 BindingResult bindingResult, Model model, HttpServletRequest request){
        if (bindingResult.hasErrors()) {
            model.addAttribute(GenericConstant.MessageAttributeName.ERROR_MSG_NAME.toValue(), SystemMessage.SystemException.LOGIN_INCORRECT_USERNAME_PASSWORD.toValue());
            model.addAttribute("systemAccount", new SystemAccount());
            return "index";
        }else {
            if (systemAccountService.authenticate(systemAccount.getUsername(), systemAccount.getPassword()) != null &&
                    !"".equals(systemAccountService.authenticate(systemAccount.getUsername(), systemAccount.getPassword()))) {

                SystemAccount dbSystemAccount = systemAccountService.authenticate(systemAccount.getUsername(), systemAccount.getPassword());

                request.setAttribute(SessionAttribute.AttributeName.LOGIN_ACC_ID.toValue(),dbSystemAccount.getAccountID());
                //check account role
                if(dbSystemAccount.getCounterStaff()!= null && !"".equals(dbSystemAccount.getCounterStaff())){
                    CounterStaff counterStaff =  dbSystemAccount.getCounterStaff();
                    request.setAttribute(SessionAttribute.AttributeName.LOGIN_ACC_NAME.toValue(), counterStaff.getStaffName());
                    request.setAttribute(SessionAttribute.AttributeName.LOGIN_ACC_ROLE.toValue(), GenericConstant.SystemRole.COUNTER_STAFF.toValue());

                }else if(dbSystemAccount.getCustomer()!= null && !"".equals(dbSystemAccount.getCustomer())){
                    Customer customer = dbSystemAccount.getCustomer();
                    request.setAttribute(SessionAttribute.AttributeName.LOGIN_ACC_NAME.toValue(), customer.getCustomerName());
                    request.setAttribute(SessionAttribute.AttributeName.LOGIN_ACC_ROLE.toValue(), GenericConstant.SystemRole.CUSTOMER.toValue());

                }else if(dbSystemAccount.getManager()!= null && !"".equals(dbSystemAccount.getManager())){
                    Manager manager = dbSystemAccount.getManager();
                    request.setAttribute(SessionAttribute.AttributeName.LOGIN_ACC_NAME.toValue(), manager.getManagerName());
                    request.setAttribute(SessionAttribute.AttributeName.LOGIN_ACC_ROLE.toValue(), GenericConstant.SystemRole.MANAGER.toValue());

                }else if(dbSystemAccount.getDoctor()!= null && !"".equals(dbSystemAccount.getCounterStaff())){
                    Doctor doctor = dbSystemAccount.getDoctor();
                    request.setAttribute(SessionAttribute.AttributeName.LOGIN_ACC_NAME.toValue(), doctor.getDoctorName());
                    request.setAttribute(SessionAttribute.AttributeName.LOGIN_ACC_ROLE.toValue(), GenericConstant.SystemRole.DOCTOR.toValue());
                }

                request.setAttribute(SessionAttribute.AttributeName.LOGIN_DATE.toValue(), DateTimeUtil.getCurrentDate());
                return "mainPage";
            }else {
                model.addAttribute(GenericConstant.MessageAttributeName.ERROR_MSG_NAME.toValue(), SystemMessage.SystemException.LOGIN_INCORRECT_USERNAME_PASSWORD);
                model.addAttribute("systemAccount", new SystemAccount());
                return "index";
            }
        }
    }

After everything is ready, the controller will navigate user to the main page and the main page able to access all the defined variable without issue. (The following figure shown the controller that mapped with mainPage).

A.java controller file,

@RequestMapping(value = "/mainPage", method = RequestMethod.GET)
    public String renderMainPageView(Model model, HttpServletRequest request) {
        if(request.getAttribute(SessionAttribute.AttributeName.LOGIN_CHECK.toValue()) != null) {
            model.addAttribute(SessionAttribute.AttributeName.LOGIN_ACC_ID.toValue(),
                    request.getAttribute(SessionAttribute.AttributeName.LOGIN_ACC_ID.toValue()));
            model.addAttribute(SessionAttribute.AttributeName.LOGIN_ACC_NAME.toValue(),
                    request.getAttribute(SessionAttribute.AttributeName.LOGIN_ACC_NAME.toValue()));
            model.addAttribute(SessionAttribute.AttributeName.LOGIN_ACC_ROLE.toValue(),
                    request.getAttribute(SessionAttribute.AttributeName.LOGIN_ACC_ROLE.toValue()));
            model.addAttribute(SessionAttribute.AttributeName.LOGIN_DATE.toValue(),
                    request.getAttribute(SessionAttribute.AttributeName.LOGIN_DATE.toValue()));
            return "mainPage";
        }else {
            model.addAttribute("systemAccount", new SystemAccount());
            return "index";
        }
    }

In the navigation menu of main page, I click on the selection to direct me to add manager web page. (The following shown the link).

<a href="addManager" target="ifrm" >Add New Account</a>

The controller that mapped with the link (GET) able to detect. However, this controller (renderAddManagerView) does not recognised the HTTP session that I defined earlier when I try to access using the getAttribute method in the if condition. It keep showing null value (Shown in the following figure.

B.java controller file,

@RequestMapping(value = "/addManager", method = RequestMethod.GET)
    public String renderAddManagerView(Model model, HttpSession httpSession) {
        if(httpSession.getAttribute(SessionAttribute.AttributeName.LOGIN_CHECK.toValue()) != null) {
            model.addAttribute("manager", new Manager());
            model.addAttribute(FormSelectionValue.FormSelectionAttributeName.COUNTRY_SELECTION.toValue(), FormSelectionValue.COUNTRY_SELECTION_LIST);
            model.addAttribute(FormSelectionValue.FormSelectionAttributeName.GENDER_SELECTION.toValue(), FormSelectionValue.GENDER_SELECTION_LIST);
            return "addManager";
        }else {
            model.addAttribute("systemAccount", new SystemAccount());
            return "index";
        }
    }

So I am not sure what is the issue for my code and there is no error message is displayed.

1
  • Add code as text not as Image Commented Feb 29, 2020 at 13:38

1 Answer 1

1

I have solved the issue by using the HttpServletRequest instead of HttpSession. Now my session will not be loss even redirect or navigate to any pages in JSP.

Something like this:

@RequestMapping("/renderview", method = RequestMethod.GET)
@Controller
public class TestController {
    @RequestMapping(method = RequestMethod.GET)
    public String myMethod(HttpServletRequest request)
    {
        request.getSession().setAttribute("mySession", "XXX");
        return "jspview";
    }
}

Reference: Set session variable spring mvc 3

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.