Collect parameters in session JEE


I have a small application where the first thing that appears is a login form. Where if the user correctly enters the data access to the menu page where you can save some records in the database. The problem is that I am not saving the session correctly or that I am accessing it in an erroneous way since when I try to access it gives me a fault (NullPointerException).

I save the session through the login session servlet. That is, with the data that the user puts in, I pick them up with the request and I call a method that checks if the user data and password are in the database. If it exists, save the session and access menu.jsp. I keep it this way:

session = request.getSession(true);
session.setAttribute("user", user);
session.setAttribute("pass", pass);
session.setAttribute("obj", usuObj);

At that moment the user is in the menu and in toeria, the session is created. Then on the page insert a record in the bd. I want to access the object that I added to the session.

usuObj = (usuarioObj) getServletContext().getAttribute("obj");
id_usu = usuObj.getIdUsu();

But it gives me failure in that line:

Advertencia:   StandardWrapperValve[AddActividad]: Servlet.service() for servlet AddActividad threw exception
at HorarioJ2EE.Control.AddActividad.processRequest(
at HorarioJ2EE.Control.AddActividad.doPost(
at javax.servlet.http.HttpServlet.service(
at javax.servlet.http.HttpServlet.service(
at org.apache.catalina.core.StandardWrapper.service(
at org.apache.catalina.core.StandardWrapperValve.invoke(
at org.apache.catalina.core.StandardContextValve.invoke(
at org.apache.catalina.core.StandardPipeline.doInvoke(
at org.apache.catalina.core.StandardPipeline.invoke(
at com.sun.enterprise.web.WebPipeline.invoke(
at org.apache.catalina.core.StandardHostValve.invoke(
at org.apache.catalina.connector.CoyoteAdapter.doService(
at org.apache.catalina.connector.CoyoteAdapter.service(
at org.glassfish.grizzly.http.server.HttpHandler.runService(
at org.glassfish.grizzly.http.server.HttpHandler.doHandle(
at org.glassfish.grizzly.http.server.HttpServerFilter.handleRead(
at org.glassfish.grizzly.filterchain.ExecutorResolver$9.execute(
at org.glassfish.grizzly.filterchain.DefaultFilterChain.executeFilter(
at org.glassfish.grizzly.filterchain.DefaultFilterChain.executeChainPart(
at org.glassfish.grizzly.filterchain.DefaultFilterChain.execute(
at org.glassfish.grizzly.filterchain.DefaultFilterChain.process(
at org.glassfish.grizzly.ProcessorExecutor.execute(
at org.glassfish.grizzly.nio.transport.TCPNIOTransport.fireIOEvent(
at org.glassfish.grizzly.strategies.AbstractIOStrategy.fireIOEvent(
at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy.run0(
at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy.access$100(
at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy$
at org.glassfish.grizzly.threadpool.AbstractThreadPool$Worker.doWork(
at org.glassfish.grizzly.threadpool.AbstractThreadPool$

Then I do not know if the failure is in which I do not keep the session well, or if the session is created but as soon as I exit the servlet and redirect to the menu the session disappears or if I'm not accessing as it should.

Finally, in the servlet where I have the problem I have this line of code:

session = request.getSession(true);
asked by Eduardo 26.02.2018 в 17:26

1 answer

usuObj = (usuarioObj) getServletContext().getAttribute("obj");

The ServletContext is not the same as the Session ... In fact, the ServletContext is common for all the webapp

usuObj = (usuarioObj) request.getSession(true).getAttribute("obj");

UPDATE Sorry for the delay, but what you tell me does not fit me, so here is a plan to review possibilities:

  • I did not say it in the comment, but the System.out.println(request.getSession(false)) should do it before of usuObj = (usuarioObj) request.getSession(true).getAttribute("obj"); .

    The difference is that if you do it later, when doing request.getSession(true) you create the session if it did not exist before; the idea of System.out.println is to see if -for some reason- the session where you saved the attributes has disappeared.

  • If point 1. does not clarify anything, it might be useful to assign listeners :

  • Finally, remember that if you solve the question on your own, it is allowed (and it is appreciated) that you write and accept your own answer so that you can help other people who have the same problem.

    answered by 26.02.2018 в 17:52