I'm pretty new with all these things but hope that you guys can help me understand how does it work. I got a form with field . How do i get data from client back? Was looking for some information but couldnt find.
<form action="/login" method="POST">
    <fieldset>
        <legend>MemberId</legend>
        <div class="form-group">
            <div class="row colbox">
                <div class="col-lg-4 col-sm-4">
                    <label for="txt_memberid" class="control-label">MemberId</label>
                </div>
                <div class="col-lg-8 col-sm-8">
                    <input class="form-control" id="txt_memberid" name="txt_memberid" placeholder="MemberId" type="text" value="" />
                    <span class="text-danger"></span>
                </div>
            </div>
        </div>
        <legend>Login</legend>
        <div class="form-group">
            <div class="row colbox">
                <div class="col-lg-4 col-sm-4">
                    <label for="txt_username" class="control-label">Username</label>
                </div>
                <div class="col-lg-8 col-sm-8">
                    <input class="form-control" id="txt_username" name="txt_username" placeholder="Username" type="text" value="" />
                    <span class="text-danger"></span>
                </div>
            </div>
        </div>
        <div class="form-group">
            <div class="row colbox">
                <div class="col-lg-4 col-sm-4">
                    <label for="txt_password" class="control-label">Password</label>
                </div>
                <div class="col-lg-8 col-sm-8">
                    <input class="form-control" id="txt_password" name="txt_password" placeholder="Password" type="password" value="" />
                    <span class="text-danger"></span>
                </div>
            </div>
        </div>
        <div class="form-group">
            <div class="col-lg-12 col-sm-12 text-center">
                <input id="btn_login" name="btn_login" type="submit" class="btn btn-default" value="Login" />
                <input id="btn_cancel" name="btn_cancel" type="reset" class="btn btn-default" value="Cancel" />
            </div>
        </div>
    </fieldset>
    get("/login", (request, response) -> {
        ConsoleLog.consoleLog(log, request);
        HashMap model = new HashMap();
        model.put("content", new VelocityTemplateEngine().render(new ModelAndView(new HashMap(),"templates/form_login.vtl")));
        return new ModelAndView(model, "templates/base.vtl");
    }, new VelocityTemplateEngine());
    post("/login", (request, response) -> {
        ConsoleLog.consoleLog(log, request);
        Integer memberId = -1;
        String username = "";
        String password = "";
        try {
            username = request.queryParams("txt_username");
            password = request.queryParams("txt_password");
            memberId = new Integer(request.queryParams("txt_memberid"));
        } catch (Exception e) {}
        log.info("MemberId="+memberId+" UserName="+username+" Password="+password);
        if (new MemberDAO().isAuth(new Auth(username,password,memberId))) {
            request.session(true);
            request.session().attribute("txt_memberid", memberId);
            request.session().attribute("txt_username", username);
        }
        return "process";
    });
If I send form (method=get) - all works fine request.queryParams(XXXX) If I send form (method=post) - in request.queryParams - nothing
Maybe anyone know what I should do in POST request?
There could be a number of things wrong. There could be an error in the Java code, HTML markup or anything else. Without knowing any other information, your best bet is to try a trial and error methodology to debug what's going wrong.
However, before trying that, the first thing you could do is to print out exactly what the post of the form is sending you:
post("/login", (request, response) -> {
    System.out.println(request.body());
    ...
If it prints out nothing, then the client is sending you nothing, so therefore the problem is in the view.
If it prints out a string, for example txt_username=john&txt_password=pass, then the client is sending you data so all you really need to do is extract the data from it using the request object's methods provided by Spark. Therefore, to debug, use the trial and error methodology as mentioned:
post("/login", (request, response) -> {
    System.out.println(request.contentType()); // What type of data am I sending?
    System.out.println(request.params()); // What are the params sent?
    System.out.println(request.raw()); // What's the raw data sent?
    // etc ...
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With