I have to replace the protocol part of a already existing url in GWT. The java.net
package has a class which was build for exactly that purpose: URL. Sadly GWT does not emulate the java.net package.
How can I reassemble a url in GWT without creating my own parser? (I know about UrlBuilder, but UrlBuilder won't take an existing URL)
Example: I have a url in a string "http://myserver.com/somepath/file.html?param" and I want to replace the protocol part with "https".
public void onModuleLoad() {
Button btn = new Button("change protocol");
btn.addClickHandler(new ClickHandler() {
@Override
public void onClick(ClickEvent event) {
UrlBuilder builder = Window.Location.createUrlBuilder().setProtocol("https");
Window.Location.replace(builder.buildString());
}
});
RootPanel.get().add(btn);
}
It's ugly, but you can always create an anchor element and extract the parts from there.
AnchorElement a = Document.get().createAnchorElement();
a.setHref("http://test.com/somerandompath");
Window.alert(a.getPropertyString("protocol") + " " + a.getPropertyString("host")) + " " a.getPropertyString("pathname"));
a.removeFromParent();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With