2

I'm trying to do some simple work with url-pattern that works well on localhost but fails when the application is deployed on the GAE server. What I want to do is next:

  • One servlet to serve all requests
  • This servlet will forward some requests to jsp files (in most cases)

To do this I've configured the web.xml:

<servlet>
    <servlet-name>MyServlet</servlet-name>
    <servlet-class>com.mycompany.MyServlet</servlet-class>
</servlet>

<servlet-mapping>
    <servlet-name>MyServlet</servlet-name>
    <url-pattern>/MyServlet</url-pattern>
</servlet-mapping>
<welcome-file-list>
    <welcome-file>MyServlet</welcome-file>
</welcome-file-list>

This works fine with the GAE localhost server, but when the app is deployed on the real GAE server, it doesn't. The problem is that when I access to myapp.appspot.com/anything GAE responds:

Error: Not Found The requested URL /anything was not found on this server.

Can anyone help me, plese?

Marto
  • 21
  • 1
  • 3
  • Sorry, the code is not show well. first must be the servlet declaration as "MyServlet". – Marto Dec 29 '11 at 23:52

1 Answers1

1

You're missing the Servlet element. The Servlet-mapping element by itself is not sufficient. Building off of your web.xml try this....

    <servlet>
            <servlet-name>MyServlet</servlet-name>
            <servlet-class>package.path.to.my.servlet.MyServlet</servlet-class>
    </servlet>
    <servlet-mapping>
            <servlet-name>MyServlet</servlet-name>
            <url-pattern>/MyServlet</url-pattern>
    </servlet-mapping>

Of course replacing the package.path.to.my.servlet with the actual package of your MyServlet class. Once you've done this you can put in multiple servlet-mapping elements to map different urls to the same servlet.

If this is happening at your appspot url, you can try and make sure all of your static files are being deployed to your appspot...

    <static-files>
      <include path="/**.css" />
      <include path="/**.xml" />
    </static-files>

Just note that this means you're deploying all xmls/css files within your war file. Adjust the include path attribute accordingly if you don't want to do this.

Also, to have all requests go to one servlet, just use the following url pattern...

<url-pattern>/*</url-pattern>

Just keep in mind when using this pattern that if you have images/css files/jsp files/etc that this servlet will intercept the request for them. So you're going to have some kind of logic that forwards the requests you're not interested in. This might be sufficient if you're doing something straight forward; but as you get more complicated (ex caching css files/image files) you're probably going to want to look into adding a static resource filter.

Dave
  • 6,141
  • 2
  • 38
  • 65
  • Thanks Dave. It was a mistake posting the code, but I have the servlet tag already in my web.xml. In fact, it works well in localhost... :( – Marto Dec 30 '11 at 01:00
  • If that is the case, then you should probably update your answer to include your full web.xml, full stack trace (go to `https://appengine.google.com/dashboard?&app_id=s~YOUR-APPSPOT-SUBDOMAIN` where `YOUR-APPSPOT-SUBDOMAIN` is from `YOUR-APPSPOT-SUBDOMAIN.appspot.com` and it should be in the `Errors` section in bottom right). – Dave Dec 30 '11 at 02:07
  • Also, it might be possible your web xml wasn't uploaded properly. You can verify by adjusting your appengine-web.xml and adding the static-files element as I've described if you haven't already. – Dave Dec 30 '11 at 02:16
  • Thanks dave. I think that there was a deployment issue, because I've redeployed and now all works fine! – Marto Dec 30 '11 at 14:08