1

How can I retain HTML form field values in JSP after submitting form to Servlet?

I am trying to retain a csv file in the jsp but it looks not possible. I also tried to pass the csv file back to the jsp and send it as a part of form back to the controller but not able to do it. so please suggest a way to retain the csv file or redirect it back to the jsp and be able to use the second time the page reloads

I have done some research and came to a conclusion that it is not possible to retain an input field of type csv file. Please correct me if I am wrong

Answer: As per my knowledge it is not possible as suggested in this https://stackoverflow.com/questions/2598904/how-do-i-keep-form-data-after-submitting-the-form-using-javascript#:~:text=To%20keep%20the%20values%2C%20you,request%20parameters%20into%20the%20fields.&text=You%20can%20use%20cookies%20from,you%20access%20something%20called%20document. answer once the form is submitted the entire page is replaced by response from the server so to retain the csv file values I think we can only use an jQuery.post or jQuery.ajax to send the form data rather than actually submitting the form

Rahul
  • 11
  • 6
  • 1
    If it was text then you can but for files you can't do . Reason [check](https://stackoverflow.com/questions/4205634/restoring-the-value-of-a-input-type-file-after-failed-validation) here . – Swati Dec 17 '20 at 13:23
  • Thank you swathi – Rahul Dec 17 '20 at 16:45

1 Answers1

0

If you want to retain any thing that was passed from a jsp and use it in back end, you can do one thing . once passed to the controller, we can read the data of the csv file and set that object in the session using .setAttribute("object name", object). The session object can be fetched and set an object using the following lines of code

HttpSession session = request.getSession(); 
session.removeAttribute("<object name>");
session.setAttribute("<object name>", object);

and use this object the second time the control moves from jsp to controller by using the getAttribute()

<className> <objectName> = session.getAttribute("<object name>");

and use it as needed.

Rahul
  • 11
  • 6