This question can be solved by googling, so did i but since i am new to servlet technology, i am not able to resolve the issue.
I need to assign session to particular user, session will expire in 10 second and when session is expired, user will be forwarded to login.html page again.
I read a bit about it but i am not getting the callback when session expires. Here is my try.
Login servlet
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String userName = request.getParameter("user_id");
String pwd = request.getParameter("pwd");
PrintWriter out = response.getWriter();
if(userName.equalsIgnoreCase(pwd)){
HttpSession session = request.getSession();
session.setMaxInactiveInterval(10);
ActiveUser mActiveUser = new ActiveUser();
mActiveUser.setUserName(userName);
session.setAttribute("userName", mActiveUser);
RequestDispatcher rd = request.getRequestDispatcher("welcome.html");
rd.forward(request, response);
}else{
response.setContentType("text/html");
out.print("UserName and password did not match, Please try again");
RequestDispatcher dispatchToIndex = request.getRequestDispatcher("index.html");
dispatchToIndex.include(request, response);
}
}
ActiveUser.java
@WebListener
public class ActiveUser implements HttpSessionBindingListener,
HttpSessionListener {
//overridden methods of the interfaces are included, and simple print statement is there to get know about callback is received or not.
Problem: When session expires(time-out) after 10 second i am not getting callback as required, but if i am sending a request after 10 second it correctly gets to know that previous session is expired and i receive callback on ActiveUser->sessionDestroyed() and
ActiveUser->valueUnbound() and HttpSessionAttributeListener->attributeRemoved()
thanks in advance