0

I'm currently working on a web app in which I want to update the user as to whether or not an operation was successful. I attempt to achieve this by setting request attributes and forwarding from one servlet to the next. However, the attribute is always null in the receiving controller.

code block that sets the attribute:

try {
                updateXRef(request, response, cmds);
            } catch (Exception e) {
                request.setAttribute("results", "Error encountered. Contact system administrator.");
                push(request, response);
            }
            request.setAttribute("results", "Update Successful");
            push(request, response);
        }

        else {
            push(request, response);
        }

the method that sends to the other servlet:

private void push(HttpServletRequest request, HttpServletResponse response) {
        String url = "/PushServer";

        try {
            request.getServletContext().getRequestDispatcher(url).forward(request, response);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

and the servlet that processes the request:

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


        if(FileFactory.getFileOperationsObject() == null || request.getParameterValues("input") == null) {

            initiliaze(request, response);

            String url = "/Display.jsp"; 
            request.setAttribute("xRefFile", FileFactory.getXRefFileObjects());
            request.setAttribute("platforms",FileFactory.getUniqueSortedPlatforms());
            request.setAttribute("showModal", 0);

            if(request.getParameter("results") == null) {
                request.setAttribute("results", "Update Pending");
            }

            request.getServletContext().getRequestDispatcher(url).forward(request, response);

        }

My only guess is that a new request is somehow being generated. If that is indeed what is happening - how do I avoid it?

Matthew
  • 817
  • 1
  • 13
  • 39
  • 1
    `getParameter("results")` != `getAttribute("results")` – Andreas Sep 01 '19 at 19:17
  • @PraveenKumarLalasangi This is not a duplicate of the linked thread. The linked thread focuses on why values were becoming null. This question is demonstrable by the differences between getAttribute and getParameter, and is far more concise in the answer. I request that you remove your "duplicate" tag. – Matthew Sep 01 '19 at 22:02
  • @PraveenKumarLalasangi If you are going to be marking legitimate questions as duplicates, then you might want to actually do it correctly. The question : https://stackoverflow.com/questions/5243754/difference-between-getattribute-and-getparameter answers my question, the question you linked does not. A comment to a question hardly serves as an answer by the way. – Matthew Sep 01 '19 at 22:34
  • 1
    As i believe duplicate questions are not necessarily bad; different descriptions of the same problem help future visitors to find the answers they're looking for. So retracted flag. – PraveenKumar Lalasangi Sep 01 '19 at 22:55

1 Answers1

1

The problem is method selection.

request.getParameter("yourAttributeName") only works for retrieving form data (<form></form>) - aka data from your .jsp page - as well as query parameters.

If one wishes to send information from one Java servlet to another Java servlet, as in the above code, one must use:

request.getAttribute("myAttributeName");

Matthew
  • 817
  • 1
  • 13
  • 39