AtariPete
AtariPete

Reputation: 1408

Servlet mapping works locally but not on webserver in app engine

I am running into an issue with servlet mappings on app engine.

Details
Using my web.xml config file I am mapping the URL pattern "/i" to redirect to info.html on the root directory of my WAR path.

When I run this locally and access the url localhost:8888/i the web server properly redirects to the info.html site. But when I push to app engine and attempt to access www.mysite.com/i I get an 404 Error: NOT_FOUND

Question
1. Why does my server mapping work locally but not remotely? 2. Is there another way I should map a url pattern like www.mysite.com/i to a static file in my war path?

*Though I'm on app engine I would assume that this would be similar to other Java servers that make use of servlet-mappings via web.xml. Also i know mapping to a static html file might seem odd, but currently I'm not loading any dynamic content.

Code Sample
Below I have included a trimmed down version of my web.xml file for reference. It isolates how I am doing the redirect.

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

    <servlet>
        <servlet-name>Info</servlet-name>
        <jsp-file>info.html</jsp-file>
    </servlet>

    <servlet-mapping>
        <servlet-name>Info</servlet-name>
        <url-pattern>/i</url-pattern>
    </servlet-mapping>

    <welcome-file-list>
        <welcome-file>index.html</welcome-file>
    </welcome-file-list>
</web-app>

Upvotes: 2

Views: 2148

Answers (2)

Zsolt Safrany
Zsolt Safrany

Reputation: 13620

Static files, files that are served verbatim to users such as images, CSS or JavaScript, are handled separately from paths mentioned in the deployment descriptor. A request for a URL path that matches a path to a file in the WAR that's considered a static file will serve the file, regardless of servlet and filter mappings in the deployment descriptor. You can exclude files from those treated as static files using the appengine-web.xml file.

That is, if you want to map an .html to an URL then you have to make sure that it is not static; because we know static files are served regardless of servlet and filter mappings.

To do that you have to exclude it from the static files with:

<static-files>
    <exclude path="/**.html"/>
</static-files>

This would make that .html file a resource-file only (all files are both static and resource files by default; except .jsp files and files in the web-inf folder).

But unfortunately, that still won't make App Engine pick up the URL mapping (defined with 'jsp-file') for the .html file. Most probably because a servlet-mapping points eventually to a servlet and while a servlet is generated for each .jsp file the same is not true for .html files (or any other file).

The only solution I could come up with is that you rename the .html file to .jsp (and remove the static-files exclusion; you don't need that anymore). This way you can map it to any URL.

The funny thing is that you can map non .jsp files to URLs on the dev server; just another inconsistency with App Engine. ;)

Upvotes: 0

user1258245
user1258245

Reputation: 3639

Keep in mind AE is not exactly the same:

By default, App Engine makes all files in the WAR available as static files except JSPs and files in WEB-INF/. Any request for a URL whose path matches a static file serves the file directly to the browser—even if the path also matches a servlet or filter mapping. You can configure which files App Engine treats as static files using the appengine-web.xml file.

From https://developers.google.com/appengine/docs/java/gettingstarted/staticfiles

Directions to configure appengine-web.xml are here https://developers.google.com/appengine/docs/java/config/appconfig

I would try declaring it explicitly:

<static-files>
    <include path="info.html" />
</static-files>

And if that didn't work would just change my .html page to really be a jsp page (even though there is no dynamic content).

And if that didn't work would try a RequestDispatcher from within a servlet to forward the html page see http://www.jguru.com/faq/view.jsp?EID=1310997

By the way, my experience is that the devmode server and deployed server sometimes resolve files differently even though the appengine-web.xml are the same.

Upvotes: 1

Related Questions