Poorya
Poorya

Reputation: 1320

get value from a textbox in HTML part of Servlet

I'm totally new to JSP and servlet so this question might be really unusual or easy to solve! i'm trying to get the value of the id=creditcard and id=expirationDate input fields! in a function in servelet to check whether the fields matches the data in database as follow:

HTML within servlet:

    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>");

and i'm using a function in servlet to check the input and display error in out.println("<span style='font-size: 75%;'>"+Error+"</span>") if its wrong.

servlet function:

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

Thanks in advance!

Upvotes: 0

Views: 7903

Answers (1)

coding_idiot
coding_idiot

Reputation: 13714

boolean check = us.checkCC(userID, creditno, expiration); // i need the values here!

You can get the values, by extracting those from request object being passed to the appropriate method.

If you're submitting the form by post then the doPost code within a servlet shall look something like this :

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
    }
}

Upvotes: 1

Related Questions