For those new to Struts and in particular the Struts HTML library it might come as a surprise that there is no id property on their tags. Don’t get frustrated the tag library document clearly states that the styleId property renders its assigned value as the element id. This means that instead of id use styleId as shown by the following example.

Example

HTML

<input type="text" name="firstName" id="firstName"
maxlength="10" size="12" />

Struts HTML

<html:text property="firstName" styleId="firstName"
maxlenght="10" size="12" />

Resources

, ,

In the previous tutorial we walked through how to send email from a Java Application. This tutorial extends off that and explains how to send email from a SMTP server that requires  basic User Authentication in the form of a user name and password.  This is performed by use of the Authenticator object.

Prerequisites

This tutorial builds off of the previous example and as such you will need to review that tutorial to understand the requirements necessary to utilize the JavaMail API.

How to send an email from SMTP Server requiring User Name/Password Authentication

To get started lets build off of our MailUtil class we have already created by copying the class and refactoring it to BasicAuthMailUtil. For those who don’t know what refactoring is it means to rename the class.

After we have refactored the class our first step is to create our Authenticator object. Now we could do this by creating a separate class or creating an internal class. For our purposes we will create an internal class. To create an internal class you simply declare a class inside the class declaration of another class. We would do this by placing our BasicSMPTAuthenticator class stub before the closing } of our BasicAuthMailUtil class.

public class BasicAuthMailUtil {
  /* existing code */
 
  private class BasicSMTPAuthenticator
        extends javax.mail.Authenticator
  {
 
  }
 
}

For our Authenticator to function we need to implement the getPasswordAuthentication method which will return to the caller a PasswordAuthentication object with the desired username and password

public PasswordAuthentication getPasswordAuthentication()
{
  return new PasswordAuthentication( AUTH_USER, AUTH_PSWD );
}

You could replace the AUTH_USER and AUTH_PSWD values directly with the user and password information for the server or define them as variables of the BasicAuthMailUtil Class.

private static String AUTH_USER = "USER NAME";
private static String AUTH_PSWD = "PASSWORD";

Now that our Authenticator is created the last step is to initialize the session with our Authenticator. Therefore we will modify:

Session sess = Session.getInstance(props);

with the following which instantiates our Authenticator then obtains the Mail Session Instance based on the properties and Authenticator.

Authenticator auth = new BasicSMTPAuthenticator();
Session sess = Session.getDefaultInstance( props, auth );

Congratulations you now have a utility class that can send emails from a SMTP Server that requires user name and password authentication.

Resources

, , , , ,

Sending email from your java application is very straightforward when you utilize the JavaMail API.  This library contains all the functionality necessary to connect to a SMTP Mail Server and send your email to its desired recipients.  The next time you have an application performing some vital functionality instead of just logging an exception send an email to the administrator or development team so that the issue can be resolved right away.

Prerequisites

In order to use the javaMail API you need to obtain both the Java Mail and Activation Framework libraries which can be found at the following locations:

How to Send an Email

To send an email from java we must first start by importing the required classes into our MailUtil java class.

import javax.mail.*;
import javax.mail.internet.*;
import java.util.*;

After we have imported the necessary resources lets go ahead with the creation of our postMail function that will send and email with subject and message, to the specified recipients, cc recipients from the sender. Also it is important to note that we will be throwing any MessagingExceptions that occur so that the caller will be responsible for handling them as they see fit.

public static void postMail( String recipients[],
    String subject, String message, String from,
    String ccRecipients[] ) throws MessagingException {
 
}

Now that we have our method stub in place lets begin by initializing a Mail Session configured to our SMTP Mail Server. Please note that if you utilize logging in your application you can also set the debug mode to true to get useful information.

//Create Properties
Properties props = new Properties();
props.put( "mail.smtp.host", HOST );
props.put("mail.debug", "true" );
 
//Get Session
Session sess = Session.getInstance(props);

After our session is initialize we can go ahead with creating our email message.

Message msg = new MimeMessage( sess );

Populate the From email address by use of the InternetAddress class

msg.setFrom(new InternetAddress(from) );

We can populate the recipents of the message utilizing the setRecipients function. This function takes two parameters the Type of recipient and the array of recipients. So we would use Message.RecipientType.TO and Message.RecipientType.CC for To and CC recipients.

// recipients
InternetAddress[] addressTo =
      new InternetAddress[recipients.length];
for( int i=0; i &lt; recipients.length; i ++ ) {
  addressTo[i] = new InternetAddress( recipients[i] );
}
msg.setRecipients( Message.RecipientType.TO, addressTo );
 
// ccRecipients
if( ccRecipients != null &amp;&amp; ccRecipients.length &gt; 0 ) {
  InternetAddress[] addressCC =
        new InternetAddress[ccRecipients.length];
  for( int i=0; i &lt; ccRecipients.length; i ++ ) {
    addressCC[i] = new InternetAddress( ccRecipients[i] );
  }
  msg.setRecipients( Message.RecipientType.CC, addressCC );
}

After populating the recipients of the email we can set the subject and Date of the email

msg.setSubject(subject);
msg.setSentDate(new Date() );

Finally we set the content of the email. Please note that in most cases the text format will be text/plain for simple text emails or text/html for html formatted messages.

msg.setContent( message, "text/html" );

Now that our message is prepared we send utilizing the Transport class.

Transport.send( msg);

And that is how to send an email from your Java Application, The full source for the created Utility class can be found below. If your server requires Password authentication then you may be interested in the Advanced Tutorial.

Resources

, , , ,

The php $_SERVER array is a super global that contains information about the server and environment that the php script is running in. Within this array there are 3 indexes or values that behave similarly and it is often hard to know which one to utilize. They are:

  • $_SERVER[ 'PHP_SELF' ]
  • $_SERVER[ 'SCRIPT_NAME' ]
  • $_SERVER[ 'REQUEST_URI' ]

To compare these variables lest look at the following 4 requests made to the same script

  • 1 : Default file in folder

    http://www.example.com/projects/

  • 2 : Direct script access

    http://www.example.com/projects/index.php

  • 3 : Script access with variables

    http://www.example.com/projects/index.php?categroy=12&id=263

  • 4 : Script access with appended directories

    http://www.example.com/projects/index.php/test/medium/

PHP_SELF

The PHP_SELF index contains the file name of the script currently executing in relation to the document root of the server it is being run on. What this means is taht for all cases except for 4 /projects/index.php will be returned. In the case where directories are appended they will get displayed by PHP_SELF and /projects/index.php/test/medium/ would be returned.

SCRIPT_NAME

The SCRIPT_NAME index like PHP_SELF is used to return the name of the executing script in relation to the document root. The one difference is that regardless of appended directories or query parameters the file is always returned the same as /projects/index.php

REQUEST_URI

The REQUEST_URI index is used to obtain the exact request that was requested from the server. This includes any query parameters or appended directories. So for each request everything after the root would be returned:

  • /projects/
  • /projects/index.php
  • /projects/index.php?categroy=12&id=263
  • /projects/index.php/test/medium/

For a full explanation of the various indices of the $_SERVER super global please visit the PHP Manual.

Resources

ex

, , ,

When querying or working with data based on datetime, date, or time values Sybase has a group of Date functions available to help perform arithmetic operations.  These functions include GETDATE, MONTH, YEAR, DATEADD, and DATEDIFF.

To help illustrate some of these functions lets assume we have the following ITEM, ORDERS, and ORDERS_ITEM tables that contains the following columns and data.

ITEM
ID NAME PRICE
1 Crystal Vase 30.00
2 White vase 5.00
3 Silver Vase 15.00
4 Tall Vase 12.00
5 Gold Vase 25.00
6 Blue Glass Vase 20.00
7 Pewter Vase 11.00
ORDERS
ID BUY_DATE
1 12/13/2008
2 12/17/2008
3 12/24/2008
4 1/02/2009
5 1/05/2009
6 1/15/2009
7 2/03/2009
ORDER_ITEM
ORDER_ID ITEM_ID QUANTITY
1 3 2
1 1 6
2 1 7
3 4 5
3 3 8
3 7 7
4 6 10
5 5 6
6 3 3
7 2 3
7 7 1

How to obtain the current date and or time

The GETDATE function is used to get the current system date and time. Lets assume that we want to obtain a report of the orders that were made today, to do so we would want to compare the ORDERS.BUY_DATE column with the current date. As our table is not storing the time and only the date we would make use of the current_date function to only get the date (likewise the current_time function returns the time).

SELECT * FROM ORDERS WHERE BUY_DATE = CURRENT_DATE()

Assuming today’s date was February 3rd, 2009 we would have the following results:

ID BUY_DATE
7 2/03/2009

Modifying the time using DATEADD

Now that we know how to get the current date and time in a Sybase T-SQL, lets assume we have a report that runs every day in the morning that reports the orders that were made the previous day. To do this we can modify our previous example to compare the date against the current date – 1 day. Sybase provides the DATEADD function to modify the date.

SELECT * FROM ORDERS
WHERE BUY_DATE = DATEADD( dd, -1, CURRENT_DATE() )

The DATEADD function takes 3 parameters the part of date to be modified, value to modify by, and the date to be modified.
Assuming today’s date was February 4th, 2009 we would have the same results as the previous example

ID BUY_DATE
7 2/03/2009

Utilizing the month and year parts of a Date

Sybase provides two convenient methods for obtaining the month or year part of a Date. These functions can be used to query data for a particular period of time.  Both the MONTH, and YEAR functions take a single parameter and that is the date for which the month or year should be returned.

For our first example lets assume we want to know which items and how many were sold for the current month. To do this we will need to join the ORDERS and ITEM tables based on the ORDERS_ITEM linking table and return the sum of the quantity that were sold for each order.

SELECT i.NAME, SUM(oi.QUANTITY) AS NUM_SOLD
FROM ORDERS o
  JOIN ORDER_ITEM oi ON o.ID = oi.ORDER_ID
  JOIN ITEM i ON i.ID = oi.ITEM_ID
WHERE MONTH (o.BUY_DATE) = MONTH( CURRENT_DATE() )
GROUP BY i.NAME

Running this query assuming that the current date is the last day of December, 2008 would return the following results.

NAME NUM_SOLD
Silver Vase 10
Crystal Vase 13
Tall Vase 5
Pewter Vase 7

Sybase provides additional Date functions besides those mentioned above to familiarize yourself with them on I suggest accessing their online manual which can be accessed under References.

References

, , , , ,