-1

我创建了这个用作货币转换的 servlet。它可以转换三种货币(SAR、USD、Euro)。我有访问表,其中包含要读取转换值的字段。如何将此 servlet 转换为 JSP?..

 public class cur_exchange extends HttpServlet {
    String from = "";
    String to = "";
    double amount = 0.0;
    String message = "";
    double total = 0.0;
    @Override
    public void init() {
    try {
      Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
      System.out.println("JDBC driver loaded");
    }
    catch (ClassNotFoundException e) {
      System.out.println(e.toString());
    }
  }
    @Override
    public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        display_form(false, request, response, false);
  }
    @Override
    public void doPost(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {

        from = request.getParameter("from");
        to = request.getParameter("to");
        boolean error = false;
        try
        {
            amount = Double.parseDouble(request.getParameter("amount").toString());
        }
        catch (Exception e) {
                message = "Error: Please Type Valid Number To Convert";
                error = true;
            }
        if(from.equals(to))
        {
            total = amount;
            display_form(true, request, response, false);
        }
        else
        {
            try {
                  Connection con = DriverManager.getConnection("jdbc:odbc:MS Access Database");
                  System.out.println("got connection");
                  Statement s = con.createStatement();
                  String sql = "SELECT RATE FROM EXCHANGE_RATE WHERE FROM_CUR='"+from+"' AND TO_CUR='"+to+"'";
                  ResultSet rs = s.executeQuery(sql);
                  if (rs.next()) {
                      total = amount * Double.parseDouble(rs.getString(1));
                  }
                  else
                  {
                      s = con.createStatement();
                      sql = "SELECT RATE FROM EXCHANGE_RATE WHERE FROM_CUR='"+to+"' AND TO_CUR='"+from+"'";
                      rs = s.executeQuery(sql);
                      if (rs.next()) 
                      {
                            total = amount / Double.parseDouble(rs.getString(1));
                      }
                  }
            }
            catch (SQLException e) {
                message = "Error." + e.toString();
                error = true;
            }
            catch (Exception e) {
                message = "Error." + e.toString();
                error = true;
            }
        rs.close();
        s.close();
            con.close();

            if (error)
                display_form(true , request, response, true);
            else
                display_form(true, request, response, false);
        }
    }

    public void display_form(boolean populated, HttpServletRequest request, HttpServletResponse response, boolean error) throws ServletException, IOException {

        PrintWriter out = response.getWriter();
        out.println("<html>");
        out.println("<head>");
        out.println("</head>");
        out.println("<body>");
        out.println("<CENTER>");
        if (error)
            out.println("<h3 style='color:red'>"+message+"</h3><br /><br />");
        out.println("<h1 style='text-decoration:underline'>Currency Converter</h1>");
        out.println("<form action='cur_exchange.java' method='post'>");
        out.println("<br /><br /><label>From:");
        out.println("<select name='from'>");
        if (populated)
        {
            if (from.equals("SAR"))
            {
                out.println("<option selected='selected'>SAR</option>");
            }
            else
            {
                out.println("<option>SAR</option>");
            }
            if (from.equals("$"))
            {
                out.println("<option selected='selected'>$</option>");
            }
            else
            {
                out.println("<option>$</option>");
            }
            if (from.equals("EURO"))
            {
                out.println("<option selected='selected'>EURO</option>");
            }
            else
            {
                out.println("<option>EURO</option>");
            }
        }
        else
        {
            out.println("<option>SAR</option>");
            out.println("<option>$</option>");
            out.println("<option>EURO</option>");
        }
        out.println("</select></label>");
        out.println("<br /><br /><label>To:");
        out.println("<select name='to'>");
        if (populated)
        {
            if (to.equals("SAR"))
            {
                out.println("<option selected='selected'>SAR</option>");
            }
            else
            {
                out.println("<option>SAR</option>");
            }
            if (to.equals("$"))
            {
                out.println("<option selected='selected'>$</option>");
            }
            else
            {
                out.println("<option>$</option>");
            }
            if (to.equals("EURO"))
            {
                out.println("<option selected='selected'>EURO</option>");
            }
            else
            {
                out.println("<option>EURO</option>");
            }
        }
        else
        {
            out.println("<option>SAR</option>");
            out.println("<option>$</option>");
            out.println("<option>EURO</option>");
        }
        out.println("</select></label><br />");
        if(populated)
        {
            out.println("<br /><label>Amount: <input name='amount' type='text' size='25' maxlength='30' id='amount' value='"+amount+"' /></label><br />");
        }
        else
        {
            out.println("<br /><label>Amount: <input name='amount' type='text' size='25' maxlength='30' id='amount' /></label><br />");
        }
        if(populated)
        {
            out.println("<br /><label>Result: <input name='result' type='text' size='25' maxlength='30' id='result' disabled='disabled' value='"+total+"'/></label><br />");
        }
        else
        {
            out.println("<br /><label>Result: <input name='result' type='text' size='25' maxlength='30' id='result' disabled='disabled' /></label><br />");
        }
        out.println("<br /><input type='submit' value='Convert'/>");
        out.println("</form>");
        out.println("</CENTER>");
        out.println("</body>");
        out.println("</html>");
    }
}
4

1 回答 1

1
  1. 阅读这些教程之一
  2. 重写为 JSP 文件。
于 2012-04-27T22:03:50.783 回答