0

I'm trying to learn how JavaEE. I've previously used JSP a little but I've never messed with scopes and servlets.

I've got a servlet that is called when a user submits a form, and a UserBean bean is created & added to the session.

UserBean userBean = new UserBean();
userBean.setLogin(user);
userBean(setPassword(pass);
request.getSession().setAttribute("user", userBean);

RequestDispatcher dispatcher = request.getRequestDispatcher("/index.jsp");
dispatcher.forward(request, response);

This forwards me to index.jsp, on which I display the value that I just set... index.jsp is just this:

${sessionScope.user.login}

This prints whatever I set the username to in the form. That's correct. That's great. Now I go back to mysite.com/index.jsp by typing the URL (not just refreshing - that's fine) and the information has gone. There's nothing. My bean has been lost. I thought a session meant an extended period of time. How can I retain my beans after the user goes away for 30 minutes and comes back by typing my URL?

I'm messing with a user account system so I would want the user to be able to leave and come back a few minutes later and not have to log in again. Is there a scope for this? I tried switching from sessionScope to dependent, but no difference.

IAmATree
  • 11
  • 2
  • request.getSession().getAttribute("user") will get you the session bean. – Robert Moskal May 29 '18 at 00:48
  • Possible duplicate of [Get JSF managed bean by name in any Servlet related class](https://stackoverflow.com/questions/2633112/get-jsf-managed-bean-by-name-in-any-servlet-related-class) – Robert Moskal May 29 '18 at 00:52
  • *How can I retain my beans after the user goes away for 30 minutes* this sounds like a server side session timeout - if tomcat check `.../conf.web.xml` ` -1 ` – Scary Wombat May 29 '18 at 00:54

1 Answers1

0

Figured it out. Thanks to the replies, I knew that it probably wasn't a misunderstanding on my part about what "session" means, and that there was an issue with the server.

After some more digging, I found out that the issue was because I was running the application on a server which I was accessing through just its IP address. I went through an actual domain instead and it works fine! Something about how browsers don't like accepting cookies from sites without a domain was supposedly making me create a new session whenever I reload.

IAmATree
  • 11
  • 2