I have mapped a servlet and a jsp form. Once the form the is filled out I should get a confirmation page with the summary of what was filled in and an email (remote using gmail) should be sent but so far nothing happens, not even error messages. It loads back to the jsp file where the registration form is. Any ideas why this is happening?
There is no DB interaction in the process at all.
This is the Servlet:
package eBooks.controller;
import eBooks.business.User;
import eBooks.util.MailUtil;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.mail.MessagingException;
import javax.servlet.RequestDispatcher;
import javax.servlet.http.HttpSession;
public class ContactServlet extends HttpServlet{
protected void doPost(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException
{
// get parameters from the request
String fName = request.getParameter("fName_contact");
String lName = request.getParameter("lName");
String emailAddress = request.getParameter("emailAddress");
String subject = request.getParameter("subject_contact");
String messageArea = request.getParameter("messageArea");
// Create a user object and store the object
User user = new User(fName, lName, emailAddress, subject, messageArea);
HttpSession session = request.getSession();
session.setAttribute("user", user);
// send email to user
String to = emailAddress;
String from = "somemail@gmail.com";
String emailSubject = subject;
String body = "Dear " + fName + ", \n\n"+
"Thanks for contacting us. This is a confirmation to inform you that"
+ "we received your message.\n\n"
+ "Our team is reviewing your message and we will get back to you as soon"
+ "as possible. \n\n"
+ "Regards,\n\n"
+ "Matt \n"
+ "CEO";
boolean isBodyHtml = false;
try
{
MailUtil.sendMail(to, from, emailSubject, body, isBodyHtml);
}
catch(MessagingException e)
{
String errorMessage =
"Error Unable to send email. " +
"Check your Server (Glassfish) logs for details.<br/>" +
"Note: You may need to configure your system again.<br/>"+
"Error Message: " + e.getMessage();
request.setAttribute("errorMessage", errorMessage);
this.log(
"Unable to send email. \n" +
"Here is the email you tried to send \n"+
"====================================\n"+
"To:" + emailAddress + "\n"+
"FROM: " + from + "\n" +
"SUBJECT: " + subject + "\n"+
"\n" +
body + "\n\n");
}
// forward request and response to JSP page
String url = "/WEB-INF/view/confirmation.jsp";
RequestDispatcher dispatcher = getServletContext().getRequestDispatcher(url);
dispatcher.forward(request, response);
}
}
This the contact form:
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" type="text/css" href="css/stylesheet.css">
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<title>Contact</title>
</head>
<body>
<div id="page-container">
<c:import url="/WEB-INF/includes/main-nav.jspf"/>
<div id="header-content">
<h1> [ Contact ] </h1>
<p> </p>
<p> </p>
<h2>Share your thoughts with us:</h2>
<form action="contactEmail" method="post">
<p class="name">
<input type="text" name="fName" id="fName"
value ="${User.fName}"/>
<label for="fName">First Name</label>
</p>
<p class="name">
<input type="text" name="lName" id="lName"
value ="${user.lName}"/>
<label for="lName">Last Name</label>
</p>
<p class="email">
<input type="text" name="emailAddress" id="emailAddress"
value="${user.emailAddress}"/>
<label for="emailAddress">E-mail</label>
</p>
<p class="web">
<input type="text" name="subject" id="subject"
value="${user.subject}"/>
<label for="subject">Subject</label>
</p>
<p class="text-message">
<label for="messageArea">Your message: </label><br>
<br>
<textarea name="messageArea" id="messageArea"
>${user.message}</textarea>
</p>
<p class="submit">
<input type="button"
onClick="validate(this.form)" value="Submit">
</p>
</form>
<p> </p>
<script language="JavaScript">
function validate(form){
if(form.fName.value==""){
alert("Please fill your first name");
form.fName.focus();
}
else if(form.lName.value==""){
alert("Please fill your last name");
form.lName.focus();
}
else if(form.subject.value==""){
alert("Please fill the subjetct");
form.subject.focus();
}
else if (form.messageArea.value==""){
alert("The message cannot be empty");
form.text_message_area.focus();
}
else{
form.submit();
}
}
</script>
</div>
<div id="content-assist">
<div class ="padding">
<h2>We are located at:</h2>
<p>153 West 139th Street New York, NY<br>
</p>
<p>We are currently undergoing a 'face lift', so if you have any questions or would like more information about the services we provide please feel free to contact us.</p>
<h2>Give us a call:</h2>
<p>Phone: 212- 470-5024<br />
Fax: (212) xxx xxxx<br />
Email: <a href="mailto:info@noemailLuistest.com">info@LuisDomainTest.com</a><br />
ZIP: 10030, New York, USA</p>
<p><a href="contact.jsp">More contact information…</a></p>
</div>
</div>
<c:import url="/WEB-INF/includes/footer.jspf"/>
This is the confirmation jsp:
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<link rel="stylesheet" type="text/css" href="css/stylesheet.css">
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<title>Dashboard</title>
</head>
<body>
<div id="page-container">
<c:import url="/WEB-INF/includes/main-nav.jspf"/>
<div id="confirmation-top">
<h2>[ Confirmation ]</h2>
</div>
<div id="confirmation">
<h2> Awesome! We got your message </h2>
<p>Thanks for contacting us. Your message has been submitted and it
will be reviewed by our specialist in the order in which it was received.</br>
<br>
A confirmation email has been sent to ${sessionScope.user.emailAddress} </p>
<form action="index.jsp">
<input type="submit" value="/index.jps" name="home_btn" />
</form>
<p>
<i>${requestScope.errorMessage}</i>
</p>
<br>
</div>
<c:import url="/WEB-INF/includes/footer.jspf"/>
This is the emailUtil:
package eBooks.util;
import java.util.Properties;
import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.Message;
import javax.mail.Address;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
public class MailUtil {
//private static String mailhost = "smtp.gmail.com";
public static void sendMail(String to, String from, String body,
String subject, boolean bodyIsHtml)
throws MessagingException
{
// 1 - get session
Properties props = new Properties();
props.put("mail.transport.protocol", "smtps");
props.put("mail.smtps.host", "smtp.gmail.com");
props.put("mail.smtps.port", 465);
props.put("mail.smtps.auth", "true");
props.put("mail.smtps.quitwait", "false");
Session session = Session.getDefaultInstance(props);
session.setDebug(true);
// 2 - create a message
Message message = new MimeMessage(session);
message.setSubject(subject);
if(bodyIsHtml)
message.setContent(body, "text/html");
else
message.setText(body);
// 3 - address the message
Address fromAddress = new InternetAddress(from);
Address toAddress = new InternetAddress(to);
message.setFrom(fromAddress);
message.setRecipient(Message.RecipientType.TO, toAddress);
// 4 - send the message
Transport transport = session.getTransport();
transport.connect("somemail2@gmail.com", "password123");
transport.sendMessage(message, message.getAllRecipients());
transport.close();
}
}
This is the servlet xml:
<!-- the definition fo r the servlets -->
<servlet>
<servlet-name>ContactServlet</servlet-name>
<servlet-class>eBooks.controller.ContactServlet</servlet-class>
</servlet>
<!-- the mapping for the servlets -->
<servlet-mapping>
<servlet-name>ContactServlet</servlet-name>
<url-pattern>/WEB-INF/view/contactEmail</url-pattern>
</servlet-mapping>
Have you checked whether servlet is being called or not ?
The problem lies here
<form action="contactEmail" method="post">and your mapping says
<url-pattern>/WEB-INF/view/contactEmail</url-pattern>Try changing
<url-pattern>/contactEmail</url-pattern>