Search code examples
atgatg-dynamooracle-commercedropletatg-droplet

ATG - session or request attribute coming as null when checkFormRedirect is called


In my ATG application, when I am redirecting user to jsp page with some parameters using checkFormRedirect, I am getting parameters as null. Please see below FormHandler code:

UserFormHandler:

public boolean handleUserRedirect(dynamo req, dynamo res){

//using request
req.setParameter("test", "testdata");

//using session
HttpSession session=req.getSession();  
session.setAttribute("uname","testdata"); 

//redirect to test.jsp
return checkFormRedirect("/test/test.jsp","null",req,res);
}

test.jsp :

<% out.println(session.getAttribute("uname")); %>

<% String stErrorMsg=(String)session.getAttribute("uname");%>

<%=stErrorMsg %>

<% request.getParameter("test")%>

Also, I have tried using variable in my formHandler and setting value and still I am getting value as null. Can some help on this.


Solution

  • Generally, you cannot send a POST request using sendRedirect() method. You can use RequestDispatcher to forward() requests with parameters within the same web application, same context.

    RequestDispatcher dispatcher = servletContext().getRequestDispatcher("test.jsp");
    dispatcher.forward(request, response);
    

    The HTTP spec states that all redirects must be in the form of a GET (or HEAD). You can consider encrypting your query string parameters if security is an issue. Another way is you can POST to the target by having a hidden form with method POST and submitting it with javascript when the page is loaded.

    So you can use Session approach: I tried out I get the value in JSP.

    <%
          out.println(session.getAttribute("message"));
          session.removeAttribute("message");
    %>
    /* Or using JSTL */
      <c:out value="${sessionScope.message}" />
      <c:remove var="message" scope="session" />
    

    screenshot enter image description here

    Hope this help.