Create a Servlet:

Make sure WPT plugin is installed:

1) Go to WPT and download and install it.

Create a Web Project

1) Go to File -> New -> Other -> Web select Dynamic Web Project


2) Name the web project MyTestProject click Finish


Create the servlet:

1) Select MyTestProject

2) Go to File -> New -> Other -> Web select Servlet


3) Name the Servlet MyTest


4) Right click src folder select new package and name it loan

5) Right click on the package and add a class named Loan.java

Edit the code:

1) Edit the Loan.java file:

package loan;

public class Loan {
  private double annualInterestRate;
  private int numberOfYears;
  private double loanAmount;
  private java.util.Date loanDate;

  /** Default constructor */
  public Loan() {
	this(2.5, 1, 1000);
  }

  /** Construct a loan with specified annual interest rate,
	  number of years and loan amount
	*/
  public Loan(double annualInterestRate, int numberOfYears, double loanAmount) {
	this.annualInterestRate = annualInterestRate;
	this.numberOfYears = numberOfYears;
	this.loanAmount = loanAmount;
	loanDate = new java.util.Date();
  }

  /** Return annualInterestRate */
  public double getAnnualInterestRate() {
	return annualInterestRate;
  }

  /** Set a new annualInterestRate */
  public void setAnnualInterestRate(double annualInterestRate) {
	this.annualInterestRate = annualInterestRate;
  }

  /** Return numberOfYears */
  public int getNumberOfYears() {
	return numberOfYears;
  }

  /** Set a new numberOfYears */
  public void setNumberOfYears(int numberOfYears) {
	this.numberOfYears = numberOfYears;
  }

  /** Return loanAmount */
  public double getLoanAmount() {
	return loanAmount;
  }

  /** Set a newloanAmount */
  public void setLoanAmount(double loanAmount) {
	this.loanAmount = loanAmount;
  }

  /** Find monthly payment */
  public double getMonthlyPayment() {
	double monthlyInterestRate = annualInterestRate / 1200;
	double monthlyPayment = loanAmount * monthlyInterestRate / (1 -
	  (Math.pow(1 / (1 + monthlyInterestRate), numberOfYears * 12)));
	return monthlyPayment;    
  }

  /** Find total payment */
  public double getTotalPayment() {
	double totalPayment = getMonthlyPayment() * numberOfYears * 12;
	return totalPayment;    
  }

  /** Return loan date */
  public java.util.Date getLoanDate() {
	return loanDate;
  }
}

Edit the MyTest.java file:

package com.franksthinktank;

import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import loan.Loan;

/**
 * Servlet implementation class LoanServlet
 */
@WebServlet("/MyTest")
public class MyTest extends HttpServlet {
  private static final long serialVersionUID = 1L;
  
  /**
   * @see HttpServlet#HttpServlet()
   */
  public MyTest() {
    super();
  }

  /**
   * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
   */
  protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, java.io.IOException {
    response.setContentType("text/html");
    PrintWriter out = response.getWriter();

    // output your page here
    out.println("<html>");
    out.println("<head>");
    out.println("<title>Loan Calculator</title>");
    out.println("</head>");
    out.println("<body>");
    out.println("<h3>Compute Loan Payment</h3><br>");
    out.println("<form action=\"MyTest\" method=\"post\"> ");    
    out.println("<p><label>Amount </label><input type =\"text\" name = \"loanAmount\"size = \"20\" /></p>");
    out.println("<p><label>Annual Interest Rate </label><input type =\"text\" name = \"loanInterest\"size = \"20\" /></p>");
    out.println("<p><label>Number of Years </label><input type =\"text\" name = \"loanYears\"size = \"20\" /></p></br>");
    out.println("<p><input type=\"submit\" value=\"Calculate Loan Payment\" />" +
				"<input type=\"reset\" value=\"Reset\" /></p>");  
				
    out.println("</form>");    		 
    out.println("</body>");
    out.println("</html>");
    out.close();
  }

  /**
   * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
   */
  protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    response.setContentType("text/html");
    PrintWriter out = response.getWriter();
    //get the data
    double loanAmount = Double.parseDouble(request.getParameter("loanAmount"));
    double annualInterestRate = Double.parseDouble(request.getParameter("loanInterest"));
    int numberOfYears = Integer.parseInt(request.getParameter("loanYears"));
    Loan loan = new Loan(annualInterestRate, numberOfYears, loanAmount);
    double monthlyPayment = loan.getMonthlyPayment();
    double totalPayment = loan.getTotalPayment();

    // output your page here
    out.println("<html>");
    out.println("<head>");
    out.println("<title>Loan Calculator</title>");
    out.println("</head>");
    out.println("<body>"); 
    out.println("</br></br><p>Loan Amount: "+loanAmount+"</p>");
    out.println("<p>Annual Interest Rate: "+annualInterestRate+" </p>");
    out.println("<p>Number of Years: "+numberOfYears+"</p>");
    out.println("<p>Monthly Payment: "+monthlyPayment+"</p>");
    out.println("<p>Total Payment: "+totalPayment+"</p>");		 
    out.println("</body>");
    out.println("</html>");
    out.close();
  }
}

Run the servlet!

1) Select MyTest.java, click on the Run drop down menu, and select Run As -> Run On Server


2) Test the program to make sure it was deployed correctly and you're done!