question archive Your assignment is to implement a simple web service which lets users convert an amount from one currency to another, for example from USD to EUR

Your assignment is to implement a simple web service which lets users convert an amount from one currency to another, for example from USD to EUR

Subject:Computer SciencePrice:2.87 Bought7

Your assignment is to implement a simple web service which lets users convert an amount from one currency to another, for example from USD to EUR. The interface should consist of two text fields, “Amount” and “Rate,” and a submit button, “Convert!” If you want to convert an amount from one currency to another, we input the amount in the “Amount” text field and press the “Convert!” button. The service should reply with a new web page, which displays the result of the conversion.

OPTIONAL FEATURES: The service should remember which exchange rate is being used for conversion, initially 1.0. If the user wants to change the exchange rate he inputs a new rate in the “Rate” text field and the amount to be converted in the “Amount” field. The new rate should be remembered for future conversion when the “Rate” field is left empty.

  1. Write your servlet in a file named Converter.java.
  2. You should implement the doGet method OR the doPost method.
  3. In this method, write code, which produces an HTML-page. Let the “action” attribute of the <form> tag have the value “/converter/servlet/Converter”, pointing out the servlet you are implementing. Let the “method” attribute have the value “post”.
  4. In your method, write code, which retrieves the values, entered into the text fields of the HTML form. Process the information accordingly and produce a suitable HTML page as a reply.
  5. Use an attribute in the Converter class to store the current exchange rate.
  6. Compile and test your servlet!

pur-new-sol

Purchase A New Answer

Custom new solution created by our subject matter experts

GET A QUOTE

Answer Preview

Answer

Converter.html:
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Currency Converter</title>
</head>
<body>
<form name="converterForm" method="post" action="Converter">
Amount: <input type="text" name="amount"/>
Rate: <input type="text" name="rate"/>
<input type="submit" value="Submit"/>
<input type="submit" value="Convert" formmethod="get"/>
</form>
</body>
</html>


Converter.java:
import java.io.*;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.*;
@WebServlet("/Converter")
public class Converter extends HttpServlet {
private static final long serialVersionUID = 1L;
private double rate = 1.0; // Rate value
private double value = 0.0; // Converted amount value
public Converter() {
super();
}
Protected void doGet(HttpServletRequest demand, HttpServletResponse reaction) tosses ServletException, IOException {
response.setContentType("text/html");
PrintWriter out = response.getWriter();
String htmlRespone = "<html>";
htmlRespone += "<h2>Amount Value: " + esteem+ "<br/>";
htmlRespone += "</html>

OUTPUT:

PFA

Related Questions