Reputation: 39671
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
Upvotes: 0
Views: 3421
Reputation: 37778
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!
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).
Upvotes: 1