如何将输入从JSP传递到其他JSP

时间:2016-04-18 11:11:24

标签: java jsp servlets scriptlet

第一个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>

processCurrency.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 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");%> ,但它不会让我传递数量,因为它声明了不同的数据类型。

SiteMap1.SiteMapProvider = "RouteBaseSitemapProvider1";

如何将货币和金额从currencyConversion.jsp传递到processCurrency.jsp中的double calculateRate(String currency,int amount)?

2 个答案:

答案 0 :(得分:0)

你得到的是一个String值,将其转换为int。

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

答案 1 :(得分: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);}


    %>