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>
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); } } }
<?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>
- 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>
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
- First, go to the project.
- Then open the command prompt.
- 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).
- Then go the project folder (dist/project name.war).
- Then open the tomcat manager XML.
- Deloy the war file into a tomcat server.
- 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.
yurtdışı kargo
ReplyDeleteresimli magnet
instagram takipçi satın al
yurtdışı kargo
sms onay
dijital kartvizit
dijital kartvizit
https://nobetci-eczane.org/
Q1MN1
salt likit
ReplyDeletesalt likit
dr mood likit
big boss likit
dl likit
dark likit
FNLQEP