15
votes

Hi I'm trying to set up a static error page for 404 not found errors on app engine.

According to https://developers.google.com/appengine/docs/java/config/webxml#Error_Handlers: it says 404 cannot be customized.

And according to: https://developers.google.com/appengine/docs/java/config/appconfig#Custom_Error_Responses it don't seem to support 404 too.

However, https://groups.google.com/forum/?fromgroups=#!topic/google-appengine-java/3C-pY5ta2HQ, seems to suggest that 404 error page can be customised. I'm confused right now.

7
I am unfamiliar with App Engine, but according to the documentation you have found, it is currently not possible to customize the 404 File Not Found page. - Zéychin
i realised this problem only doesn't work on the local dev server. however, when i deploy the app to app engine. i can see the 404 error page that was configured using web.xml. thanks for all your help. - tommi

7 Answers

10
votes

i realised this problem happens when running the app on local dev server. however, when i deploy the app to app engine. i can see the 404 error page that was configured using web.xml. thanks for all your help.

5
votes

Why don't you use a Java EE <error-page> entry for these purposes?

in your web.xml:

<error-page>
    <error-code>404</error-code>
    <location>/app/error/404</location>
</error-page>
....
<error-page>
    <error-code>500</error-code>
    <location>/app/error/500</location>
</error-page>

You can combine it with Spring and get a general error-controller like:

....   
@Controller
public class ErrorController {

@RequestMapping(value = "/error/{errorCode}")
public final String errorCode(
        @PathVariable("errorCode") final String errorCode,
        final ModelMap modelMap) {
    modelMap.put("statusCode", errorCode);
    return "redirect:/ui/error.htm";
}

and your jsp-like file:

<h2>Error!!!</h2>
....
<h3>Status code: #{param.statusCode}</h3>

Hope this helps.

1
votes

You are correct, this is not currently available in Java on App Engine. However, there is a workaround for Python. For an example on this, you can take a look at GAE Boilerplate.

Hope this helps.

1
votes

I've been able to get the 404 errors successfully redirected in App Engine SDK versions 1.6.1 and earlier on my local dev server. However, after updating to GAE SDK 1.7.5 I cannot get it to work locally. I've since read that it's not currently supported for 404's as stated above.

1
votes

From Google App Engine Standard environment documentation (Aug-19 2018): enter image description here

In short, you must have a servlet mapping for this URL as follow:

<web-app>
    <servlet>
        <servlet-name>404</servlet-name>
        <jsp-file>/404.jsp</jsp-file>
    </servlet>
    <servlet-mapping>
        <servlet-name>404</servlet-name>
        <url-pattern>/404</url-pattern>
    </servlet-mapping>

    ...

    <error-page>
        <error-code>404</error-code>
        <location>/404</location>
    </error-page>
</web-app>

This configuration works as expected. The error page is shown while trying to get a page that does not exist (eg. www.domain.com/unknownPage).

0
votes

At app engine version 1.9.48 this is working on the cloud:

[ web.xml ]

<web-app>

    ...    

    <error-page>
        <error-code>404</error-code>
        <location>/static-error.html</location>
    </error-page>
</web-app>