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

Introduction:

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>

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

import java.io.IOException;
import java.io.PrintWriter;
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 {

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

        Properties props = new Properties();
        props.put("mail.smtp.host", "smtp.gmail.com");
        props.put("mail.smtp.socketFactory.port", "465");
        props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
        props.put("mail.smtp.auth", "true");
        props.put("mail.smtp.port", "587");
        final String from = "jana.randhi@gmail.com";
        final String password = "Ram.RJ@5429";
        String to = "rrandhi@miraclesoft.com";
        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));
            message.setSubject(sub);
            message.setText(msg);
            //send message  
            Transport.send(message);
            response.setContentType("text/html;charset=UTF-8");
            PrintWriter out = response.getWriter();
            out.println("<!DOCTYPE html>");
            out.println("<html>");
            out.println("<head>");
            out.println("<title>Send an Email</title>");
            out.println("</head>");
            out.println("<body>");
            out.println("<h3 align='center'>Message send successfully</h3>");
            out.println("</body>");
            out.println("</html>");

        } 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="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd">
    <servlet>
        <servlet-name>Senemail</servlet-name>
        <servlet-class>com.quickdevops.Senemail</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>Senemail</servlet-name>
        <url-pattern>/Senemail</url-pattern>
    </servlet-mapping>
    <session-config>
        <session-timeout>
            30
        </session-timeout>
    </session-config>
</web-app>
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"/>
        </fileset>
    </path>
     
    <target name="init">
        <mkdir dir="build/classes"/>
        <mkdir dir="dist" />
    </target>
     
    <target name="compile" depends="init" >
        <javac includeantruntime="false" destdir="build/classes" debug="true" srcdir="src">
            <classpath refid="compile.classpath"/>
        </javac>
    </target>
     
    <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"/>
        </war>
    </target>
     
    <target name="clean">
        <delete dir="dist" />
        <delete dir="build" />
    </target>

</project>

Ant script
Explanation:
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 target.in 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.

SUBSCRIBE TO OUR NEWSLETTER

I’m the Founder of quickdevops.com. 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"