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"/>
</zip>
<mail from="AntServlet@hotmail.com" tolist="${uemail}"
subject="test" mailhost="localhost">
<message>Hi ${uname}</message>
<fileset dir="${session.id}" includes="**/${uname}.zip"/>
</mail>
<delete dir="${session.id}"/>
</target>
</project>
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.
<html>
<body bgcolor=white>
AntServlet
<p>
<FORM ACTION="AntServlet" ENCTYPE="multipart/form-data" METHOD=POST>
name<BR>
<INPUT TYPE="TEXT" NAME="uname"><BR>
email<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>
<INPUT TYPE=SUBMIT>
</FORM>
</body>
</html>
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 |
AntServlet.war
\index.html
\WEB-INF\
\web.xml
\ant\
| \build.xml
\lib\
| \ant.jar
| \optional.jar
| \xercesImpl.jar
| \xml-apis.jar
| \activation.jar
| \mail.jar
| \mailapi.jar
| \smtp.jar
| \cos.jar
\classes\
\com\
\ru2\
\ant\
\AntServlet.class
\AntServlet.java
\HtmlReportLogger.class
\HtmlReportLogger.java
\ProjectInputHandler.class
\ProjectInputHandler.java
\ServletLogger.class
\ServletLogger.java
The following libraries are required:
activation.jarfrom Java Activation Frameworkmail.jar,mailapi.jar, andsmtp.jarfrom Java Mailcos.jarfrom Servlets.com
The Web application descriptor, web.xml, looks like this:
<web-app>
<servlet>
<servlet-name>AntServlet</servlet-name>
<servlet-class>com.ru2.ant.AntServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>AntServlet</servlet-name>
<url-pattern>/AntServlet</url-pattern>
</servlet-mapping>
</web-app>
Conclusion
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.
References
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.