1

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.

Manan Kapoor
  • 675
  • 1
  • 11
  • 28

1 Answers1

2

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.

Anshu Kumar
  • 633
  • 7
  • 12
  • Yeah, I did the same approach, just instead of saving the values in session, I have added in handler itself and accessed in jsp. Thanks for your help. Is there any other way where we can directly post the request from server instead of redirecting to frontend. – Manan Kapoor Feb 04 '19 at 10:42
  • @MananKapoor It's not clear what you want to achieve. If you are calling some webservice, if so then you can use httpclient etc. – Anshu Kumar Feb 04 '19 at 13:17