How to get the data from a "post" form?

1,371 views
Skip to first unread message

Yao

unread,
Jan 17, 2013, 6:18:03 PM1/17/13
to ve...@googlegroups.com
here is the html:

<!DOCTYPE html>
<html><head><title>Login Page</title></head>
<body>
    <form id='login' action='/login' method='post'>
        <fieldset >
            <legend>Login</legend>
            <input type='hidden' name='submitted' id='submitted' value='1'/>

            <label for='username' >UserName*:</label>
            <input type='text' name='username' id='username'  maxlength="50" />

            <label for='password' >Password*:</label>
            <input type='password' name='password' id='password' maxlength="50" />

            <input type='submit' name='Submit' value='Submit' />
        </fieldset>
    </form>
</body>
</html>

here is the java code:

public class Main extends Verticle
{
    @Override
    public void start() throws Exception
    {
        System.out.println("starting the vertx stuff");
        final String host = "localhost";
        final String port = "8181";

        Vertx vertx = Vertx.newVertx();
        HttpServer httpServer = vertx.createHttpServer();
...
        final String login = "/login";
...       
       
        httpServer.requestHandler(new Handler<HttpServerRequest>()
        {
            public void handle(HttpServerRequest req)
            {
                String path = req.path;

                /* start mapping of page urls*/
                // redirect user to the login page
...
                /* start mapping of form urls */
                // login
                else if (path.equals(login))
                {
                    mainLogin();
                    String username = req.params().get("username");
                    String password = req.params().get("password");
                    req.response.end("user=" + username + " password=" + password);
                }
...
        System.out.println("vertx listening to: " + host + " " + port);
        httpServer.listen(Integer.valueOf(port), host);
    }

Every time i enter username/password, they both come up as null. what am i doing wrong?

Yao

unread,
Jan 17, 2013, 6:58:56 PM1/17/13
to ve...@googlegroups.com

Tim Fox

unread,
Jan 18, 2013, 3:10:39 AM1/18/13
to ve...@googlegroups.com
It's a common misconception (mainly because other web frameworks blur the distinction) that form attributes somehow magically map to parameters of the request. This is not true when multipart form encoding is used.

Take a look at the formupload module for how to do this.
--
 
 

Yao

unread,
Jan 18, 2013, 9:30:35 AM1/18/13
to ve...@googlegroups.com
Hi Tim,

I managed to find some snippets online and I could get the POST form parameters using this:

req.bodyHandler(new Handler<Buffer>()
        {
            @Override
            public void handle(Buffer buff)
            {
                String contentType = req.headers().get("Content-Type");
                if ("application/x-www-form-urlencoded".equals(contentType))
                {
                    QueryStringDecoder qsd = new QueryStringDecoder(buff.toString(), false);
                    Map<String, List<String>> params = qsd.getParameters();
                    System.out.println("inside getFormData() --> " + params);
                }
            }
        });

The problem I am stuck now at is that the handle function "void". I have several forms that requires these params to be returned, any advice?
Usually use Spring, first time with Vertx.

Tim Fox

unread,
Jan 18, 2013, 10:17:40 AM1/18/13
to ve...@googlegroups.com
I definitely recommend using mod-formupload - it will handle this for you.
--
 
 

Yao Jiang

unread,
Jan 18, 2013, 10:29:28 AM1/18/13
to ve...@googlegroups.com
alright, i will try that out. thanks!
> --
>
>

Paulo Lopes

unread,
Jan 24, 2013, 3:42:20 AM1/24/13
to ve...@googlegroups.com
Hi,

I was playing with mod-formupload and it does not integrate "as good as it could" with Groovy, I would like to contribute to it and allow the usage of the groovy Closures instead of Handlers and use the Groovy Vertx and HttpRequest objects.

My question is, do you have any guidelines for this? Should i create a separate package for it and extend the original classes or can i just extend the mod classes to have matching method signatures that accept groovy Closures and Groovy Vertx/HttpRequest objects?

Cheers,
Paulo
Reply all
Reply to author
Forward
0 new messages