13

I'm trying to read the query arguments of the URL in client side Java code, but I can't figure out how to find the current URL in Java.

When I tried using httpServletRequest as recommended in this question, it says that it cannot be resolved and it doesn't offer adding an import statement.

I'm using Google Web Toolkit with Google App Engine.

Community
  • 1
  • 1
Senseful
  • 86,719
  • 67
  • 308
  • 465
  • By client-side Java code, you mean GWT code, right? If so, please fix the tags (this is not an app-engine question, but a GWT one). Or do you mean applets? – Thilo Dec 23 '10 at 01:03
  • 1
    @Thilo: sorry, I'm new to both GWT and GAE, so I'm still not sure where one starts and the other ends. You would probably know what it is better than me. – Senseful Dec 23 '10 at 01:07

1 Answers1

24

Look at Window.Location:

public static class Window.Location

This class provides access to the browser's location's object. The location object contains information about the current URL and methods to manipulate it. Location is a very simple wrapper, so not all browser quirks are hidden from the user.

There are a number of methods to retrieve info about the URL, including one to get the whole thing (getHref()) or get the constituent components (e.g. getProtocol(), getHost(), getHostName(), etc).

Since you say you want to read the query arguments, you probably want one of these:

static java.lang.String getQueryString()
   Gets the URL's query string.

static java.lang.String getParameter(java.lang.String name)
  Gets the URL's parameter of the specified name

static java.util.Map<java.lang.String,java.util.List<java.lang.String>> getParameterMap() 
  Returns a Map of the URL query parameters for the host page; since changing the map would not change the window's location, the map returned is immutable.
Bert F
  • 85,407
  • 12
  • 106
  • 123