Help: A Strange Thing in my Servlet!
I wrote a simple servlet to get user feed back, and call an object to send feedback to a given address via Java Mail. For some strange reason, the data validation part never works - it is a simple IF/ELSE!
Below is a simplised version of the code. Anyone could give some ideas? Thanks a lot!
################################
// a java servlet to handle user feed back at a site
// it gets form data
// it checks if required data are all there
// if they are, it calls other object to send these data via Java Mail, and display thank-you msg
// if not, it displays the form with data filled, if any, and asks user to resubmit
package feedback;
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import feedback.FeedBack;
public class UserFeedBack extends HttpServlet {
// give mail server host name, mail receiver address, class var
static final String host="mail.myserver.com";
static final String receiver="me@myserver.com";
// method to get form data, validate data, and decide to send off or ask for resubmitting
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
ServletOutputStream out = response.getOutputStream();
response.setContentType("text/html");
out.println("<HTML>");
out.println("<HEAD><TITLE>User FeedBack</TITLE></HEAD>");
out.println("<BODY>");
// get data from HTML form
String name=request.getParameter("name");
String email=request.getParameter("email");
String content=request.getParameter("content");
String subject=("User Feedback from " + name);
System.out.println("Go to validation!");
// data validation
if(name==""||name==null||email==""||email==null||content==""||content==null) {
System.out.println("Go reSubmit!");
reSubmit(out, name, email, content);
} else {
// all data are there, try to send mail via FeedBack
System.out.println("Go to Java Mail!");
try {
// call FeedBack object, which handle mail via Java Mail
FeedBack feedBack=new FeedBack(host, receiver, name, email, subject, content);
// thank sender
showConfirmation(request, out, name);
} catch (Exception e_) {
// when sending mail fails
System.out.println("Got an exception when calling FeedBack: " + e_.toString());
}
}
out.println("</BODY>");
out.println("</HTML>");
out.close();
}
// method to ask user to re-submit data
private void reSubmit (ServletOutputStream out, String name, String email, String content) throws ServletException, IOException {
// show submitted data in filed
// recursively call this page for resubmitting
String fb_name=name;
String fb_email=email;
String fb_content=content;
try {
out.println("<table><tr><form name=\"Feed_Back\" method=\"POST\" action=\"/servlet/feedback.UserFeedBack\">");
out.println("<td colspan=2>Please submit all data required as indicated by *</td></tr>");
out.println("<tr><td>Name*</td><td><input type=text name=\"name\" value=");
out.println(fb_name + "></td></tr>");
out.println("<tr><td>Email*</td><td><input type=text name=\"email\" value=");
out.println(fb_email + "></td></tr>");
out.println("<tr><td>Content*</td><td><input type=text name=\"content\" value=");
out.println(fb_content + "></td></tr>");
out.println("<tr><td><input type=\"submit\" value=\"Submit\"></td>");
out.println("<td><input type=\"reset\" value=\"Reset\"></td></tr>");
out.println("</form></table>");
} catch (Exception e_) {
System.out.println("Got an exception when making re-submit form: " + e_.toString() );
}
}
// thanks msg to this trouble maker
private void showConfirmation (HttpServletRequest request, ServletOutputStream out, String name) throws ServletException, IOException {
String user_name=name;
try {
out.println("Thank you, " + user_name);
out.println(", for writing something to keep us busy!" + "\n");
} catch (Exception e_) {
System.out.println("Got an exception when showing thanks note: " + e_.toString() );
}
}
// a standard way to assure both form methods can be safely used
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doPost(request, response);
}
}

