Home » Java » How to make use of session object which has been set in simple HTTP request with GET/POST in ajax request of java platform

How to make use of session object which has been set in simple HTTP request with GET/POST in ajax request of java platform

Posted by: admin July 12, 2018 Leave a comment

Questions:

Originally I am a php developer. Now i am working in java web project and trying to authenticate user. I set session after having correct username and password like below (request is simple HTTP post request no cross domain here)-

HttpSession session = request.getSession();
// setting userEntity
userEntity.setUserName(request.getParameter("userName"));
userEntity.setEmail(request.getParameter("email"));
userEntity.setPassword(request.getParameter("password"));
// if user provide correct user name and password 
if(user.authenticate(userEntity))
{
    session.setAttribute("userName", userEntity.getUserName());
    session.setAttribute("userId",userEntity.getId());
}

This session is accessible to all simple HTTP request GET/POST method but not possible in ajax Request.

I tried to access the session by request.getSession().getAttribute("userName"); Is there any way to share the session in ajax request and simple request. In php session is accessible to all request.

Answers: