jspjstlservlet-3.0setattribute

JSP servlet JSTL


When I send attribute 'saved' from servlet to jsp, if it equals to true I show alert msg otherwise I want to assign it to false in the second refresh.

Servlet:

 saved = true;
 request.setAttribute("saved", saved);
 response.sendRedirect("temp/pr_home.jsp");

JSP:

<c:choose>

        <c:when test="${saved==true}">

                <c:out value="${saved}"> </c:out>
            <div class="alert-box success" role="alert" >
                    Project Review has been saved! <a href="pr_home.jsp" > click here to see details  </a>
            </div>
            <br />

        </c:when>    

        <c:otherwise>
            <div class="alert-box failure" role="alert">
                    This is a danger alert—check it out!
            </div>
        </c:otherwise>

    </c:choose>

View: enter image description here

The flag variable 'saved' still in true all the time !! So, also alert message appears each time I refresh the page :(

Sorry for my English.

thank you for your help.


Solution

  • The problem is how you redirect to .jsp .
    Because .sendRedirect(String path) method doesn't transfer the request and response objects.
    You typically use this technique when you want to transfer control to a URL outside of your application.
    So I suggest this method to call a jsp page:

    getServletContext.getRequestDispatcher("temp/pr_home.jsp").forward(request, response);
    

    And for the .setAttribute(String name, Object o) method is better use a wrapper class, for example:

    request.setAttribute("saved", new Boolean(saved));