@Override protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String contactSubject = request.getParameter("contactSubject"); String contactMessage = request.getParameter("contactMessage"); String contactName = request.getParameter("contactName"); String contactEmail = request.getParameter("contactEmail"); List error = new ArrayList(); if (!FormHelper.isNotNull(contactSubject)) { error.add("Your subject cannot be blank"); } if (!FormHelper.isNotNull(contactMessage)) { error.add("Your message cannot be blank"); } if (!FormHelper.isNotNull(contactName)) { error.add("Your name cannot be blank"); } if (!FormHelper.isNotNull(contactEmail)) { error.add("Your email cannot be blank"); } if (!FormHelper.checkEmail(contactEmail)) { error.add("Invalid email address"); } if (error.isEmpty()) { try { Contact c = new Contact(); c.setContactSubject(contactSubject); c.setContactMessage(contactMessage); c.setContactName(contactName); c.setContactEmail(contactEmail); c.setContactDate(new java.sql.Date(System.currentTimeMillis())); c.setContactStatus((short) 0); contactFacade.create(c); request.setAttribute( "redirectMessage", "Your contact have been sent successful!<br>Back to homepage..."); request.setAttribute("redirectLink", "Home"); request.getRequestDispatcher("redirect.jsp").forward(request, response); } catch (ServletException | IOException e) { error.add("Error: " + e.getMessage()); } } else { request.setAttribute("errors", error); request.getRequestDispatcher("contact.jsp").forward(request, response); } processRequest(request, response); }
@Before public void setUp() { contact = new Contact("java", "ran", "you", 33); contact.setContactId(33); dao = new ContactDao(); }