mercredi 10 juillet 2019

connect servlet and java class

i have exchange web application and i need to get two values of currency from my jsp page, transfer them to my main class(CurrencyExchangeLoader) do some actions and return them to my servlet(CurrencyExchangeServlet) and show them on my jsp page

my servlet

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

@WebServlet("/ExchangeCurrency")
public class CurrencyExchangeServlet extends HttpServlet {
    private static String fromCurrency = null;
    private static String toCurrency = null;
    private static Double amount = null;
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        Double totalAmount = null;
        fromCurrency = req.getParameter("fromCurrency");
        toCurrency = req.getParameter("toCurrency");
        amount = Double.parseDouble(req.getParameter("amount"));
        req.setAttribute("fromCurrencyToPost", fromCurrency);
        req.setAttribute("toCurrencyToPost", toCurrency);
        req.setAttribute("amountToPost", totalAmount);
        req.getRequestDispatcher("/result.jsp").forward(req, resp);
    }
}

and i need to transfer fromCurrency, toCurrency and amount to that class

import java.io.IOException;
import java.sql.SQLException;
import java.text.DecimalFormat;

public class CurrencyExchangeLoader {
    private static final String url = "jdbc:mysql://127.0.0.1:3306/currencies_spots?useUnicode=true&characterEncoding=UTF-8&zeroDateTimeBehavior=CONVERT_TO_NULL&serverTimezone=GMT";
    private static final String user = "gambit";
    private static final String password = "qwerty";
    private static final String driver = "com.mysql.cj.jdbc.Driver";

    public static void main(String[] args) throws IOException, ClassNotFoundException, SQLException, InstantiationException, IllegalAccessException {
        DatabaseService databaseService = new DatabaseService(url, user, password, driver);
        Loader loader = new Loader();
        loader.download();
        loader.extract();
        databaseService.writeToDB(loader.writeToArray());
        System.out.println(new DecimalFormat("#0.00").format(databaseService.converter(loader.writeToArray(), "USD", "JPY", 1000.0)));
    }
}




Aucun commentaire:

Enregistrer un commentaire