Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Play Framework 2.2 : Get URL of the requesting page

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

like image 634
Incpetor Avatar asked Jun 12 '14 08:06

Incpetor


2 Answers

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

    ...
}
like image 69
Michael Zajac Avatar answered Oct 29 '22 11:10

Michael Zajac


you didn't mention language

In Scala you can get path in controller by

val path= request.path

or

val path=request.uri

i have tried this in play framework 2.2*


In Java

String path= request.path();

or

String path=request.url();

according to this link http://www.playframework.com/documentation/1.0.1/api/play/mvc/Http.Request.html

like image 35
Govind Singh Avatar answered Oct 29 '22 13:10

Govind Singh