Thursday, June 2, 2011

Re: GWT servlet access

404 means not found

try to print out your request-url first to see if it matches host:port/mycompany/sayHello or not.
i think it will not match, your request url will also include your gwt-module-name in the path



On Thu, Jun 2, 2011 at 8:15 PM, Ybrek <xybrek@gmail.com> wrote:
Hi i'm trying to access a simple servlet from within GWT (dev mode):

The basic idea is to get the simple response from the servlet and then
show it to GWT client side.

I think I'm missing something here or have misconfigured the code, I
presume in the RequestBuilder part or in the url-pattern. I'm always
getting 404 response.

Can anyone help? Cheers, Ybrek



SayHelloServlet.java

package com.mycompany.server;

import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;

public class SayHelloServlet extends HttpServlet {
         public void doGet(HttpServletRequest request,
                           HttpServletResponse response)
             throws ServletException, IOException {
           PrintWriter out = response.getWriter();
           out.println("Hello!!!");
         }
}


ServletDemo.java

package com.mycompany.client

public class ServletDemo implements EntryPoint {
..
       public void onModuleLoad() {
       ...
               makeServletCall();
       ...

       }
..
  public void makeServletCall() {
       String url = GWT.getHostPageBaseURL() + "sayHello";;
       RequestBuilder builder = new RequestBuilder(RequestBuilder.GET,
                       URL.encode(url));

       try {

               builder.sendRequest(null, new RequestCallback(){

                               public void onError(Request request, Throwable exception) {
                                       Window.alert("Error ("+ exception +")");
                               }

                               public void onResponseReceived(Request request,
                                               Response response) {
                                       if (200 == response.getStatusCode()) {
                                               Window.alert("the string: "+ response.getText());
                                       }
                                       else {
                                       // Handle the error. Can get the status text from
response.getStatusText()
                                       Window.alert(" Connected with error(maybe): " +
response.getText());
                                       }

                               }});

               } catch (Exception e) {
                       GWT.log(e.toString());
                       Window.alert("Whoosps " + e.toString());
               }
   }
...
}

Here's the web.xml in the war folder:

web.xml

<web-app>
...
 <servlet>
     <servlet-name>sayHelloServlet</servlet-name>
     <servlet-class>com.mycompany.server.SayHelloServlet</servlet-
class>
 </servlet>

 <servlet-mapping>
     <servlet-name>sayHelloServlet</servlet-name>
     <url-pattern>/mycompany/sayHello</url-pattern>
 </servlet-mapping>
...
</web-app>

--
You received this message because you are subscribed to the Google Groups "Google Web Toolkit" group.
To post to this group, send email to google-web-toolkit@googlegroups.com.
To unsubscribe from this group, send email to google-web-toolkit+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/google-web-toolkit?hl=en.


--
You received this message because you are subscribed to the Google Groups "Google Web Toolkit" group.
To post to this group, send email to google-web-toolkit@googlegroups.com.
To unsubscribe from this group, send email to google-web-toolkit+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/google-web-toolkit?hl=en.

No comments:

Post a Comment