Hi everyone
I've got a problem with a java scipt that's not doing what I want it to do.
I have a jsp which gets data from a database to display.
On the jsp there is a "add" button which on click will open another jsp to add a contact, when I add the contact the jsp calls a web servlet which adds the contact to the database.
After the contact is added the servlet directs the page back to the contacts page, and it must display the newly added contact in the jsp.
I've configured a script that checks if there is a attribute called "addContact", if it has a value of "added" it must reload the page on load, which in turn will display the newly added contact,
it refreshes but it doesn't display the newly added contact, it worked but after awhile it stopped working.
Beneath is my code for the contact list jsp and the servlet.
ContactList.jsp
<?xml version="1.0"?>
<%@ page import="contacts.Contacts"%>
<%@ page import="java.util.*"%>
<jsp:useBean id = "contactsData" scope = "page" class ="contacts.ContactsData"/>
<html>
<head>
<title>Contact List</title>
<script language=JavaScript>
<!--
function clicked(clickedValue)
{
if(clickedValue == "add")
{
window.location = "AddContact.jsp";
}
}
-->
</script>
</head>
<%
String refreshSession = (String)session.getAttribute("addContact");
if(refreshSession != null && refreshSession.equals("added"))
{
%>
<body onload="window.location.reload(true);">
<%
session.removeAttribute("addContact");
}
else
{
%>
<body>
<%
}
%>
<h1>Contacts</h1>
<form>
<table cellpadding="5">
<%
ArrayList contactList = contactsData.getContacts();
Iterator contactsIterator = contactList.iterator();
Contacts contacts;
while(contactsIterator.hasNext())
{
contacts = (Contacts) contactsIterator.next();
String contactName = contacts.getContactName();
%>
<tr>
<td><input type="radio" name="deleteContact" value=<%=contactName%>></td>
<td><a href = "UpdateContact.jsp?paramName=<%=contactName%>"><%=contactName%></a></td>
<td><%=contacts.getCompany()%></td>
<td><%=contacts.getWorkNumber()%></td>
<td><a href = "mailto:<%=contacts.getEmail() %>"><%=contacts.getEmail() %></a>
</tr>
<%
}
%>
</table>
<br>
<input type="button" value="Add" onClick="clicked('add')"/>
</form>
</body>
</html>
NewContactServlet.java
package contacts;
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import contacts.*;
public class NewContactServlet extends HttpServlet
{
public void doPost(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException
{
res.setHeader("Expires", "Tues, 01 Jan 1980 00:00:00 GMT"); //no cache allowed
HttpSession session = req.getSession(true); //get current session
String contactssession = (String)session.getAttribute("contactsSession");
if(contactssession.equals("add"))
{
Contacts contacts = new Contacts();
ContactsData contactsData = new ContactsData();
contacts.setContactName(req.getParameter("contactName"));
contacts.setCompany(req.getParameter("companyName"));
contacts.setWorkNumber(req.getParameter("workNum"));
contacts.setCellNumber(req.getParameter("cellNum"));
contacts.setFaxNumber(req.getParameter("faxNum"));
contacts.setEmail(req.getParameter("email"));
contacts.setCountry(req.getParameter("country"));
contactsData.addContact(contacts);
}
else if(contactssession.equals("add"))
{
}
session.setAttribute("addContact","added");
res.sendRedirect("ContactList.jsp");
}
}