How to create warning message on jsp page after submit process is complete

I am trying to add a message to my jsp after the process is completed by clicking the submit button.

function onSubmit() { alert("Master_Data.xlsx and Consistency_Check_Data.xlsx are located under d:/stage/MasterDataReports"); } </script> <body> <form name="input" action="getMasterData" method="get"> <br /> <br /> <h1 align='center'>Master Data File</h1> <br /> <br /> <table border="0" align='center'> <tr> <td> <h2>Site Name</h2> </td> <td align='left'> <jsp:useBean id="masterDao" class="master.dao.MasterDataDao"/> <select name="siteId" id="siteId"> <option value="0">ALL</option> <c:forEach items="${masterDao.allSites}" var="siteDto"> <option value="${siteDto.id}">${siteDto.name}</option> </c:forEach> </select></td> </tr> <tr> <td> <h2>Division</h2> </td> <td align='left'> <jsp:useBean id="masterDaoUtil" class="master.dao.util.MasterDataConstants"/> <select name="divisionId" id="divisionId"> <option value="33"><%=MasterDataConstants.DIVISION_TYPE_AUDIT_MANAGEMENT_GLOBAL_NAME%></option> <option value="31"><%=MasterDataConstants.DIVISION_TYPE_CHANGE_MANAGEMENT_GLOBAL_NAME%></option> <option value="34"><%=MasterDataConstants.DIVISION_TYPE_DEA_MANAGEMENT_GLOBAL_NAME%></option> <option value="35"><%=MasterDataConstants.DIVISION_TYPE_EHS_MANAGEMENT_GLOBAL_NAME%></option> <option value="23"><%=MasterDataConstants.DIVISION_TYPE_EVENT_MANAGEMENT_GLOBAL_NAME%></option> </select></td> </tr> </table> <br /> <br /> <div style="text-align: center"> **strong text**<input type="submit" value="Submit" OnClick="onSubmit()"> </div> 

At the moment, the sending process will occur only after clearing the warning. Is there a way in which I can raise a warning after the submit process is complete, or if I can add a message to the jsp page? thanks in advance sonny

Here is my updated servlet that is causing the error:

 package master.service; import java.io.IOException; import java.sql.SQLException; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; **strong text**import javax.servlet.http.HttpSession; @SuppressWarnings("serial") public class MasterDataServlet extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response, HttpSession session) throws IOException, ServletException { MasterDataService masterDataService = new MasterDataService(); try { int siteId = Integer.parseInt(request.getParameter("siteId")); int divisionId = Integer.parseInt(request.getParameter("divisionId")); //For master data file masterDataService.createMasterDataFile(siteId, divisionId,false); //For consistency checker file masterDataService.createMasterDataFile(siteId, divisionId,true); request.getRequestDispatcher("/masterDataQueryScreen.jsp").forward(request, response); **strong text**session.setAttribute("getAlert", "Yes");//Just initialize a random variable. **strong text**response.sendRedirect("/masterDataQueryScreen.jsp"); } catch (ClassNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } 
+5
source share
3 answers

So, let's say after receiving the servlet getMasterData will be response.sendRedirect to test.jsp.

In test.jsp

Create javascript

 <script type="text/javascript"> function alertName(){ alert("Form has been submitted"); } </script> 

and than below

 <script type="text/javascript"> window.onload = alertName; </script> 

Note: im not sure how to enter code in stackoverflow !. Edit: I just learned

Edit 2: To the question: This works fine. Another question. How can I get rid of the initial warning when I first started JSP? "Form Submitted" is the second one that I execute. It appears after the load is completed, and it will be perfect.

To do this, I would highly recommend using a session!

So what you want to do is your servlet:

 session.setAttribute("getAlert", "Yes");//Just initialize a random variable. response.sendRedirect(test.jsp); 

than in test.jsp

 <% session.setMaxInactiveInterval(2); %> <script type="text/javascript"> var Msg ='<%=session.getAttribute("getAlert")%>'; if (Msg != "null") { function alertName(){ alert("Form has been submitted"); } } </script> 

and than below

 <script type="text/javascript"> window.onload = alertName; </script> 

Therefore, every time you submit this form, the session will take place! If the session is not equal to zero, the function will be launched!

+4
source

in your servlet

  request.setAttribute("submitDone","done"); return mapping.findForward("success"); 

In your jsp

 <c:if test="${not empty submitDone}"> <script>alert("Form submitted"); </script></c:if> 
+3
source

You can also create a new saysp jng file in which the form will be submitted, and in your main action file just write the name of your file

Eg. Your form is submitted to the succes.jsp file. Then your action file will have

 Request.sendRedirect("success.jsp") 
0
source

Source: https://habr.com/ru/post/1200221/


All Articles