Hello,
Problem: When an html form in a JSP page is submitted to another JSP page to add
phone number for a personnel the second JSP page automatically executes twice so it
makes calls to Java Bean twice and I end up with two same phone numbers added in the
system.
Attached: personnelPhone.jsp has two forms one update and delete a phone and the
second one is for add new phone for the personnel. This second form (has the problem)
it is submitted through JavaScript and goes to personnelPhoneAddRoute.jsp which calls
the java Bean to add the information in database and displays an alert (javascript
alert) and redirects the page back to personnelPhone.jsp
By looking at the system outs the personnelPhoneAddRoute is somehow being executed
twice AND IT IS NOT ALL THE TIME. (IT IS RANDOM)
Below are 3 files copied to this email.......... Please take a look...
Thanks for your offer to help
Regards
Hassan
===========================================================================
//file name: personnelPhone.jsp
<%@page import="java.sql.*, java.text.*, java.io.*, java.util.*, cdis.beans.*,
lib.beans.*" %>
<%
String dbrespass = "";
String dbresid = "";
String dbresrole = "";
String isFalse = "false";
String errmesg = new String();
HttpSession vSession = request.getSession(false);
try
{
if(!(vSession.isNew()))
{
dbresid = (String)vSession.getValue("sesUserId");
dbrespass = (String)vSession.getValue("sesPasswd");
dbresrole = (String)vSession.getValue("sesRole");
if( dbresid.equals(isFalse))
{ response.sendRedirect("errorInt.jsp?Res=NS");}
}
else { response.sendRedirect("errorInt.jsp?Res=NS"); }
}
catch(Exception e)
{
System.out.println("searchPersonal.jsp Exception");
out.println(e);
}
System.out.println("in personnelPhone.jsp page");
cdisPersPhoneBean sqlBean = new cdisPersPhoneBean();
int empid = Integer.parseInt(request.getParameter("empNumber"));
String pURL = "./personnelDetails.jsp?empNumber=" + empid;
int intPhnID = 0;
String pFirst = (String)vSession.getValue("sesPersFirst");
String pMiddle = (String)vSession.getValue("sesPersMiddle");
String pLast = (String)vSession.getValue("sesPersLast");
String ID = "";
String sAddPhoneType = "";
String phnType = "";
String sPhoneType = "";
String areaCode = "";
String phoneNum = "";
String ext = "";
String pin = "";
Vector vrs = new Vector();
%>
<html>
<head>
<title> Update Personnel Phone </title>
<script language="JavaScript">
//**********************************
function updatePhoneLine(form) {
var selectedPhoneType = '';
for (a=0; a < form.frmPhoneType.options.length; a++) {
if(form.frmPhoneType.options[a].selected){
selectedPhoneType = "" + form.frmPhoneType.options[a].value;
}
}
form.hPhoneType.value = selectedPhoneType;
form.action = "./personnelPhoneUpdateRoute.jsp";
form.submit();
return true;
}
//********************************
function removePhoneLine(form) {
var selectedPhoneType = '';
for (a=0; a < form.frmPhoneType.options.length; a++) {
if(form.frmPhoneType.options[a].selected){
selectedPhoneType = "" + form.frmPhoneType.options[a].value;
}
}
form.hPhoneType.value = selectedPhoneType;
form.action = "./personnelPhoneRemoveRoute.jsp";
form.submit();
return true;
}
//********************************
function addPhoneLine(form) {
var selectedPhoneType = '';
for (a=0; a < form.frmPhoneType.options.length; a++) {
if(form.frmPhoneType.options[a].selected){
selectedPhoneType = "" + form.frmPhoneType.options[a].value;
}
}
form.haPhoneType.value = selectedPhoneType;
form.action = "./personnelPhoneAddRoute.jsp";
form.submit();
return true;
}
//********************************
</script>
</head>
<body bgcolor="#FFFFFF" text="#000000">
<% if(dbresrole.equals("admin")) { %>
<%@ include file="./menuBarAdmin.html" %>
<% } else { %>
<%@ include file="./menuBarUser.html" %>
<% } %>
<CENTER>
<IMG SRC="images/updatepersonnelphone.jpg"></CENTER><BR>
<center>
<b>Name: </b><%=pFirst%> <%=pMiddle%> <%=pLast%> <br> <br>
<table width="70%" align="center">
<tr align=left bgcolor="#D0E1F4">
<td><B> Phone Type</B></td>
<td><B> Phone Number</B></td>
<td><B> Ext</B></td>
<td><B> Pin</B></td>
<td><B> Update</B></td>
<td><B> Delete</B></td>
</tr>
<%
try
{
vrs = sqlBean.getPersonnelPhone(empid);
Enumeration phRs = vrs.elements();
while(phRs.hasMoreElements())
{
intPhnID = Integer.parseInt(phRs.nextElement().toString());
phnType = phRs.nextElement().toString();
areaCode = phRs.nextElement().toString();
phoneNum = phRs.nextElement().toString();
ext = phRs.nextElement().toString();
pin = phRs.nextElement().toString();
%>
<tr bgcolor="#eeeeef">
<form name="frmUpdatePhone" method="POST">
<td><DIV ALIGN="left"> <SELECT NAME="frmPhoneType">
<%
try
{
vrs = sqlBean.getValidPhoneType();
for(int i = 0; i < vrs.size(); i=i+1)
{
sPhoneType = vrs.get(i).toString();
if(sPhoneType.equals(phnType)) { %>
<option value="<%= sPhoneType %>" SELECTED>
<%=sPhoneType%> </option>
<% } else { %>
<option value="<%= sPhoneType %>">
<%=sPhoneType%> </option>
<% }
}// for loop
}// try
catch(OAException ex ){
errmesg = "Error in getValidPhoneType() " +
ex.getMessage();
vSession.putValue("errmsg", errmesg);
System.out.println("errmsg: "+ errmesg);
response.sendRedirect("errorInt.jsp");
}
catch (Exception e) {
errmesg = "Error in getValidPhoneType Result " +
e.getMessage();
vSession.putValue("errmsg", errmesg);
System.out.println("errmsg: "+ errmesg);
response.sendRedirect("errorInt.jsp");
} %>
</SELECT></TD>
<td><input size="4" maxlength="3" type="text"
name="txtAreaCode" value="<%= areaCode %>">
<input size="9" maxlength="8" type="text" name="txtPhoneNum"
value="<%= phoneNum %>"></td>
<td><input size="5" maxlength="4" type="text" name="txtExt"
value="<%= ext %>"></td>
<td><input size="10" maxlength="9" type="text" name="txtPin"
value="<%= pin %>"></td>
<td><input type="Submit" name="Submit" value="Update Phone"
onClick="return(updatePhoneLine(this.form)) "></td>
<td><input type="Submit" name="Delete" value="Delete Phone"
onClick="return(removePhoneLine(this.form)) "></td>
<input type="HIDDEN" name="hPhoneType" value="">
<input type="HIDDEN" name="empNumber" value="<%= empid %>">
<input type="HIDDEN" name="phnNumber" value="<%= intPhnID %>">
</form>
</tr>
<%
}
}
catch(OAException ex ){
errmesg = "Error in getPersonnelPHone() " + ex.getMessage();
vSession.putValue("errmsg", errmesg);
System.out.println("errmsg: "+ errmesg);
response.sendRedirect("errorInt.jsp");
}
catch (Exception e) {
errmesg = "Error in getPersonnelPhone Result " + e.getMessage();
vSession.putValue("errmsg", errmesg);
System.out.println("errmsg: "+ errmesg);
response.sendRedirect("errorInt.jsp");
}
%>
</table>
<hr width="50%">
<table width="60%" align="center">
<tr align=left bgcolor="#D0E1F4">
<td><B> Phone Type</B></td>
<td><B> Phone Number</B></td>
<td><B> Ext</B></td>
<td><B> Pin</B></td>
<td><B> Add</B></td>
</tr>
<tr bgcolor="#eeeeef">
<form name="frmAddPersonnelPhone" method="POST">
<td><DIV ALIGN="left"> <SELECT NAME="frmPhoneType">
<%
try
{
vrs = sqlBean.getValidPhoneType();
for(int i = 0; i < vrs.size(); i=i+1)
{
sAddPhoneType = vrs.get(i).toString();
%> <option value="<%= sAddPhoneType
%>"> <%=sAddPhoneType%> </option> <%
}
}
catch(OAException ex ){
errmesg = "Error in getValidPhoneType() " +
ex.getMessage();
vSession.putValue("errmsg", errmesg);
System.out.println("errmsg: "+ errmesg);
response.sendRedirect("errorInt.jsp");
}
catch (Exception e) {
errmesg = "Error in getValidPhoneType Result " +
e.getMessage();
vSession.putValue("errmsg", errmesg);
System.out.println("errmsg: "+ errmesg);
response.sendRedirect("errorInt.jsp");
} %>
</SELECT></TD>
<td><input size="4" maxlength="3" type="text"
name="txtaAreaCode" value="">
<input size="9" maxlength="8" type="text" name="txtaPhoneNum"
value=""></td>
<td><input size="5" maxlength="4" type="text" name="txtaExt"
value=""></td>
<td><input size="10" maxlength="11" type="text" name="txtaPin"
value=""></td>
<td><input type="Submit" name="Submit" value="Add Phone"
onClick="return(addPhoneLine(this.form)) "></td>
<input type="HIDDEN" name="haPhoneType" value="">
<input type="HIDDEN" name="empaNumber" value="<%= empid %>">
<input type="HIDDEN" name="phnaNumber" value="<%= intPhnID %>">
</form>
</tr>
</table>
<BR><BR>
<CENTER>
<table align="center" width="50%" border="0">
<form Name="frmBackToDetails" method="post" action= <%= pURL %> >
<tr align="center"><td>
<input type="Submit" name="Button" value="Back to Personnel
Details Page">
</td></tr>
</form>
</table>
<CENTER>
</body>
</html>
************************************************************************************************************************
//file name: personnelPhoneAddRoute.jsp
<%@page import="java.sql.*, java.text.*, java.io.*, java.util.*, cdis.beans.*,
lib.beans.*" %>
<%
String dbrespass = "";
String dbresid = "";
String dbresrole = "";
String isFalse = "false";
HttpSession vSession = request.getSession(false);
try {
if(!(vSession.isNew())) {
dbresid = (String)vSession.getValue("sesUserId");
dbrespass = (String)vSession.getValue("sesPasswd");
dbresrole = (String)vSession.getValue("sesRole");
if( dbresid.equals(isFalse))
{ response.sendRedirect("errorInt.jsp?Res=NS");}
}
else { response.sendRedirect("errorInt.jsp?Res=NS"); }
}
catch(Exception e)
{
System.out.println("searchPersonal.jsp Exception");
out.println(e);
}
System.out.println("in personnel add phone route page");
String errmesg = new String();
int intPhnID =
Integer.parseInt(request.getParameter("phnaNumber"));
int empid =
Integer.parseInt(request.getParameter("empaNumber"));
String sPhoneType = request.getParameter("haPhoneType");
String sAreaCode = request.getParameter("txtaAreaCode");
String sPhoneNum = request.getParameter("txtaPhoneNum");
String sExt = request.getParameter("txtaExt");
String sPin = request.getParameter("txtaPin");
cdisPersPhoneBean sqlBean = new cdisPersPhoneBean();
try
{
System.out.println("in JSP ------- before calling add function");
sqlBean.addPersonnelPhone(empid, sPhoneType, sAreaCode, sPhoneNum,
sExt, sPin);
System.out.println("in JSP ------- after calling add function");
}
catch(OAException e) {
errmesg += " Error in addPersonnelPhone.jsp " + e.getMessage();
vSession.putValue("errmsg", errmesg);
System.out.println("errmsg: "+ errmesg);
response.sendRedirect("errorInt.jsp");
}
catch(Exception e) {
errmesg += " \n Error in addPersonnelPhone.jsp " + e.getMessage();
vSession.putValue("errmsg", errmesg);
System.out.println("errmsg: "+ errmesg);
response.sendRedirect("errorInt.jsp");
}
%>
<html>
<head>
<script language="JavaScript">
function runThisMethod()
{
alert("Phone Number has been added successfuly");
self.location= "./personnelPhone.jsp?empNumber=" + <%=
empid %>;
}
</script>
</head>
<body onload="runThisMethod()"></body>
</html>
************************************************************************************************************************
//file name: PersPhoneBean.java
//**********************************************************************************
//BELOW IS THE METHOD I AM CALLING FROM MY "PERSONNELPHONEUPDATE.JSP" PAGE
//**********************************************************************************
public void addPersonnelPhone(int id, String type, String areaCode,
String number, String ext, String pin) throws OAException
{
//int phoneId = getCurrentPhoneID();
//phoneId++;
System.out.println("1. in cdisPersPhoneBean.java --- in
addPersonnelPhone \n"
+ "---- before calling add
function");
String addPersonnelPhoneQuery = "Insert into personnel_phone_number
values (" + id
+ " , '" + type
+ "', '" + areaCode
+ "', '" + number
+ "', '" + ext
+ "', '" + pin
+ "', personnel_phone_id.NextVal)" ;
try{
System.out.println("2. in cdisPersPhoneBean.java --- in
addPersonnelPhone \n"
+ "----in TRY --- before
calling add function");
DB.DBInsert(addPersonnelPhoneQuery);
System.out.println("3. in cdisPersPhoneBean.java --- in
addPersonnelPhone \n"
+ "---- after calling add
function");
System.out.println("in cdisPersPhoneBean.java --- in
addPersonnelPhone \n"
+ "---- after logging in the
information");
}
catch(Exception e){
System.out.println("in cdisPersPhoneBean.java --- in
addPersonnelPhone \n"
+ "----in CATCH --- before
calling add function");
e.printStackTrace();
throw new OAException(e.toString());
}
}
//************************************************************
===========================================================================
To unsubscribe: mailto [EMAIL PROTECTED] with body: "signoff JSP-INTEREST".
For digest: mailto [EMAIL PROTECTED] with body: "set JSP-INTEREST DIGEST".
Some relevant FAQs on JSP/Servlets can be found at:
http://java.sun.com/products/jsp/faq.html
http://www.esperanto.org.nz/jsp/jspfaq.html
http://www.jguru.com/jguru/faq/faqpage.jsp?name=JSP
http://www.jguru.com/jguru/faq/faqpage.jsp?name=Servlets