Collect parameters in session JEE

2

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
java.lang.NullPointerException
at HorarioJ2EE.Control.AddActividad.processRequest(AddActividad.java:70)
at HorarioJ2EE.Control.AddActividad.doPost(AddActividad.java:122)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:707)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:790)
at org.apache.catalina.core.StandardWrapper.service(StandardWrapper.java:1682)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:318)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:160)
at org.apache.catalina.core.StandardPipeline.doInvoke(StandardPipeline.java:734)
at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:673)
at com.sun.enterprise.web.WebPipeline.invoke(WebPipeline.java:99)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:174)
at org.apache.catalina.connector.CoyoteAdapter.doService(CoyoteAdapter.java:416)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:283)
at com.sun.enterprise.v3.services.impl.ContainerMapper$HttpHandlerCallable.call(ContainerMapper.java:459)
at com.sun.enterprise.v3.services.impl.ContainerMapper.service(ContainerMapper.java:167)
at org.glassfish.grizzly.http.server.HttpHandler.runService(HttpHandler.java:206)
at org.glassfish.grizzly.http.server.HttpHandler.doHandle(HttpHandler.java:180)
at org.glassfish.grizzly.http.server.HttpServerFilter.handleRead(HttpServerFilter.java:235)
at org.glassfish.grizzly.filterchain.ExecutorResolver$9.execute(ExecutorResolver.java:119)
at org.glassfish.grizzly.filterchain.DefaultFilterChain.executeFilter(DefaultFilterChain.java:283)
at org.glassfish.grizzly.filterchain.DefaultFilterChain.executeChainPart(DefaultFilterChain.java:200)
at org.glassfish.grizzly.filterchain.DefaultFilterChain.execute(DefaultFilterChain.java:132)
at org.glassfish.grizzly.filterchain.DefaultFilterChain.process(DefaultFilterChain.java:111)
at org.glassfish.grizzly.ProcessorExecutor.execute(ProcessorExecutor.java:77)
at org.glassfish.grizzly.nio.transport.TCPNIOTransport.fireIOEvent(TCPNIOTransport.java:536)
at org.glassfish.grizzly.strategies.AbstractIOStrategy.fireIOEvent(AbstractIOStrategy.java:112)
at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy.run0(WorkerThreadIOStrategy.java:117)
at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy.access$100(WorkerThreadIOStrategy.java:56)
at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy$WorkerThreadRunnable.run(WorkerThreadIOStrategy.java:137)
at org.glassfish.grizzly.threadpool.AbstractThreadPool$Worker.doWork(AbstractThreadPool.java:591)
at org.glassfish.grizzly.threadpool.AbstractThreadPool$Worker.run(AbstractThreadPool.java:571)
at java.lang.Thread.run(Thread.java:748)

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
source

1 answer

2
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