【问题标题】:get value from a textbox in HTML part of Servlet从 Servlet 的 HTML 部分中的文本框获取值
【发布时间】:2013-11-10 05:30:58
【问题描述】:

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

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("&lt;span style='font-size: 75%;'&gt;"+Error+"&lt;/span&gt;") 中显示错误(如果错误)。

servlet 函数:

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

提前致谢!

【问题讨论】:

  • 您可以在 Servlet 中获取这些值,例如 request.getParameter("creditcard");request.getParameter("expirationDate");。 P.S:这些输入字段需要名称属性creditcardexpirationDateid 属性被 JavaScript 引用。
  • 在使用按钮类型button - type="button" 时是否使用 AJAX?否则,类型应该是提交。
  • 是的,我在 payment.js 中使用 Ajax Post 方法

标签: java jsp servlets


【解决方案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
    }
}

【讨论】:

  • 你提到,你正在使用 ajax post。
  • 我的意思是,我没有使用任何&lt;form&gt;。是的,我在我的 java 脚本文件中使用了 ajax post。我按照您向doPost 的建议添加了代码!如果我想使用这些值来检查数据库中的日期,我应该在哪里添加bolean check = us.checkCC(userID, creditno, expiration);?在 doPost 中或使用creditnoexpiration 的全局变量?
  • 当然在 doPost 内部。 Other code follows here - 在这里写下你的代码。
  • 非常感谢!这有很大帮助!我想知道有没有办法在 javascript 中访问 bolean check 值?我试过sessionattributem 但似乎无法直接从 JS 访问它。有没有办法在 doPOST 中向 DOM 添加隐藏属性?再次感谢!
  • 因为它超出了问题的范围,所以我创建了另一个问题! the link
猜你喜欢
  • 2020-09-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-09-02
  • 2012-05-26
  • 1970-01-01
  • 2019-04-10
  • 2011-04-07
相关资源
最近更新 更多