tags:

views:

67

answers:

1

Hi,

I'm using GWT with GAE. When the user enters any of the following urls, I want to just serve my app as usual to them:

http://www.mysite.com/
http://www.mysite.com/dog
http://www.mysite.com/cat

the first case works by default. I'm not sure how to get the /dog and /cat cases to work. I think I have to modify something with the url mappings to get that to work in web.xml. Essentially I'm trying to just get my app served with any url entered:

http://www.mysite.com/*

I'm trying this with a brand new project, so my web.xml looks like this:

<!-- Servlets -->
<servlet>
  <servlet-name>greetServlet</servlet-name>
  <servlet-class>com.me.test.server.GreetingServiceImpl</servlet-class>
</servlet>

<servlet-mapping>
  <servlet-name>greetServlet</servlet-name>
  <url-pattern>/test/greet</url-pattern>
</servlet-mapping>

and now I've appended the following below:

<servlet>
  <servlet-name>servletGate</servlet-name>
  <servlet-class>com.me.test.server.ServletGate</servlet-class>
</servlet>

<servlet-mapping>
  <servlet-name>servletGate</servlet-name>
  <url-pattern>/*</url-pattern>
</servlet-mapping>

when I enter a url like:

http://localhost:8888/
http://localhost:8888/dog

I get a null pointer exception thrown on the doGet() line here:

getServletConfig().getServletContext().getRequestDispatcher("test.html").forward(request,response);

what have I missed?

Thanks

+1  A: 

Can you please post your current web.xml? You should be able to add a servlet mapping like:

  <servlet>
    <servlet-name>myServlet</servlet-name>
    <servlet-class>com.example.MyServlet</servlet-class>
  </servlet>

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

You would then write a simple Servlet like:

public class MyServlet extends HttpServlet {

  public void doGet(HttpServletRequest request, HttpServletResponse response)
          throws ServletException {

      getServletConfig().getServletContext().getRequestDispatcher(
        "/MyApplication.html").forward(request,response);

  }

}

Or you could also generate the content dynamically, either directly in the servlet, or in a JSP, or by using any technique you prefer - this is often very useful anyway!

In response to your comment:

Because you want to map every path to the servlet, you get an endless loop. A solution with an InputStream could look like this:

public class SomeServlet extends HttpServlet {

    @Override
    protected void doGet(final HttpServletRequest request, final HttpServletResponse response)
            throws ServletException,
            IOException {

        final InputStream is = getClass().getClassLoader().
                 getResourceAsStream("/com/example/MyApplication.html");
        final byte[] buffer = new byte[255];
        int len = 0;
        while ((len = is.read(buffer)) != -1) {
            response.getOutputStream().write(buffer, 0, len);
        }
        response.flushBuffer();

    }
}

Put the MyApplication.html file in your source folder in the package com.example.

If you don't want to put the html file in your source folder, you can also use any other means to create an InputStream (e.g. from any File using FileInputStream).

Chris Lercher
Ok I tried to update as directed, I must have messed something up though because I'm getting an exception in the servlet (but at least it's being called with the wild card mapping which is awesome!)
@user291701: I think I forgot the forward slash in "/MyApplication.html". But thinking about it, this could lead to an indefinite loop - because in your case, "/MyApplication.html" will also be served by the servlet! So in this special case, it's probably better to simply include the html file by reading it with an InputStream or something.
Chris Lercher
Ah yes I'll get a stackoverflow error if I use "/MyApp.html", you're right. Can you elaborate on "reading it with an InputStream", or what would this technique be called, I'm not sure what to search for? Thanks
@user291701: I updated the answer.
Chris Lercher