/* * @(#)EJBMailServlet.java 1.00 3/4/2000 */ /* ***************************************************************************** Copyright 2000 by Erica Andrews (cylikon@hotmail.com). All rights reserved. ./ButchWhipAppeal/ ./IcedPinky/ NO portion of this software may be used in ANY way by anyone other than myself without my express written consent. If it is downloaded for testing, it must be deleted from your system within 24 hours. Any other use constitutes piracy and fraudulent misuse of this software, punishable in a court of law. Copyright 2000 by Erica Andrews. All rights reserved. ***************************************************************************** */ import java.io.*; import javax.servlet.*; import javax.servlet.http.*; import java.net.InetAddress; import java.util.Properties; import javax.ejb.*; import javax.naming.*; import java.rmi.*; /** * EJBMailServlet class. * @author Li'L CJ aka Erica Andrews * @version 1.00 03/04/2000 */ // This servlet supports both the doGet and doPost method. // doGet, for when we need to access this mailer for through something like a simple HTML link // doPost, this is the actual method supported by the HTML form created in the servlet. // This servlet also uses the JavaMail API to send mail; however, unlike my other example // this servlet looks much cleaner and simpler because all mail validation and sending // with the JavaMail API is done on the back end inside a stateless session EJB. public class EJBMailServlet extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse resp) throws IOException, ServletException { // setting up the print writer and mime type PrintWriter out = resp.getWriter(); resp.setContentType("text/html"); String FROM = request.getParameter("from"); String SUBJECT = request.getParameter("subject"); String MESSAGE = request.getParameter("message"); String userinfo = request.getParameter("userinfo"); InetAddress here = InetAddress.getLocalHost(); // the following string is used to collect some information about the user, the site they saw, URL referrer and their browser String UserI = "\n URL: "+request.getRequestURI()+"\n Method: "+request.getMethod()+"\n Servlet Path: "+request.getServletPath()+"\n Query String: "+request.getQueryString()+"\n Remote User: "+request.getRemoteUser()+"\n Remote Address: "+request.getRemoteAddr()+"\n Remote Host: "+request.getRemoteHost()+"\n Web Browser: "+request.getHeader("User-Agent")+"\n\n Connected on Server : "+request.getServerName()+", IP:"+here.getHostAddress()+",Port:"+request.getServerPort(); // first dealing with cases in which the user is seeing the mail page for the first time and have not sent anything yet if (FROM == null && SUBJECT == null && MESSAGE == null) { out.print("Use this form to send your comments, suggestions, compliments, and to report any problems you found while using this site.

From:
Subject:

Your Message:

"); // now, dealing with cases in which the user has tried to send something in the mail form. } else { // if there was something returned in any of the getParameter methods, let's try to send the mail using // the EJB - No need to do mail validation here, all validation is being done by the EJB out.println("

"); // let's go ahead and send this portion of the HTML back to the client while we perform // the EJB lookup, so that they can start loading the page while they wait. out.flush(); try { // perform the EJB lookup Properties p = new Properties(); p.put(Context.INITIAL_CONTEXT_FACTORY, "org.ejboss.naming.spi.EjbossClientContextFactory"); Context ctx = new InitialContext(p); // get a reference to the EJB"s Home interface cylikon.EJBMail.EJBMailDaemonHome home = (cylikon.EJBMail.EJBMailDaemonHome) ctx.lookup("EJBMailDaemon"); // get a reference to the EJB's Remote interface by creating a new instance cylikon.EJBMail.EJBMailDaemon mail = home.create(); // now, we can send the mail and get the mail status message in one action. // The mail will be sent when we call the string SendMail(String,String,String,String) in the EJB... out.print(mail.SendMail(FROM,SUBJECT,MESSAGE,userinfo)); // this is where the Mail will actually be sent out.print("

"); } catch (Exception e) { out.print("
Problem occured with the JNDI naming and Bean lookup.
"+e+e.getMessage()); } //end try-catch } // end if-else } // end doGet method public void doPost(HttpServletRequest request, HttpServletResponse resp) throws IOException, ServletException { // setting up the print writer and mime type PrintWriter out = resp.getWriter(); resp.setContentType("text/html"); String FROM = request.getParameter("from"); String SUBJECT = request.getParameter("subject"); String MESSAGE = request.getParameter("message"); String userinfo = request.getParameter("userinfo"); InetAddress here = InetAddress.getLocalHost(); // the following string is used to collect some information about the user, the site they saw, URL referrer and their browser String UserI = "\n URL: "+request.getRequestURI()+"\n Method: "+request.getMethod()+"\n Servlet Path: "+request.getServletPath()+"\n Query String: "+request.getQueryString()+"\n Remote User: "+request.getRemoteUser()+"\n Remote Address: "+request.getRemoteAddr()+"\n Remote Host: "+request.getRemoteHost()+"\n Web Browser: "+request.getHeader("User-Agent")+"\n\n Connected on Server : "+request.getServerName()+", IP:"+here.getHostAddress()+",Port:"+request.getServerPort(); // first dealing with cases in which the user is seeing the mail page for the first time and have not sent anything yet if (FROM == null && SUBJECT == null && MESSAGE == null) { out.print("Use this form to send your comments, suggestions, compliments, and to report any problems you found while using this site.

From:
Subject:

Your Message:

"); // now, dealing with cases in which the user has tried to send something in the mail form. } else { // if there was something returned in any of the getParameter methods, let's try to send the mail using // the EJB - No need to do mail validation here, all validation is being done by the EJB out.println("

"); // let's go ahead and send this portion of the HTML back to the client while we perform // the EJB lookup, so that they can start loading the page while they wait. out.flush(); try { // perform the EJB lookup Properties p = new Properties(); p.put(Context.INITIAL_CONTEXT_FACTORY, "org.ejboss.naming.spi.EjbossClientContextFactory"); Context ctx = new InitialContext(p); // get a reference to the EJB"s Home interface cylikon.EJBMail.EJBMailDaemonHome home = ( cylikon.EJBMail.EJBMailDaemonHome) ctx.lookup("EJBMailDaemon"); // get a reference to the EJB's Remote interface by creating a new instance cylikon.EJBMail.EJBMailDaemon mail = home.create(); // now, we can send the mail and get the mail status message in one action. // The mail will be sent when we call the string SendMail(String,String,String,String) in the EJB... out.print(mail.SendMail(FROM,SUBJECT,MESSAGE,userinfo)); // this is where the Mail will actually be sent out.print("

"); } catch (Exception e) { out.print("
Problem occured with the JNDI naming and Bean lookup.
"+e+e.getMessage()); } //end try-catch } // end if-else } // end doPost method /** * Initializes this servlet. */ public void init(ServletConfig config) throws ServletException { super.init(config); // not using this method in this example } }