Wednesday 14 January 2015

Spark java : Request parameter


As you observe implementation of get method, it takes request and response objects as parameters.

get("/hello/*/welcome/*", (request, response) -> {     
        return "Hello";
});


Request object provides number of APIs to access request parameters, cookies and session handling.

request.body();               // request body sent by the client
request.cookies();            // request cookies sent by the client
request.contentLength();      // length of request body
request.contentType();        // content type of request.body
request.headers();            // the HTTP header list
request.headers("BAR");       // value of BAR header
request.attributes();         // the attributes list
request.attribute("foo");     // value of foo attribute
request.attribute("A", "V");  // sets value of attribute A to V
request.host();               // "example.com"
request.ip();                 // client IP address
request.pathInfo();           // the path info
request.params("foo");        // value of foo path parameter
request.params();             // map with all parameters
request.port();               // the server port
request.queryMap();           // the query map
request.queryMap("foo");      // query map for a certain parameter
request.queryParams("FOO");   // value of FOO query param
request.queryParams();        // the query param list
request.raw();                // raw request handed in by Jetty
request.requestMethod();      // The HTTP method (GET, ..etc)
request.scheme();             // "http"
request.session();            // session management
request.splat();              // splat (*) parameters
request.url();                // "http://example.com/foo"
request.userAgent();          // user agent


For cookies support
request.cookies();                              // get map of all request cookies
request.cookie("foo");                          // access request cookie by name


For session support
request.session(true)                            // create and return session
request.session().attribute("user")              // Get session attribute 'user'
request.session().attribute("user", "foo")       // Set session attribute 'user'
request.session().removeAttribute("user", "foo") // Remove session attribute 'user'
request.session().attributes()                   // Get all session attributes
request.session().id()                           // Get session id
request.session().isNew()                        // Check is session is new
request.session().raw()                          // Return servlet object

import static spark.Spark.*;

import java.util.Set;

public class HelloSpark {
  public static void main(String[] args) {  
    get("/hello", (request, response) -> {
        Set<String> queryParams = request.queryParams();
        StringBuilder str = new StringBuilder();
        str.append("Request Parameters are <br/>");
        for(String param : queryParams){
          str.append(param).append(" ").append(request.queryParams(param)).append("<br />");
        }
        
        return str.toString();
    });
  }

}


For the URL

Response shows below data.

Request Parameters are
firstName hari
lastName Krishna
age 25



Prevoius                                                 Next                                                 Home

No comments:

Post a Comment