package examples.servlets; import java.io.*; import java.util.*; import javax.servlet.*; import javax.servlet.http.*; import weblogic.html.*; import weblogic.utils.*; /** * This simple servlet sends a form requesting a name and an email address. * When form data is posted back from the browser, email is send to the * address given using the name given. * * @author Copyright (c) 1996-98 by WebLogic, Inc. All Rights Reserved. * @author Copyright (c) 1999 by BEA WebXpress. All Rights Reserved. */ public class MailServlet extends HttpServlet { /** * Implements the HttpServlet service() method. * This uses an htmlKona FormServlet to take the user's input, which * is formatted into an email message and posted. */ public synchronized void service(HttpServletRequest req, HttpServletResponse res) throws IOException { res.setContentType("text/html"); ServletPage sp = new ServletPage("Mail Servlet"); sp.getBody() .addElement(MarkupElement.BeginParagraph) .addElement(new HeadingElement("Form Reply")) .addElement(MarkupElement.EndParagraph); String name = (String) req.getParameter("name"); String email = (String) req.getParameter("email"); if ((name != null) && (name.length() > 0) && (email != null) && (email.length() > 0)) { try { MailUtils.sendMail("mail", "somebody@texashardhats.com", email, "Thanks!", "Hello " + name + ",\n\n" + "Thanks from the HardHat Team!\n\n" + "(email generated by MailServlet).\n"); sp.getBody() .addElement(new StringElement("Your thank you message is on its way!")); } catch (Exception e) { e.printStackTrace(); sp.getBody() .addElement(new StringElement("Sorry, we could not send mail: " + e)); } } else { // Not complete; create a form for input FormElement form = new FormElement("/mail", "POST"); TableElement te = new TableElement(); te.addElement(new TableRowElement() .addElement(new BoldElement("Email address:")) .addElement(new InputElement("email") .setSize(30) .setMaxlen(40) .setValue(email))) .addElement(new TableRowElement() .addElement(new BoldElement("Name:")) .addElement(new InputElement("name") .setSize(30) .setMaxlen(40) .setValue(name))) .addElement(new TableRowElement() .addElement(new TableDataElement (new InputElement ("SUBMIT", FieldType.submit) .setValue("Register")) .setColSpan(2))); // Add the table to the Form form.addElement(te); // Add the form to the page sp.getBody() .addElement(form); } sp.output(res.getOutputStream()); } /** * Info about the servlet. */ public String getServletInfo() { return "This mail servlet sends email"; } }