I'm a newbie to JSPs and am trying to write a feedback form that validates
some of the fields, but not others. The problem is the bean never seems to
update its fields. Can you please help?
// Feedback.java
import javax.servlet.*;
import javax.servlet.http.*;
import java.util.*;
import javax.mail.*;
import javax.mail.internet.*;
public class Feedback {
private static final String host = "mySmtpHost"; //this is a
valid host
private static final String from = "[EMAIL PROTECTED]";
private static final String to = "[EMAIL PROTECTED]";
private static final String subject = "Website Feedback";
private String title;
private String firstName;
private String surname;
private String company;
private String address;
private String telephone;
private String fax;
private String email;
private String comments;
private String how;
public void setTitle(String s) {
title = s;
}
public String getTitle() {
return title;
}
public void setFirstName(String s) {
firstName= s;
}
public String getFirstName() {
return firstName;
}
public void setSurname(String s) {
surname = s;
}
public String getSurname() {
return surname;
}
public void setCompany(String s) {
company = s;
}
public String getCompany() {
return company;
}
public void setAddress(String s) {
address = s;
}
public String getAddress() {
return address;
}
public void setTelephone(String s) {
telephone = s;
}
public String getTelephone() {
return telephone;
}
public void setFax(String s) {
fax = s;
}
public String getFax() {
return fax;
}
public void setEmail(String s) {
email = s;
}
public String getEmail() {
return email;
}
public void setComments(String s) {
comments = s;
}
public String getComments() {
return comments;
}
public void setHow(String s) {
how = s;
}
public String getHow() {
return how;
}
public void sendMessage()
throws Exception {
//Get system properties
Properties props = System.getProperties();
//Setup mail server
props.put("mail.smtp.host", host);
//Build up message body
String body = "This message has been automatically generated
- do not reply to this address.\n\n";
body = body + "From: " + title + ". " + firstName + " " +
surname + "\n";
body = body + "Company: " + company + "\n";
body = body + "Address: " + address + "\n";
body = body + "Telephone: " + telephone + "\n";
body = body + "Fax: " + fax + "\n";
body = body + "Email: " + email + "\n";
body = body + "Comments: " + comments + "\n";
body = body + "How: " + how + "\n";
//Get session
Session session = Session.getInstance(props,null);
//Define message
MimeMessage message = new MimeMessage(session);
message.setFrom(new InternetAddress(from));
message.addRecipient(Message.RecipientType.TO, new
InternetAddress(to));
message.setSubject(subject);
message.setText(body);
//Send message
Transport.send(message);
}
}
Feedback.html:
<table border="0" cellpadding="0" cellspacing="0" width="600">
<tr>
<td width="100%"><table border="0" cellpadding="5" cellspacing="0">
<tr>
<td valign="top"><font color="#800000" face="Arial"
size="7"><strong>Feedback</strong></font><br>
<font color="#012F84" face="Arial"><big><big>Send us your
comments</big></big></font></td>
<td align="right"><img src="../images/group.jpg" alt="group.jpg
(4314 bytes)" WIDTH="213"
HEIGHT="128"></td>
</tr>
</table>
<p><small><font face="Verdana">To send your comments on this site
directly to our
webmaster, please fill in your details in the form
below:</font></small></p>
<form method="POST" action="feedback1.jsp">
<table border="0" cellpadding="0" cellspacing="0">
<tr>
<td>Title:</td>
<td><select name="Title" size="1">
<option selected>Mr</option>
<option>Mrs</option>
<option>Miss</option>
<option>Ms</option>
</select></td>
</tr>
<tr>
<td>First Name:</td>
<td><input type="text" size="20" name="FirstName"></td>
</tr>
<tr>
<td>Surname:</td>
<td><input type="text" size="20" name="Surname"></td>
</tr>
<tr>
<td>Company:</td>
<td><input type="text" size="36" name="Company"></td>
</tr>
<tr>
<td>Address:</td>
<td><textarea name="Address" rows="5" cols="34"></textarea></td>
</tr>
<tr>
<td>Telephone:</td>
<td><input type="text" size="20" name="Telephone"></td>
</tr>
<tr>
<td>Fax:</td>
<td><input type="text" size="20" name="Fax"></td>
</tr>
<tr>
<td>Email:</td>
<td><input type="text" size="36" name="Email"></td>
</tr>
<tr>
<td>Comments:</td>
<td>
<textarea name="Comments" rows="7" cols="34"></textarea>
</td>
</tr>
<tr>
<td>How did you get to this site?</td>
<td><select name="How" size="1">
<option selected>Followed a link from another page</option>
<option>Search Engine</option>
<option>Specified the URL explicitly</option>
<option>Other</option>
</select></td>
</tr>
</table>
<p><input type="submit" name="Submit" value="Submit"><input
type="reset" name="Reset"
value="Reset"></p>
</form>
</td>
</tr>
</table>
feedback.jsp:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<title>Mail Test</title>
</head>
<body>
<jsp:useBean id="mailBean" scope="request" class="Feedback" />
<jsp:include page="Feedback.html" flush="true"/>
<jsp:setProperty name="mailBean" property="*" />
</body>
</html>
feedback1.jsp:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<title>Feedback response</title>
</head>
<body>
<jsp:useBean id="mailBean" scope="request" class="Feedback" />
<%
String errMsg = "";
if (mailBean.getFirstName()== null ||
mailBean.getFirstName().compareTo("") == 0) {
errMsg = errMsg + "You haven't entered your first name<BR>";
}
if (mailBean.getSurname() == null ||
mailBean.getSurname().compareTo("") == 0) {
errMsg = errMsg + "You haven't entered you're surname<BR>";
}
if (mailBean.getEmail() == null || mailBean.getEmail().compareTo("")
== 0) {
errMsg = errMsg + "You haven't entered you're e-mail
address<BR>";
}
if (mailBean.getComments() == null ||
mailBean.getComments().compareTo("") == 0) {
errMsg = errMsg + "You haven't entered any comments<BR>";
}
if (errMsg.compareTo("") == 0) {
try {
mailBean.sendMessage();
} catch (Exception e){
%>
<h2>Something went wrong</h2>
<%
}
%>
<h2>Thankyou. Your feedback has been sent.</h2>
<%
}
else {
%>
<h2>Your form could not be submitted because:</h2>
<h3><%= errMsg%></h3>
<jsp:include page="Feedback.html" flush="true"/>
<%
}
%>
</body>
</html>
> ==========================================================================
> =
> To unsubscribe: mailto [EMAIL PROTECTED] with body: "signoff
> JSP-INTEREST".
> 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
===========================================================================
To unsubscribe: mailto [EMAIL PROTECTED] with body: "signoff JSP-INTEREST".
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