ONJava.com -- The Independent Source for Enterprise Java
oreilly.comSafari Books Online.Conferences.


AddThis Social Bookmark Button

Java Software Automation with Jakarta Ant
Pages: 1, 2, 3

Our AntServlet is also employing the com.oreilly.servlet package to manage file uploads in the temporary session directory.


  protected void service( HttpServletRequest req, HttpServletResponse res) 
        throws ServletException, IOException {
    HttpSession session = req.getSession();    
    ServletContext context = getServletContext();
    String dir = context.getRealPath( "/WEB-INF/ant/"+session.getId().hashCode());
    HttpServletRequest request = req;

    String type = req.getHeader( "Content-Type");
    if( type!=null && type.startsWith( "multipart/form-data"))
      request = new MultipartWrapper( req, dir);


Now we are ready to prepare a simple build.xml file for our Web application automation that will pick up all of the uploaded files, compress them into a zip archive, and then send an email with this fille attached.

  <?xml version="1.0"?>

  <project default="main" basedir=".">

    <target name="main">
      <echo message="Ant started for id ${session.id}"/>
      <echo message="${uname}"/>
      <echo message="${uemail}"/>
      <zip destfile="${session.id}/${uname}.zip">
        <fileset dir="${session.id}" includes="*" excludes="**/${uname}.zip"/>
      <mail from="AntServlet@hotmail.com" tolist="${uemail}" 
            subject="test" mailhost="localhost">
        <message>Hi ${uname}</message>
        <fileset dir="${session.id}" includes="**/${uname}.zip"/>
      <delete dir="${session.id}"/>


You can use the following simple HTML form to invoke our AntServlet. It allows you to supply the uname and uemail parameters, and pick the files for upload.

  <body bgcolor=white>

  <FORM ACTION="AntServlet" ENCTYPE="multipart/form-data" METHOD=POST>
  <INPUT TYPE="TEXT" NAME="uname"><BR>
  <INPUT TYPE="TEXT" NAME="uemail"><BR>
  file 1<BR>
  <INPUT TYPE="FILE" NAME="file1"><BR>
  file 2<BR>
  <INPUT TYPE="FILE" NAME="file2"><BR>
  file 3<BR>
  <INPUT TYPE="FILE" NAME="file3"><BR>
  file 4<BR>
  <INPUT TYPE="FILE" NAME="file4"><BR>


Before deploying and testing this sample application, you might want to package it as a .WAR file. The AntServlet.war file should have the following structure:

Related Reading

Ant: The Definitive Guide
By Jesse E. Tilly, Eric M. Burke

        |   \build.xml
        |   \ant.jar
        |   \optional.jar
        |   \xercesImpl.jar
        |   \xml-apis.jar
        |   \activation.jar
        |   \mail.jar
        |   \mailapi.jar
        |   \smtp.jar
        |   \cos.jar

The following libraries are required:

The Web application descriptor, web.xml, looks like this:



The given example demonstrates the simplicity and flexibility of using Ant-driven build scenarios for your custom software automation. It shows how the same Ant script can be called from the command prompt as well as from a GUI, or even from inside of the Web container.


Eugene Kuleshov is an independent consultant with over 15 years of experience in software design and development.

Dmitry Platonoff is a Senior Developer with Think Dynamics Canada, specializing in web application architecture and user interface concepts.

Return to ONJava.com.