Chris
Chris

Reputation: 1587

GWT - making GET requests

Simple question. I need to make a GET request in GWT that redirects to a new page, but I can't find the right API.

Is there one? I am supposed to simply form the URL myself and then do Window.Location.replace?

(The reason is that I want my search page to be linkable)

Thanks.

(and sorry for not making my question clear enough, initially)

Upvotes: 12

Views: 10091

Answers (5)

eQ19
eQ19

Reputation: 10691

Similar with answer from ivo. I can do this in my GWT todomvc frame with filter mapping instead of servlet-mapping in web.xml file.

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
              http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
         version="2.5"
         xmlns="http://java.sun.com/xml/ns/javaee">

  <filter>
    <filter-name>guiceFilter</filter-name>
    <filter-class>com.google.inject.servlet.GuiceFilter</filter-class>
  </filter>

  <filter-mapping>
    <filter-name>guiceFilter</filter-name>
    <url-pattern>/myurl/*</url-pattern>
  </filter-mapping>

  <listener>
    <listener-class>com.todomvc.server.ToDoServerInjector</listener-class>
  </listener>

  <!-- Default page to serve -->
  <welcome-file-list>
    <welcome-file>GwtGaeChannelToDo.html</welcome-file>
  </welcome-file-list>

</web-app>

Upvotes: 0

Silfverstrom
Silfverstrom

Reputation: 29322

have a look at http://library.igcar.gov.in/readit2007/tutori/tools/gwt-windows-1.4.10/doc/html/com.google.gwt.http.client.html

public class GetExample implements EntryPoint {

    public static final int STATUS_CODE_OK = 200;

    public static void doGet(String url) {
        RequestBuilder builder = new RequestBuilder(RequestBuilder.GET, url);

        try {
            Request response = builder.sendRequest(null, new RequestCallback() {
                public void onError(Request request, Throwable exception) {
                    // Code omitted for clarity
                }

                public void onResponseReceived(Request request, Response response) {
                    // Code omitted for clarity
                }
            });

        } catch (RequestException e) {
            // Code omitted for clarity
        }
    }

    public void onModuleLoad() {
        doGet("/");
    }
}

Upvotes: 14

Chris
Chris

Reputation: 1587

Redirecting to a new page is done with Window.Location.replace.

Multiple pages should be handled using the history mechanism.

Upvotes: 2

OtherDevOpsGene
OtherDevOpsGene

Reputation: 7451

If you are opening a separate window, it is easy:

Window.open(url, windowName, "resizable=yes,scrollbars=yes,menubar=yes,location=yes,status=yes");

Otherwise, use RequestBuilder as Silfverstrom suggests.

Upvotes: 1

ivo
ivo

Reputation: 4211

GWT does not prohibit you from using regular servlets.

You can declare a servlet in your 'web.xml' file:

<servlet>
    <servlet-name>myServlet</servlet-name>
    <servlet-class>org.myapp.server.MyServlet</servlet-class>
</servlet>

<servlet-mapping>
    <servlet-name>myServlet</servlet-name>
    <url-pattern>/myurl/*</url-pattern>
</servlet-mapping>

and then you can implement your Servlet:

public class MyServlet extends HttpServlet {

public void doGet(HttpServletRequest req, HttpServletResponse resp) throws 
        IOException {

      // your code here

}

}

Upvotes: 2

Related Questions