0

I want to send a message (example : String msg = "Hello";) from Servlet to JSP (myPage.jsp).

The flow: Step 1 : On myPage.jsp, there are multiple textboxes and various other data fields that the user fills and hits submit. Step 2: The control goes to Servlet, Servlet validation happens and a message needs to be returned back to the same JSP as an alert BOX.

NOTE : No data on myPage.jsp that was entered by the user should be deleted. Only an alert should pop up and user should be able to click OK on the alert box and continue working on myPage.jsp.

There are multiple links that tell about how to pass message from servlet to JSP.

How can I alert message in Servlet code and sendredirect to JSP page?

Message alert box in servlet

JSP and Servlet serverside validation

display a message coming from servlet on jsp page in javascript alert box

But none of these solve the condition: "No data on myPage.jsp that was entered by the user should be deleted. Only an alert should pop up and user should be able to click OK on the alert box and continue working on myPage.jsp."

Gouri Yashodhan
  • 81
  • 1
  • 1
  • 8
  • on JSP, i have : this can be added multiple times using add button that triggers a javascript function which does cloning. on Servlet, I have in doPost method : String[] pc = request.getParameterValues("pc"); This works. I get all the data in pc array, but when i display an error message on the same page, the form data is lost. – Gouri Yashodhan Jan 29 '18 at 20:02
  • So: Now, I changed this to: JSP: Although I dont get any error, still my issue is not resolved. I still don't see 'pc' populated with its before-submit value. – Gouri Yashodhan Jan 29 '18 at 20:02
  • @BalusC , could you tell me what wrong am I doing ? – Gouri Yashodhan Jan 30 '18 at 14:54
  • Maybe you've wiped out the whole request by performing a redirect from servlet to JSP for some reason. Put your project aside for now. Create a sandbox project. Follow **exactly** the example in 2nd duplicate link https://stackoverflow.com/questions/6464931/how-perform-validation-and-display-error-message-in-same-form-in-jsp Then base off your final solution for real project on that. – BalusC Jan 30 '18 at 15:30
  • thank you. The problem was I was using sendRedirect instead of forwarding the request. – Gouri Yashodhan Jan 30 '18 at 16:12
  • however, it still does not work as expected. My code is : JSP : <%int pcRowCount = 0; while(pcRowCount<4) {%> <% pcRowCount++; } %> Servlet: String[] pc = request.getParameterValues("pc"); So there are 4 textboxes with the same name. Although before-submit I enter a value in only 1 text box, the same gets replicated on all 4 textboxes after-submit – Gouri Yashodhan Jan 30 '18 at 16:17

0 Answers0