2016-04-18 8 views
0

まずJSPにJSPからcurrencyConversion.jspどのように、他のJSP

<%@page contentType="text/html" pageEncoding="UTF-8"%> 
<!DOCTYPE html> 
<html> 
<head> 
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 
    <title>Currency Conversion</title> 
    <style> 
label{ display: inline-block; 
    width: 140px; 
    text-align: left; 
    padding-top:10px;} 
    </style> 
</head> 
<body> 
    <h1>Use JSP Declaration tag, JSP Scriplet and JSP Expression 
    in application</h1> 
    <font style="color:plum; font-family:verdana;"><b> 
     Currency Conversion</b></font> 
     <form id="currency" action="processCurrency.jsp" method="get"> 
      <label for="amount">Amount (in RM)</label> 
      <input name="amount" id="amount"></br> 
      <label for = "currency">Convert to</label> 
    <select name="currency" id = "currency"><br/> 
     <option value = "1">USD</option> 
     <option value = "2">Pound Sterling</option> 
        <option value = "3">Euro</option> 
    </select> 
    <br /> 
      <br /> 
      <input type = "submit" id = "btnSubmit" value="Submit"/> 
    <input type = "reset" id = "btnReset" value = "Reset"/> 
     </form> 

</body> 
</html> 

第二JSP、processCurrency.jsp

<%@page contentType="text/html" pageEncoding="UTF-8"%> 
    <!DOCTYPE html> 
    <html> 
    <head> 
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 
    <title>Currency process</title> 
    </head> 
    <body> 
    <% 
     String currency=request.getParameter("currency"); 
     int amount=request.getParameter("amount"); 
    %> 
    <%! 
    final double USD=3.92; 
    final double STG=5.96; 
    final double EURO=4.47; 
    double calculateRate(String currency, int amount) 
    { 
     double currencyChange=0.00f; 
     if(currency.equals("1")) 
     currencyChange=(double)(amount*USD); 
     if(currency.equals("2")) 
      currencyChange=(double)(amount*STG); 
     if(currency.equals("3")) 
      currencyChange=(double)(amount*EURO); 
     return currencyChange; 
    } 
    %> 
</body> 
</html> 

を入力を渡すために、私持ってJSP:paramを使用してみてください、それは文句を言わない私はそれとして量を渡してみましょうその異なるデータ型を述べる。 processCurrency.jspでダブルcalculateRate(文字列の通貨、int型の量)にcurrencyConversion.jspから通貨と金額を渡す方法

<%int amount=request.getParameter("amount");%> 

答えて

0

あなたが得るものは、String値でintに変換します。

int amount = Integer.valueOf(request.getParameter("amount")); 
+0

何<%! %>と<% %>の違いは? – Ghost

+0

どのようにしてamountとcurrencyをdouble calculateRate(文字列通貨、int量)に渡すことができますか?{@ tak3shi – Ghost

0
<% 
     String currency=request.getParameter("currency"); 
     double amount=Double.valueOf(request.getParameter("amount")); 
     out.println("MYR "+amount+" to"); 

    final double USD=3.92; 
    final double STG=5.96; 
    final double EURO=4.47; 

     double currencyChange=0.00f; 
     if(currency.equals("1")){ 
     currencyChange=(double)(amount*USD); 
     out.println("USD "+currencyChange);} 
     else if(currency.equals("2")){ 
      currencyChange=(double)(amount*STG); 
     out.println("Sterling Pound "+currencyChange);} 
     else if(currency.equals("3")){ 
      currencyChange=(double)(amount*EURO); 
     out.println("EURO "+currencyChange);} 


    %> 
関連する問題