0

我对 JSP 和 servlet 完全陌生,所以这个问题可能真的很不寻常或很容易解决!我正在尝试获取id=creditcardid=expirationDate输入字段的值!在 serverlet 的函数中检查字段是否与数据库中的数据匹配,如下所示:

servlet 中的 HTML:

    out.println("<html>");
    out.println("<head>");
    out.println("<title>Make payment</title>");
    out.println("<script type='text/javascript' src='js/jquery-1.5.2.min.js'></script>");
    out.println("<script type='text/javascript' src='js/payment.js'></script>");
    out.println("<link type='text/css' href='css/style.css' rel='Stylesheet' />");
    out.println("</head>");
    out.println("<body>");
    out.println("<div class='bg-light' style='width: 200px; height: 200px; position: absolute; left:50%; top:50%;  margin:-100px 0 0 -100px; padding-top: 40px; padding-left: 10px;'>");
    out.println("<input id='reservationID' style='display: none' value='"+rb.reservationID+"' />");
    out.println("<div>Credit Card Number : </div>");
    out.println("<div><input id='creditcard' onKeyPress='return checkIt(event);' type='text' name='creditcard' maxlength='16' /></div>");
    out.println("<div>ExpirationDate : </div>");
    out.println("<div><input id='expirationDate' type='text' onKeyPress='return checkIt(event);' name='expirationDate' maxlength='4' /></div>");
    out.println("<span style='font-size: 75%;'>"+Error+"</span>");
    out.println("<div><input type='button'  name='buttonsave' value='Make Payment' onclick='makePayment("+rb.reservationID+");' /></div>");
    out.println("<div><input type='button'  name='buttoncancel' value='Cancel Payment' onclick='cancelPayment("+rb.reservationID+");' /></div>");
    out.println("</div>");
    out.println("</body>");
    out.println("</html>");

我正在使用 servlet 中的函数来检查输入并在错误时显示错误out.println("<span style='font-size: 75%;'>"+Error+"</span>")

小服务程序功能:

String Error= "";
bolean check = us.checkCC(userID, creditno, expiration); // i need the values here!
....

提前致谢!

4

1 回答 1

1

boolean check = us.checkCC(userID, creditno, expiration); // 我需要这里的值!

request您可以通过从传递给适当方法的对象中提取这些值来获取这些值。

如果您在此时提交表单,post则 servlet 中的 doPost 代码应如下所示:

public class NewClass extends HttpServlet {

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String creditno = req.getParameter("creditcard");       //name of the input field, not id
        String expiration = req.getParameter("expirationDate");     //name of the input field should be expirationDate
        //...  Other code follows here
    }
}
于 2013-11-10T06:43:06.463 回答