javawebparts.servlet
Class FormSenderServlet

java.lang.Object
  |
  +--javax.servlet.GenericServlet
        |
        +--javax.servlet.http.HttpServlet
              |
              +--javawebparts.servlet.FormSenderServlet
All Implemented Interfaces:
java.io.Serializable, javax.servlet.Servlet, javax.servlet.ServletConfig

public class FormSenderServlet
extends javax.servlet.http.HttpServlet

The FormSenderServlet allows you to accept the submission of a form and take values from that form, insert them into a template, and send the resultant text as an eMail message to a specified address or list of addresses.

Example configuration in web.xml:

<servlet>
  <servlet-name>FormSenderServlet</servlet-name>
  <servlet-class>javawebparts.servlet.FormSenderServlet
  </servlet-class>
  <init-param>
    <param-name>smtpHost</param-name>
    <param-value>????</param-value>
  </init-param>
  <init-param>
    <param-name>smtpLogonRequired </param-name>
    <param-value>true</param-value>
  </init-param>
  <init-param>
    <param-name>smtpUsername</param-name>
    <param-value>????</param-value>
  </init-param>
  <init-param>
    <param-name>smtpPassword</param-name>
    <param-value>????</param-value>
  </init-param>
  <init-param>
    <param-name>fromAddress</param-name>
    <param-value> ;FormSenderServlet@javawebparts.sourceforge.net
</param-value>
  </init-param>
  <init-param>
    <param-name>toAddresses</param-name>
    <param-value>????</param-value>
  </init-param>
  <init-param>
    <param-name>subject</param-name>
    <param-value>This is a test </param-value>
  </init-param>
  <init-param>
    <param-name>staticTokens</param-name>
    <param-value>ccList=Bill in Accounting, Dana in Legal~~deskOf=Beelzebub</param-value>
  </init-param>
  <init-param>
    <param-name>templateFile</param-name>
    <param-value>/WEB-INF/eMailTemplate.txt </param-value>
  </init-param>
  <init-param>
    <param-name>pageAfter</param-name>
    <param-value> /packages_jsp/servlet/mailSent.jsp</param-value>
  </init-param>
</servlet>

Init parameters explained:


Creating an eMail template is simple. It is just plain text with some replacement tokens present. The tokens are in the form ${xxx}, where xxx is the name of the token. The tokens are replaced with values from one of two places: the static tokens defined in the staticTokens parameter, and incoming request parameters. Every request parameter is captured into a map, and this map is then used to populate the tokens.

So, for example, if you have an eMail template that contains the token ${firstName}, and you have a form submitted that has a text field named firstName, the token in the eMail template will be replaced with the value of the incoming firstName parameter form the form. Likewise, if you define a static token "lastName=Hedburg" in web.xml, and in the eMail template there is a ${lastName} token, then it will be replaced with "Hedburg".

In addition to the staticTokens, there are some built-in static tokens you can use:

Author:
Frank W. Zammetti.
See Also:
Serialized Form

Field Summary
private  java.lang.String fromAddress
          The from address used when sending the eMail.
private static org.apache.commons.logging.Log log
          Log instance.
private  java.lang.String pageAfter
          The JSP to forward to after sending the message.
private  java.lang.String smtpHost
          The SMTP host name to send the message through.
private  boolean smtpLogonRequired
          Flag: Does the SMTP server require logon credentials or not?
private  java.lang.String smtpPassword
          Password to log on to the SMTP server (if smtpRequiresLogin is "true").
private  java.lang.String smtpUsername
          Username to log on to the SMTP server (if smtpRequiresLogin is "true").
private  java.util.HashMap staticTokens
          The list of static tokens configured.
private  java.lang.String subject
          The subject of the eMail to send.
private  java.lang.String template
          The actual eMail template.
private  java.lang.String templateFile
          The path and name of the context-relative eMail template file.
private  java.util.ArrayList toAddresses
          The list of eMail addresses to send eMail to.
 
Fields inherited from class javax.servlet.http.HttpServlet
 
Fields inherited from class javax.servlet.GenericServlet
 
Constructor Summary
FormSenderServlet()
           
 
Method Summary
 void doGet(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response)
          doGet.
 void doPost(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response)
          doPost.
 void init(javax.servlet.ServletConfig config)
          init.
 void sendMessage(java.lang.String inText)
          This method sends a message.
 
Methods inherited from class javax.servlet.http.HttpServlet
doDelete, doHead, doOptions, doPut, doTrace, getLastModified, service, service
 
Methods inherited from class javax.servlet.GenericServlet
destroy, getInitParameter, getInitParameterNames, getServletConfig, getServletContext, getServletInfo, getServletName, init, log, log
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

log

private static org.apache.commons.logging.Log log
Log instance.


smtpHost

private java.lang.String smtpHost
The SMTP host name to send the message through.


smtpUsername

private java.lang.String smtpUsername
Username to log on to the SMTP server (if smtpRequiresLogin is "true").


smtpPassword

private java.lang.String smtpPassword
Password to log on to the SMTP server (if smtpRequiresLogin is "true").


smtpLogonRequired

private boolean smtpLogonRequired
Flag: Does the SMTP server require logon credentials or not?


fromAddress

private java.lang.String fromAddress
The from address used when sending the eMail.


toAddresses

private java.util.ArrayList toAddresses
The list of eMail addresses to send eMail to.


subject

private java.lang.String subject
The subject of the eMail to send.


templateFile

private java.lang.String templateFile
The path and name of the context-relative eMail template file.


template

private java.lang.String template
The actual eMail template.


pageAfter

private java.lang.String pageAfter
The JSP to forward to after sending the message.


staticTokens

private java.util.HashMap staticTokens
The list of static tokens configured.

Constructor Detail

FormSenderServlet

public FormSenderServlet()
Method Detail

init

public void init(javax.servlet.ServletConfig config)
          throws javax.servlet.ServletException
init.

Specified by:
init in interface javax.servlet.Servlet
Overrides:
init in class javax.servlet.GenericServlet
Parameters:
config - ServletConfig.
Throws:
javax.servlet.ServletException - ServletException.

doGet

public void doGet(javax.servlet.http.HttpServletRequest request,
                  javax.servlet.http.HttpServletResponse response)
           throws javax.servlet.ServletException,
                  java.io.IOException
doGet. Calls doPost() to do real work.

Overrides:
doGet in class javax.servlet.http.HttpServlet
Parameters:
request - HTTPServletRequest.
response - HTTPServletResponse.
Throws:
javax.servlet.ServletException - ServletException.
java.io.IOException - IOException.

doPost

public void doPost(javax.servlet.http.HttpServletRequest request,
                   javax.servlet.http.HttpServletResponse response)
            throws javax.servlet.ServletException,
                   java.io.IOException
doPost.

Overrides:
doPost in class javax.servlet.http.HttpServlet
Parameters:
request - HTTPServletRequest
response - HTTPServletResponse
Throws:
javax.servlet.ServletException - ServletException
java.io.IOException - IOException

sendMessage

public void sendMessage(java.lang.String inText)
This method sends a message.

Parameters:
inText - The text of the message.


Copyright 2005 Frank W. Zammetti