-1

I am trying to update my db entity via a simple java servlet. In my doGet methosd of UpdateFooServlet I am using a query param which is an id of my Foo, I get that Foo object by id from my db and set it as a 'foo' attribute in my request. Later on, in my doPost method I am trying to get my request attribute foo (in order to write the differences, but it's another part of the application not needed to be here) and once I try to do so, I get NullPointerExeption. I tried to set it as a session attribute as well, just like I did with ,y user and it didn't work either. Where did i make a mistake?

@WebServlet("/UpdateFooServlet")
public class UpdateFooServlet extends HttpServlet {

    FooDAO fooDao = new FooDAO();

    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        int id = Integer.parseInt(request.getParameter("id"));
        Foo foo = fooDao.getFooById(id);
        System.out.println(foo.toString());
        request.setAttribute("foo", foo);
        RequestDispatcher dispatcher = request.getRequestDispatcher("updatefoo.jsp");
        dispatcher.include(request, response);
    }

    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

        Foo notUpdatedFoo = (Foo) request.getAttribute("foo");
        System.out.println(notUpdatedFoo.toString());

I use System.out in order to check whether the very same Foo was extracted either from db or request attribute.

Logs:

ERROR [io.undertow.request] (default task-7) UT005023: Exception handling request to /tasks-manager/UpdateFooServlet: java.lang.NullPointerException
    at servlets.UpdateFooServlet.doPost(UpdateFooServlet.java:38)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:707)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:790)
    at io.undertow.servlet.handlers.ServletHandler.handleRequest(ServletHandler.java:85)
    at io.undertow.servlet.handlers.FilterHandler$FilterChainImpl.doFilter(FilterHandler.java:129)
    at filters.AuthenticationFilter.doFilter(AuthenticationFilter.java:38)
    at io.undertow.servlet.core.ManagedFilter.doFilter(ManagedFilter.java:61)
    at io.undertow.servlet.handlers.FilterHandler$FilterChainImpl.doFilter(FilterHandler.java:131)
    at io.undertow.servlet.handlers.FilterHandler.handleRequest(FilterHandler.java:84)
    at io.undertow.servlet.handlers.security.ServletSecurityRoleHandler.handleRequest(ServletSecurityRoleHandler.java:62)
    at io.undertow.servlet.handlers.ServletDispatchingHandler.handleRequest(ServletDispatchingHandler.java:36)
    at org.wildfly.extension.undertow.security.SecurityContextAssociationHandler.handleRequest(SecurityContextAssociationHandler.java:78)
    at io.undertow.server.handlers.PredicateHandler.handleRequest(PredicateHandler.java:43)
    at io.undertow.servlet.handlers.security.SSLInformationAssociationHandler.handleRequest(SSLInformationAssociationHandler.java:131)
    at io.undertow.servlet.handlers.security.ServletAuthenticationCallHandler.handleRequest(ServletAuthenticationCallHandler.java:57)
    at io.undertow.server.handlers.PredicateHandler.handleRequest(PredicateHandler.java:43)
    at io.undertow.security.handlers.AbstractConfidentialityHandler.handleRequest(AbstractConfidentialityHandler.java:46)
    at io.undertow.servlet.handlers.security.ServletConfidentialityConstraintHandler.handleRequest(ServletConfidentialityConstraintHandler.java:64)
    at io.undertow.security.handlers.AuthenticationMechanismsHandler.handleRequest(AuthenticationMechanismsHandler.java:60)
    at io.undertow.servlet.handlers.security.CachedAuthenticatedSessionHandler.handleRequest(CachedAuthenticatedSessionHandler.java:77)
    at io.undertow.security.handlers.NotificationReceiverHandler.handleRequest(NotificationReceiverHandler.java:50)
    at io.undertow.security.handlers.AbstractSecurityContextAssociationHandler.handleRequest(AbstractSecurityContextAssociationHandler.java:43)
    at io.undertow.server.handlers.PredicateHandler.handleRequest(PredicateHandler.java:43)
    at org.wildfly.extension.undertow.security.jacc.JACCContextIdHandler.handleRequest(JACCContextIdHandler.java:61)
    at io.undertow.server.handlers.PredicateHandler.handleRequest(PredicateHandler.java:43)
    at io.undertow.server.handlers.PredicateHandler.handleRequest(PredicateHandler.java:43)
    at io.undertow.servlet.handlers.ServletInitialHandler.handleFirstRequest(ServletInitialHandler.java:292)
    at io.undertow.servlet.handlers.ServletInitialHandler.access$100(ServletInitialHandler.java:81)
    at io.undertow.servlet.handlers.ServletInitialHandler$2.call(ServletInitialHandler.java:138)
    at io.undertow.servlet.handlers.ServletInitialHandler$2.call(ServletInitialHandler.java:135)
    at io.undertow.servlet.core.ServletRequestContextThreadSetupAction$1.call(ServletRequestContextThreadSetupAction.java:48)
    at io.undertow.servlet.core.ContextClassLoaderSetupAction$1.call(ContextClassLoaderSetupAction.java:43)
    at io.undertow.servlet.api.LegacyThreadSetupActionWrapper$1.call(LegacyThreadSetupActionWrapper.java:44)
    at io.undertow.servlet.api.LegacyThreadSetupActionWrapper$1.call(LegacyThreadSetupActionWrapper.java:44)
    at io.undertow.servlet.api.LegacyThreadSetupActionWrapper$1.call(LegacyThreadSetupActionWrapper.java:44)
    at io.undertow.servlet.api.LegacyThreadSetupActionWrapper$1.call(LegacyThreadSetupActionWrapper.java:44)
    at io.undertow.servlet.api.LegacyThreadSetupActionWrapper$1.call(LegacyThreadSetupActionWrapper.java:44)
    at io.undertow.servlet.handlers.ServletInitialHandler.dispatchRequest(ServletInitialHandler.java:272)
    at io.undertow.servlet.handlers.ServletInitialHandler.access$000(ServletInitialHandler.java:81)
    at io.undertow.servlet.handlers.ServletInitialHandler$1.handleRequest(ServletInitialHandler.java:104)
    at io.undertow.server.Connectors.executeRootHandler(Connectors.java:202)
    at io.undertow.server.HttpServerExchange$1.run(HttpServerExchange.java:805)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
    at java.lang.Thread.run(Thread.java:748)
Maurice Perry
  • 9,261
  • 2
  • 12
  • 24
jimmyrap
  • 99
  • 1
  • 4
  • 17

3 Answers3

0

A request object life span is exactly while the request is processed. Therefore, what you store in the request in a doGet() won't be there for the doPost() method.

Use a session attribute instead of a request attribute. That is in the doGet() do:

request.getSession().setAttribute("foo", foo);

And in the doPost() do:

Foo notUpdatedFoo = (Foo) request.getSession().getAttribute("foo");

BTW, this will work, but it's not thread safe.

Andres
  • 10,561
  • 4
  • 45
  • 63
0

I'm 100% sure I understand what you mean but:

If that servlet is first called by a GET-request (request 1) and you go to a JSP. From that JSP you call the servlet again? If so that is a new request (request 2) ,your attribute will not be on the new request.

To do this you have to adapt the JSP to see if the attribute exists and pass it on to the new request.

Or you have to work with a sesion variable.

I hope this is your scenario and that you are not trying to access doGet() & doPost() after the same request.

gotjee
  • 43
  • 2
  • 9
  • in my JSP I fill in form fields with ${requestScope.foo.id} values and I can see the correct data of my Foo object from doGet method. How could I pass it to the second request? – jimmyrap Jan 23 '18 at 11:02
  • On your JSP you have your form. Just add an additional input like this: and output the parameter instead of the "..." preferably in a secure & escaped way. – gotjee Jan 23 '18 at 11:07
  • I do have it, but how can I store the old inputs of my form, before the client will fill in the form with new values? – jimmyrap Jan 23 '18 at 11:13
  • Adding it as an input, makes sure it will be a parameter on the request originating from submitting that form. If this is not working maybe I don't understand something in your scenario, as why you are calling the same servlet twice, once with POST & once with GET.. – gotjee Jan 23 '18 at 11:16
  • for old inputs it's also data from a previous request so you can do the same? You can also do it in javascript like this: on your JSP give an id attribute to your input, like input1, then in javascript do the following : document.getElementById('input1').value="<%=request.getParameter("input1")%>" – gotjee Jan 23 '18 at 11:30
  • unfortunately, nothing works, including sessionAttributes. I do see my input values from a previous request, but I cannot pass it to my doPost method as an attribute of my request – jimmyrap Jan 23 '18 at 11:36
  • I see now, In the servlet you put it as attribute, then in JSP you have get it as attribute, you then insert it in the input, but inputs are not going to be send as attirubtes they are parameters so it will arrived in your doPost as Parameter. So please change your doPost to retrieve it as a parameter and try again. So your JSP changes it from attribute to parameter basically. – gotjee Jan 23 '18 at 12:20
-1

I think you should add the method attribute to your <form> tag:

<form ..... method="POST">

Then submitting the form will call the doPost() method of your servlet.

gsl
  • 676
  • 5
  • 16