Search code examples
javajspservletswar

Request Attributes are lost during forwarding from one Servlet to another Servlet


In my web application I have 2 servlets where one pass data to another. Please check the below code.

RequestDispatcher requestDispatcher = request.getRequestDispatcher("LoadCategoryList");
            request.setAttribute("save_result", result);
            requestDispatcher.forward(request, response);

In this code I am passing data save_result to LoadCategoryList. Below is how I try to collect this data in LoadCategoryList

String result = request.getParameter("save_result");

Anyway, the passed data seems to be NULL. But I can GUARANTEE that data I am passing are not NULL. What's wrong here?


Solution

  • Request parameters and request attributes are two different things.

    If you set a value using ServletRequest.setAttribute(), you have to retrieve it using ServletRequest.getAttribute().