2016-04-18 13 views
0

Первый JSP, currencyConversion.jspКак пройти вход С 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, но это обыкновение позвольте мне передать сумму в него укажите другой тип данных.

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

Как передать валюту и сумму от currencyConversion.jsp в двойной calculateRate (String валюту, внутр количество) в processCurrency.jsp?

ответ

0

То, что вы получаете, является значением String, преобразует его в int.

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

чем разница между <%! %> и <% %>? – Ghost

+0

как я могу передать сумму и валюту в double calculateRate (String currency, int amount) {@ 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);} 


    %>