4

I Couldn't find any class/method which gives me access to the referrer header in GWT. anyone knows about this?

Rohan
  • 2,226
  • 2
  • 17
  • 14

5 Answers5

7

See

Document.get().getReferrer()
LINEMAN78
  • 2,562
  • 16
  • 19
  • This seems to be valid answer http://stackoverflow.com/questions/220149/how-do-i-access-the-http-request-header-fields-via-javascript/220169#220169 – Reddy Sep 02 '11 at 13:14
  • Oups - it was my poor navigation techniques. This works great. – Kieveli May 16 '12 at 13:55
  • @Kieveli I have the same problem, that it always returns blank for me! Could you share with me what you mean by "poor navigation techniques" and how you fixed it? Much appreciated. – Markus A. Nov 28 '12 at 05:45
  • The webpage has to be served by a web server. I was testing using a 'test.html' on my desktop. Once you're on a test page served by a web-server, then the link you click on will result in the referer being set. – Kieveli Nov 30 '12 at 14:06
2

Since you can't get the headers in javascript, I don't think you can get them in a GWT client either: Accessing the web page's HTTP Headers in JavaScript

Update:

Maybe you can update login.php to write out the referrer to a hidden input tag, maybe something like this:

<input type="hidden" name="referrer" name="referrer" value="<?php Print referrer_value ?>">

Then, in gwt you should be able to get the value using something like this:

InputElement elt = (InputElement)Document.get().getElementById("referrer")
String referrer = elt.getValue()

Note: This is untested code, and I'm not even sure that is valid php, but hope this helps!

Community
  • 1
  • 1
Upgradingdave
  • 12,916
  • 10
  • 62
  • 72
  • I have page login.php which redirects to a gwt application. Wont i be able to get the referrer header which says "somedomain/login.php" ? Not even in the entry point class? – Rohan Jan 19 '11 at 18:56
  • No, it won't be possible to get any request headers even in the entrypoint class because, remember, that all the java code under the "client" package is actually compiled into javascript code. But, what you could do is use login.php to send the data to the javascript that is generated by gwt. Perhaps you could write the referrer data to some element inside the dom of the page returned by login.php and then access it from gwt that way. – Upgradingdave Jan 19 '11 at 20:32
  • 1
    thank you. I added a extra input element to my login form. :) – Rohan Jan 19 '11 at 21:02
0

You can access to the referrer in JavaScript and pass it to Java (rather to the JavaScript compiled from Java). You need to define a JSNI (JavaScript Native Method) method in Java with a JavaScript definition. This code can access the document and window objects of the browser, although you need to respectively use $doc and $wnd variables for that purpose. More info at

https://developers.google.com/web-toolkit/doc/latest/DevGuideCodingBasicsJSNI

0

You can get the full URL String like so:

String url = Document.get().getURL();

get the index of a question mark and parse it by yourself

Rubber Duck
  • 3,673
  • 3
  • 40
  • 59
0

I had the same question, but I made some changes to charge the header link tag dinamically. I used this code:

LinkElement link = Document.get().createLinkElement();
link.setHref("css/home.css");

I don't know if is the most graceful solution, but it works!

EDIT: If you need to modify any current element you should to do this:

NodeList<Element> links = Document.get().getElementsByTagName("link");
for(int i = 0; i < links.getLength(); i++){
    LinkElement l = (LinkElement)links.getItem(i);
    if( l.toString().contains("href_to_replace.css") ){
        l.setHref("new_href.css");
        break;
    }
}
Sergio Viera
  • 581
  • 6
  • 5