Sonny Sonny - 2 months ago 9
Javascript Question

How to create an alert message in jsp page after submit process is complete

I am trying to add a message in my jsp after the process is done by hitting 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>


Right now the submit process will only happen after I clear the alert. Is there a way that I can either pop an alert after the submit process is done or if I can add a message to the jsp page?
Thanks in advance
Sonny

Here is my updated Servlet that is causing 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();
}
}

}

Answer

So let's say after getMasterData servlet will response.sendRedirect to to test.jsp.

In test.jsp

Create a javascript

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

and than at the bottom

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

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

Edit 2: TO the question:This works perfectly. Another question. How would I get rid of the initial alert when I first start up the JSP? "Form has been submitted" is present the second I execute. It shows up after the load is done to which is perfect.

To do that i would highly recommendation to use session!

So what you want to do is in your servlet:

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

than in the 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 at the bottom

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

So everytime you submit that form a session will be pass on! If session is not null the function will run!