You are on page 1of 31

RAD

Practicals
(No author respomsibility. Refer on your own risk.)

Q1

Matrix.JSP
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Insert title here</title> </head> <body>

<!-- Forming a 2D matrice with JSP Matrice values will be printed inside a table --> <% //indicates that we are going to type JAVA codes inside HTML out.print("<table width='400'>");// to establish HTML <table> within JSP for(int row=0;row<10;row++){ out.print("<tr>");//to establish HTML <tr> within JSP for(int col=0;col<10;col++){ //calculate matrice values out.print("<td>"+row*col +"</td>"); } out.print("</tr>"); } out.print("</table>"); %> </body> </html>

Q2

Timer.JSP
<%@page import="java.text.SimpleDateFormat"%> <!-- This is how we import classes in JSP --> <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" import="java.util.*" import="java.io.*"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Insert title here</title> </head> <body>

<% //Create the object dNow using class Date to get today's date Date dNow=new Date(); //Create a date format as we wish (05:30:45) SimpleDateFormat sDF=new SimpleDateFormat("hh:mm:ss"); //we put our today's date object into our created date format and display out.print("<h1>Hello! the Time is now :"+sDF.format(dNow)+"</h2>"); %> </body> </html>

Q3

Login.HTML
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Insert title here</title> </head> <body> <!-- Sent the collected data from the form to the servelet specified in the attribute action. It will go into the doPost method in servelet since mthod in the form is specified as post --> <form action="http://localhost:8080/Assignment04/LogInServer" method="post"> <table> <tr> <td>First Name :</td> <td><input type="text" name="fName"></td>Checking Your Name</h1>q </tr>Checking Your Name</h1> <tr> <td>Last Name :</td> <td><input type="text" name="lName"></td> </tr> <tr> <td></td> <td><input type="Submit" value="Submit"></td> </tr> </table> </form> </body> </html>

LogInServer.java package logIn; import java.io.IOException; import java.util.Scanner; import java.io.PrintWriter; import import import import import javax.servlet.ServletException; javax.servlet.annotation.WebServlet; javax.servlet.http.HttpServlet; javax.servlet.http.HttpServletRequest; javax.servlet.http.HttpServletResponse;

/** * Servlet implementation class LogInServer */ @WebServlet("/LogInServer") public class LogInServer extends HttpServlet { private static final long serialVersionUID = 1L; protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html"); //must be imported. Helps to give outputs in the browser PrintWriter out =response.getWriter(); //collects the data from HTML page String fName=request.getParameter("fName"); String lName=request.getParameter("lName"); //out is a object of PrintWriter class. It should be imported to use print() as below out.print("<h1>Checking Your Name</h1>"); out.print("<p>"+"Your First Name Is :"+fName+"</p>"); out.print("<p>"+"Your Last Name Is :"+lName+"</p>"); if(fName.equals("Kamal") && lName.equals("Sarath")){ out.print("<p style='color:green'>"+"Name: OK"+"</p>"); }else{ out.print("<p style='color:red'>"+"You are Not permitted to Enter"+"</p>"); } } }

Q4

LogIn.HTML
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Insert title here</title> </head> <body> <h1>Enter Your User name and password </h1> <form method="post" action="http://localhost:8080/Assignment04/LogIn2"> <table> <tr> <td>User Name :</td> <td><input type="text" name="uName"></td> </tr> <tr> <td>Password :</td> <td><input type="password" name="password"></td> </tr> <td></td> <td><input type="submit" value="Submit"></td> </tr> </table> </form> </body> </html>

LogIn2.java
package logIn2; import java.io.IOException; import java.io.PrintWriter; import import import import import javax.servlet.ServletException; javax.servlet.annotation.WebServlet; javax.servlet.http.HttpServlet; javax.servlet.http.HttpServletRequest; javax.servlet.http.HttpServletResponse;

/** * Servlet implementation class LogIn2 */ @WebServlet("/LogIn2") public class LogIn2 extends HttpServlet { private static final long serialVersionUID = 1L; protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { PrintWriter out=response.getWriter(); String uName=request.getParameter("uName"); String pwd=request.getParameter("password"); if(uName.equals("Kamal") && pwd.equals("123")){ String resultString="Congragulation Vlad......"; out.print("<h1>"+"Congragulation Vlad"+"</h1>"); /*Load the page specified in the URL. '? resultStatement="+resultString' can be use to send data from this servelet to the JSP page which specifid in the URL. resultString is a string variable which holds the string that I need to send */ response.sendRedirect("http://localhost:8080/Assignment04/jspSet/Result.jsp? resultStatement="+resultString);

}else{ out.print("<h1>"+"Wrong Password For"+uName+"</h1>"); out.print("<a href='http://localhost:8080/Assignment04/GeneralWeb/Login2.html'>Try again</a>"); } } }

Result.JSP
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Insert title here</title> </head> <body> <% //print the string that was sent from the servelet. 'resultStatement is the name of the parameter' out.print(request.getParameter("resultStatement")); %> <!-- Data exchange is happenning between two JSP files --> <!-- Data inside the form will be transfered to the DisplayResult.jsp using post method (secure method) --> <form action="http://localhost:8080/Assignment04/jspSet/DisplayResult.jsp" method="post"> <table> <tr> <td>First Name :</td> <td><input type="text" name="fName"><!-- Name for the text field will be used to identify its data uniqeuly in pages in other side --></td> </tr> <tr> <td>Last Name :</td> <td><input type="text" name="lName"></td> </tr> <tr> <td>Address :</td> <td><input type="text" name="address"></td> </tr> <td>Country :</td> <td> <!-- Creating a combo box to select a country --> <select name="country"> <option value="Sri Lanka">Sri Lanka</option> <option value="India">India</option> <option value="America">America</option> </select> </td> </tr> <tr> <td>Telephone :</td> <td><input type="text" name="tpn"></td> </tr> <tr> <td></td> <td><input type="submit" value="Submit"></td> </tr> </table> </form> </body> </html>

DisplayResult.JSP
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Insert title here</title> </head> <body> <% //Retrive the data from Result.jsp into variables String fName=request.getParameter("fName"); String address=request.getParameter("address"); String tpn=request.getParameter("tpn"); String country=request.getParameter("country"); %> <!-- Display the values of the string variables --> <h2>Hi again <%=fName %>, Your Details are</h2> firstName :<%=fName %><br> address :<%=address %><br> Telephone : <%=tpn %><br> Country :<%=country %> </body> </html>

Q5

Name.HTML
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Insert title here</title> </head> <body> <h1>Enter a part of a name.....</h1> <!-- Enter the part of the name and send to the servelet LogIn.java with the method GET --> <form action="http://localhost:8080/Assignment6/LogIn" method="get"> <input type="text" name="namePart"> <input type="submit"> </form> </body> </html>

LogIn.java
package dBLogIn; import java.io.IOException; import java.io.PrintWriter; import import import import import import import import import import javax.servlet.ServletException; javax.servlet.annotation.WebServlet; javax.servlet.http.HttpServlet; javax.servlet.http.HttpServletRequest; javax.servlet.http.HttpServletResponse; java.sql.Connection; java.sql.DriverManager; java.sql.PreparedStatement; java.sql.ResultSet; java.sql.SQLException;

/** * Servlet implementation class LogIn */ @WebServlet("/LogIn") public class LogIn extends HttpServlet { private static final long serialVersionUID = 1L;

/** * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response) */ protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { PrintWriter out=response.getWriter(); //get the sent data from the EnterName.HTML String fName=request.getParameter("namePart"); //Try catch is compulsory since we are going to interact with a database. try { //MySQL connector should be imported to the JRE System Library int count=0; Class.forName("com.mysql.jdbc.Driver"); //Creating the MySQL connection. (friends=Name of the database, root=userName, root=userName) Connection con =DriverManager.getConnection("jdbc:mysql://localhost:3306/friends","root","root" ); // Compose the SQL query PreparedStatement statement = con.prepareStatement("select first,last from names where last like \"%"+fName+"%\" or first like \"%"+fName+"%\";");

ResultSet result = statement.executeQuery(); out.print("<html>" + "<body>"); boolean check=false; if(fName!=""){ while (result.next()){ if(count==0){ out.print("<h2 style='color:green'>Search results for "+fName+"</h2>"); } //View the results from the names table in database friends out.println(result.getString(1) +" "+ result.getString(2)+"<br>"); check=true; count++; } } if(check==false){ out.print("<h2 style='color:red'>No results for } //out.print("Error.......");

"+fName+"</h2>");

out.print("<br><a href='http://localhost:8080/Assignment6/generalaWebSet/EnterName.html'>Return to Search Page</a></body></html>");

} catch (ClassNotFoundException | SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); }

} }

Q6

SearchName.JAVA
package swingPkg; import java.awt.EventQueue; public class SearchName { private private private private private private private private private JFrame frame; JTextField textField; JTable table; String namePart; JLabel lblName ; Vector columnNames=new Vector(); DefaultTableModel model; Vector data=new Vector(); JLabel lblResult;

/** * Launch the application. */ public static void main(String[] args) { EventQueue.invokeLater(new Runnable() { public void run() { try { SearchName window = new SearchName(); window.frame.setVisible(true); } catch (Exception e) { e.printStackTrace(); } } }); } /** * Create the application. */ public SearchName() { initialize(); } /** * Initialize the contents of the frame. */ private void initialize() { frame = new JFrame(); frame.setBounds(100, 100, 450, 300); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.getContentPane().setLayout(null); lblName = new JLabel("Name :"); lblName.setBounds(24, 27, 70, 15); frame.getContentPane().add(lblName); textField = new JTextField(); textField.setBounds(91, 25, 324, 19); frame.getContentPane().add(textField); textField.setColumns(10);

JButton btnSearch = new JButton("Search"); btnSearch.setBounds(169, 56, 117, 25); frame.getContentPane().add(btnSearch); model=new DefaultTableModel(data,columnNames); table = new JTable(model); table.setBounds(12, 112, 411, 154); frame.getContentPane().add(table); lblResult = new JLabel("result"); lblResult.setBounds(12, 61, 144, 15); frame.getContentPane().add(lblResult); btnSearch.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent arg0) { } }); btnSearch.addMouseListener(new MouseAdapter() { @Override public void mouseClicked(MouseEvent e) { namePart=textField.getText(); columnNames.clear(); model.getDataVector().removeAllElements(); //lblName.setText(namePart); try { Class.forName("com.mysql.jdbc.Driver"); Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/friends","root","root") ; PreparedStatement statement = con.prepareStatement("select * from names where last like \"%"+namePart+"%\" or first like \"%"+namePart+"%\";"); ResultSet result = statement.executeQuery(); java.sql.ResultSetMetaData md = result.getMetaData(); int columns = md.getColumnCount(); for (int i = 1; i < columns; i++) { columnNames.addElement( md.getColumnName(i) ); } model.addColumn(columnNames); while (result.next()){ System.out.println(result.getString(1) +" "+ result.getString(2)); Vector row = new Vector(columns); for (int i = 1; i <= columns; i++){ row.addElement( result.getObject(i) ); } model.addRow(row);

} if(model.getRowCount()==0){ lblResult.setText("No Search Result....."); }else{ lblResult.setText("Search result found....."); } result.close(); statement.close();

} catch (Exception e1) { // TODO Auto-generated catch block e1.printStackTrace(); }

} }); } }

Q7
Quiz 01 -> Servlets & JSPs MVC & Sesion Tracking You need three JSPs: login.jsp [publicly available] o must contain the phrase Login Service and a button with name=submit o must contain two textFields: name=userid and name=passwd o when returning to the page after a bad login attempt, must display the word: again o o o getDate.jsp [in WEBINF] must display the users name somewhere on the page must contain the word Date requires a form button: with name=submit textfield: name=key

showResult.jsp [in WEBINF] o display the text corresponding to the key o form: button: name=more button: name=logout (if more, display getDate.jsp; if logout, remove session; display login.jsp Set up your servlet so that you support users logging in with userid and password. You must support a user with userid=0001 and password=0001 and name=your name You must not support a userid=0002 and 0000 Any other users and passwords are ok. login.jsp Captures login and password and passes to the servlet as parameters. If servlet sees that that there are incoming parameter values for userid and password, it means a new user is logging in. Servlet responsibility [when userid and passwd parameters exist] DO: invalidate any session information, before checking for valid user. If NOT valid user, redirect to login.jsp and include phrase: invalid user on the page somewhere If valid user: o store userid and username in session object. o redirect to getDate.jsp [under WEBINF], getDate.jsp o The getDate.jsp should display the users name somewhere on the page. The servlet will have stored the users name in the session object. o Get the key (date) from the user with form with textfield name=key and pass to the same servlet. Returning to Servlet from getDate.jsp Servlet will not find parameters userid and password. This means existing user Check session object for userid and username If not found, redirect to login.jsp If found in session object, lookup the key date and arrange for the corresponding phrase and the user name to be available to the showResponse.jsp Redirect to showResponse.jsp which should display the text associated with the date and also display the users name somewhere on the page. Track your users with a session object, so that if a user hits your servlet while their session is active, they do not see the login page but go directly to getDate.jsp

login.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Insert title here</title> <% //invalidate the Session object named session session.invalidate(); %> </head> <body> <!-- Sends the data to the servelet (this is a public JSP) --> <form method="post" action="http://localhost:8080/SessionTracker/Login"> User Name :<input type="text" name="uName"><br> PassWord :<input type="password" name="pwd"><br> <input type="submit" value="Submit"> </form> <% /* At the start up there is no any requested value from the servelet. So this is used to prevent displaying word 'null' at the startup */ if(request.getParameter("sentResult")!=null){ out.print(request.getParameter("sentResult")); } %> </body> </html>

LogIn.java
package sessionPkg; import java.io.IOException; import import import import import import import import javax.servlet.RequestDispatcher; javax.servlet.ServletContext; javax.servlet.ServletException; javax.servlet.annotation.WebServlet; javax.servlet.http.HttpServlet; javax.servlet.http.HttpServletRequest; javax.servlet.http.HttpServletResponse; javax.servlet.http.HttpSession;

import org.apache.catalina.Session; /** * Servlet implementation class Login */ @WebServlet("/Login") public class Login extends HttpServlet { private static final long serialVersionUID = 1L; protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { //redirect to the showResult.jsp in web-inf folder RequestDispatcher dispatcher=request.getRequestDispatcher("/WEBINF/showResult.jsp"); //At the time of redirection it forwards the sent parameter value from the getDate.jsp dispatcher.forward(request,response); } protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { try{ //creates a object using Session to store password and username HttpSession session=request.getSession(); String uName=request.getParameter("uName"); String pwd=request.getParameter("pwd"); if(uName==null){ uName=session.getAttribute("userName").toString(); pwd=session.getAttribute("password").toString(); System.out.print("I ma here"); } //save the value in variable uName in a session named userName session.setAttribute("userName", uName); //save the value in pwd in a session named password session.setAttribute("password", pwd);

if(session.getAttribute("userName").equals("002") || session.getAttribute("userName").equals("000")){ //if compiler is inside of this part, it redirect to the logIn.php with a parameter named sentResult with value invalid User response.sendRedirect("http://localhost:8080/SessionTracker/logIn.jsp? sentResult=Invalid User");

}else{ they have been stored //password & user name are checked from sessions where

if((session.getAttribute("userName").equals("001")) && (session.getAttribute("password").equals("001"))){ "Udara"); session.setAttribute("currentName",

/*getDate.php is included in WEB-INF in WebContent folder. We cannot access JSP which are saved there with response.sendRedirect(). * so we have to use getRequestDispatcher() * */ RequestDispatcher dispatcher=request.getRequestDispatcher("/WEB-INF/getDate.jsp"); dispatcher.forward(request,response);

//response.sendRedirect("http://localhost:80 80/SessionTracker/getDate.jsp"); }else{ session.setAttribute("currentName", "UnKnown User"); RequestDispatcher dispatcher=request.getRequestDispatcher("/WEB-INF/getDate.jsp"); dispatcher.forward(request,response); } } }catch (Exception ex) { System.out.print(ex.toString()); } } }

getDate.jsp (Located in WEB-INF folder folder in web content directory)


<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Insert title here</title> </head> <body> <% //Reads the session named currentName and prints its value out.print("WelCome..."+session.getAttribute("currentName")); %> <!-- Data are sent to the doGet method in the servelet named LogIn.java --> <form method="get" action="http://localhost:8080/SessionTracker/Login"> <input type="text" name="keyDate"> <input type="submit"> </form> </body> </html>

showResult.jsp (Located in WEB-INF folder folder in web content directory)


<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Insert title here</title> </head> <body> <% //Grabs the sent parameter values from the servelet String keyDate=request.getParameter("keyDate"); String Event=""; if(keyDate.equals("Jan 23")){ Event="Ian Bob's Birth day"; }else if(keyDate.equals("Feb 23")){ Event="Ian Bob's death"; } %> <h2>Event Is <%=Event %></h2> <!-- To logon to the getDate.jsp we again call to the doPost in the LogIn.java servelet Since we have stored the login details in session we no need to log via login.jsp --> <form action="http://localhost:8080/SessionTracker/Login" method="post"> <input type="submit" value="More"> </form> <!-- Link to log to the login.jsp --> <a href="http://localhost:8080/SessionTracker/logIn.jsp"><input type="button" value="Log Out"></a> </body> </html>

Q8

EnterBMI.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Insert title here</title> </head> <body> <form action="http://localhost:8080/BMI/JSPSet/EnterBMI.jsp" method="get"> <input type="text" name="weight"> <input type="text" name="height"> <input type="submit" value="Calculate BMI"> </form> <% try{ double weight=Double.parseDouble(request.getParameter("weight")); double height=Double.parseDouble(request.getParameter("height")); double BMI; BMI=weight/(height*height); String BMICat=""; if(BMI<=18.4){ BMICat="UnderWeight"; } if(BMI>=18.5 && BMI<=24.9){ BMICat="Normal Weight"; } if(BMI>=25 && BMI<=29.9){ BMICat="Over Weight"; } if(BMI>=30){ BMICat="Obesity"; } response.sendRedirect("http://localhost:8080/BMI/JSPSet/BmiResult.jsp? BMIValue="+BMICat); }catch(Exception ex){} %> </body> </html>

BMIResult.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Insert title here</title> </head> <body> <% String result=request.getParameter("BMIValue"); %> <h1><%=result %></h1> </body> </html>

Q9-

package velocityPkg; public class CalVelocity { /** * @param args */ public static void main(String[] args) { /*store the values which is taken from command line argument * Command line arguments are given at the run time. * when we compile this program through CMD, we type 'javac fileName.java' * after a successful compilation we run our file * 'java fileName' instead of this to give command line arguments use * eg* 'java fileName argument1,argument2' * * */ //stores data that is given as command line arguments double time=Double.parseDouble(args[0]); double initialVelocity=Double.parseDouble(args[1]); double finalVelocity=Double.parseDouble(args[2]); double mass=Double.parseDouble(args[3]);

double accelaration=(finalVelocity-initialVelocity)/time; double force=mass*accelaration; System.out.println("..............................................."); System.out.println("..............................................."); System.out.println("Initial velocity = "+initialVelocity+" m/s"); System.out.println("Final velocity = "+finalVelocity+" m/s"); System.out.println("Time = "+time+" s"); System.out.println("Accelaration = "+accelaration+" m/s*s"); System.out.println("Mass = "+mass+" Kg"); System.out.println("Force acting on the particle = "+force+" N"); System.out.println("..............................................."); System.out.println("..............................................."); } }

build.xml
<project name="Ant-Test" default="main" basedir="."> <!-- Sets variables which can later be used. --> <!-- The value of a property is accessed via ${} --> <property name="src.dir" location="src" /> <property name="build.dir" location="build" /> <property name="dist.dir" location="dist" /> <property name="docs.dir" location="docs" /> <!--To create the lib folder in the project --> <property name="lib.dir" location="lib" /> <!-- Deletes the existing build, docs and dist directory--> <target name="clean"> <delete dir="${build.dir}" /> <delete dir="${docs.dir}" /> <delete dir="${dist.dir}" /> <delete dir="${lib.dir}" /> </target> <!-- Creates the build, docs and dist directory--> <target name="makedir"> <mkdir dir="${build.dir}" /> <mkdir dir="${docs.dir}" /> <mkdir dir="${dist.dir}" /> <!-- To create the lib folder in the project --> <mkdir dir="${lib.dir}" /> </target> <!-- Compiles the java code (including the usage of library for JUnit --> <target name="compile" depends="clean, makedir"> <javac srcdir="${src.dir}" destdir="${build.dir}"> </javac> </target> <!-- Creates Javadoc --> <target name="docs" depends="compile"> <javadoc packagenames="src" sourcepath="${src.dir}" destdir="$ {docs.dir}"> <!-- Define which files / directory should get included, we include all --> <fileset dir="${src.dir}"> <include name="**" /> </fileset> </javadoc> </target> <!--Creates the deployable jar file --> <target name="jar" depends="compile"> <!-- jar file will be copy to dist.dir --> <jar destfile="${dist.dir}\ff.jar" basedir="${build.dir}"> <manifest> <!-- To create the deployable jar file we have indicate the name of the class where ou main method is included --> <attribute name="Main-Class" value="CalVelocity.Main" /> </manifest> </jar> <!-- jar file will be copy to lib.dir --> <jar destfile="${lib.dir}\ff.jar" basedir="${build.dir}"> <manifest> <attribute name="Main-Class" value="CalVelocity.Main" /> </manifest> </jar>

</target> <target name="main" depends="compile, jar, docs"> <description>Main target</description> </target> </project> (Learn how to create the build.xml file. After creating the file we can copy above details into it and do the changes requird in the file and right click on it and go to 'run as' then choose 'Ant Build' )

You might also like