0

[Passing an object from JSP page back to Servlet

From the top reviewed answer of the question above, What does Form preprocessing Servlet and Form postprocessing Servlet mean? Where do I put it in my servlet? Do I put it on the same method? Do I call preprocessing Servlet first then proceed with the form submission of my JSP file and then proceed to the postprocessing file?

ss1
  • 43
  • 7
myonii
  • 1
  • 1

1 Answers1

1

You write both the code in the same servlet in the sequence mentioned below:

A. Get the request parameters from the request (which may have values of HTML form elements and explicit request parameters), as mentioned in the Form postprocessing part

String myObjectId = request.getParameter("myObjectId");
Object myObject = request.getSession().getAttribute(myObjectId);
request.getSession().removeAttribute(myObjectId);
// ...

B. Perform some business logic and forward the request to some JSP, as mentioned in the Form preprocessing part

String myObjectId = UUID.randomUUID().toString();
request.getSession().setAttribute(myObjectId, myObject);
request.setAttribute("myObjectId", myObjectId);
request.getRequestDispatcher("/WEB-INF/page.jsp").forward(request, response);
Arvind Kumar Avinash
  • 71,965
  • 6
  • 74
  • 110
  • thank you, but will it be in the same method though? If not, how and where will you call both methods? – myonii Dec 03 '19 at 21:21
  • @myonii - I missed your comment somehow. While looking for something else, I just came across this page. I am not sure if this solution worked for you or you implemented a different solution. If you implemented a different solution, it will be helpful if you share the same. In either case, you can help the community by accepting the answer that worked for you. An accepted answer helps future visitors use the solution confidently. Check https://meta.stackexchange.com/questions/5234/how-does-accepting-an-answer-work to learn how to do it. – Arvind Kumar Avinash May 29 '20 at 16:14