// Collection of code snippets by Arne Vajhøj 
// (from articles on eksperten.dk / vajhoej.dk written sometime between 2004 and now) 
package test;

import java.io.IOException;
import java.sql.SQLException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.DynaActionForm;

public class InsertAction extends Action {
    public ActionForward execute(ActionMapping mapping,
                                 ActionForm form,
                                 HttpServletRequest request,
                                 HttpServletResponse response) throws IOException, ServletException {
        try {
            DynaActionForm daf = (DynaActionForm)form;
            String name = (String)daf.get("name");
            String phone = (String)daf.get("phone");
            String email = (String)daf.get("email");
            Contacts newc = new Contacts();
            newc.setName(name);
            newc.setPhone(phone);
            newc.setEmail(email);
            ContactsDatabaseAccess.insert(newc);
            request.setAttribute("allc", ContactsDatabaseAccess.getAll());
        } catch (ClassNotFoundException e) {
            throw new ServletException(e.getMessage());
        } catch (SQLException e) {
            throw new ServletException(e.getMessage());
        }
        return mapping.findForward("ok");
    }
}
