How to write the ant script for sample web application step by step


In this web project, I am sending an email to the user. here I am providing the input box and submit button, just type your text and enter the submit button. Mail will send automatically to the user with an input text value.

For this web application, I am writing the ant script and generating the war and deploy into the tomcat server.

Here is my index.html page
<!DOCTYPE html>

        <title>Send an email</title>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <div align="center">
            <form action="Senemail" method="post">
                <input type="text" name="Text" placeholder="Enter text here">
                <input type="submit" name="submit" value="send"/>
Here is my servlet class
package com.quickdevops;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Properties;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

 * @author janardhan randhi
public class Senemail extends HttpServlet {

    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

        Properties props = new Properties();
        props.put("", "");
        props.put("mail.smtp.socketFactory.port", "465");
        props.put("mail.smtp.socketFactory.class", "");
        props.put("mail.smtp.auth", "true");
        props.put("mail.smtp.port", "587");
        final String from = "";
        final String password = "Ram.RJ@5429";
        String to = "";
        String sub = "Greetngs From Quickdevops";
        String msg = request.getParameter("Text");
        Session session = Session.getDefaultInstance(props, new javax.mail.Authenticator() {
            protected PasswordAuthentication getPasswordAuthentication() {
                return new PasswordAuthentication(from, password);
        //compose message    
        try {
            MimeMessage message = new MimeMessage(session);
            message.addRecipient(Message.RecipientType.TO, new InternetAddress(to));
            //send message  
            PrintWriter out = response.getWriter();
            out.println("<!DOCTYPE html>");
            out.println("<title>Send an Email</title>");
            out.println("<h3 align='center'>Message send successfully</h3>");

        } catch (MessagingException e) {
            throw new RuntimeException(e);

Here is my web.xml file
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.1" xmlns="" xmlns:xsi="" xsi:schemaLocation="">
Here are the libraries to send an email to the user.
  • activation.jar
  • java-mail-1.4.4.jar
Here is my ant script file
<?xml version="1.0" ?>
<project name="SendEmail" default="war">
    <path id="compile.classpath">
        <fileset dir="libs">
            <include name="*.jar"/>
    <target name="init">
        <mkdir dir="build/classes"/>
        <mkdir dir="dist" />
    <target name="compile" depends="init" >
        <javac includeantruntime="false" destdir="build/classes" debug="true" srcdir="src">
            <classpath refid="compile.classpath"/>
    <target name="war" depends="compile">
        <war destfile="dist/Sendemail.war" webxml="web/WEB-INF/web.xml">
            <fileset dir="web"/>
            <lib dir="libs"/>
            <classes dir="build/classes"/>
    <target name="clean">
        <delete dir="dist" />
        <delete dir="build" />


Ant script
Any java web application web files, XML files, and Java classes and libraries are mandatory. Here I am creating the two directories for all generated java class file goes into build/class folder. And then created the dist folder for war file.

while executing the build.xml first will trigger the war target. but it will depend on compile ten compile depends on init so first trigger the init this init target creating the two directories(class files and war file).

In compile target (compiles all java classes and placed into build/classes folder).

In war, section copy the web.xml file and class file with libraries into one component (war).

In clean section delete the directories (build and dist).

Steps to Execute the Ant script
  1. First, go to the project.
  2. Then open the command prompt.
  3. Type the ant or type Ant clean (it will clean if incase directories already created )(it will check build.xml files is there or not if it's found it will execute).
  4. Then go the project folder (dist/project name.war).
  5. Then open the tomcat manager XML. 
  6. Deloy the war file into a tomcat server.
  7. Check weather application is successfully running or not.
Output through video:
Note: Just copy all files and create a web project and run with ant . in this way to create war or jar files for web applications.


I’m the Founder of I am a Professional Blogger, Application developer, YouTuber. I’ve been blogging since 2015.I spend a lot of time learning new techniques and actively help other people learn web development through a variety of help groups and writing web development tutorials for my website and blog about advancements in web design and development.Besides programming I love spending time with friends and family and can often be found together going out catching the latest movie or planning a trip to someplace I've never been before.

2 Responses to "How to write the ant script for sample web application step by step"