Play Framework 2.2 : Get URL of the requesting page

Incpetor picture Incpetor · Jun 12, 2014 · Viewed 14.9k times · Source

PLAY FRAMEWORK JAVA:

I am trying to get the name of the URL that requested the controller function. For example , I have a routes as

GET /mypage controllers.Mypage.myfunction()

and I have another page that requests the same controller

GET /anotherpage controllers.Mypage.myfunction()

is there a way to find in controllers if the request is from /mypage or from /anotherpage?

Thanks

Answer

Michael Zajac picture Michael Zajac · Jun 12, 2014

Say you visit example.com:9000/login?param=test, then within your controller function:

public static Result login() {

    // set to "/login" -- The URI path without query parameters.
    String path = request().path(); 

    // set to "/login?param=test" -- The full URI.
    String uri = request().uri(); 

    // set to "example.com:9000" -- The host name from the request, with port (if specified by the client).
    String host = request().host(); 

    ...
}