Google App Engine

Java Application Configuration with appengine-web.xml

Python |Java |PHP |Go

In addition to the web.xml deployment descriptor, an App Engine Java application uses a configuration file, named appengine-web.xml, to specify the app's registered application ID and the version identifier of the latest code, and to identify which files in the app's WAR are static files (like images) and which are resource files used by the application. The AppCfg command uses this information when you upload the app.

Note: If you created your project using the Google Cloud Console, your project has a title and an ID. In the instructions that follow, the project title and ID can be used wherever an application title and ID are mentioned. They are the same thing.

  1. About appengine-web.xml
  2. Static files and resource files
  3. System properties and environment variables
  4. Configuring Secure URLs (SSL)
  5. Enabling sessions
  6. Reserved URLs
  7. Inbound services
  8. Warmup requests
  9. Disabling precompilation
  10. Administration console custom pages
  11. Custom error responses
  12. Using concurrent requests
  13. Custom PageSpeed configuration
  14. Auto ID policy

About appengine-web.xml

An App Engine Java app must have a file named appengine-web.xml in its WAR, in the directory WEB-INF/. This is an XML file whose root element is <appengine-web-app>. A minimal file that specifies the application ID, a version identifier, and no static files or resource files looks like this:

<?xml version="1.0" encoding="utf-8"?>
<appengine-web-app xmlns="http://appengine.google.com/ns/1.0">
  <application>_your_app_id_</application>
  <version>1</version>
  <threadsafe>true</threadsafe>
</appengine-web-app>

The <application> element contains the application's ID. This is the application ID you register when you create your application in the Admin Console. When you upload your app, AppCfg gets the application ID from this file.

The <version> element contains the version identifier for the latest version of the app's code. The version identifier can contain lowercase letters, digits, and hyphens. It cannot begin with the prefix "ah-" and the names "default" and "latest" are reserved and cannot be used. AppCfg uses this version identifier when it uploads the application, telling App Engine to either create a new version of the app with the given identifier, or replace the version of the app with the given identifier if one already exists. You can test new versions of your app using a URL using "-dot-" as a subdomain separator in the URL, e.g. http://_version_id_-dot-_your_app_id_.appspot.com. You can select which version of the app your users see, the "default" version, using the Admin Console.

The <threadsafe> element defines whether App Engine can send multiple requests at the same time to a given web server. It is further described in the section Using concurrent requests.

The <static-files> and <resource-files> elements are described in the next section.

You can find the DTD and schema specifications for this file in the SDK's docs/ directory.

Static files and resource files

Many web applications have files that are served directly to the user's browser, such as images, CSS style sheets, or browser JavaScript code. These are known as static files because they do not change, and can benefit from web servers dedicated just to static content. App Engine serves static files from dedicated servers and caches that are separate from the application servers.

Files that are accessible by the application code using the filesystem are called resource files. These files are stored on the application servers with the app.

By default, all files in the WAR are treated as both static files and resource files, except for JSP files, which are compiled into servlet classes and mapped to URL paths, and files in the WEB-INF/ directory, which are never served as static files and always available to the app as resource files.

You can adjust which files are considered static files and which are considered resource files using elements in the appengine-web.xml file. The <static-files> element specifies patterns that match file paths to include and exclude from the list of static files, overriding or amending the default behavior. Similarly, the <resource-files> element specifies which files are considered resource files.

Including and excluding files

Path patterns are specified using zero or more <include> and <exclude> elements. In a pattern, * represents zero or more of any character in a file or directory name, and ** represents zero or more directories in a path. Files and directories matching <exclude> patterns will not be uploaded when you deploy your app to App Engine. However, these files and directories will still be accessible to your application when running on the local Development Server.

An <include> element overrides the default behavior of including all files. An <exclude> element applies after all <include> patterns (as well as the default if no explicit <include> is provided).

The following example demonstrates how to designate all .png files as static files (except those in the data/ directory and all of its subdirectories):

<static-files>
  <include path="/**.png" />
  <exclude path="/data/**.png" />

Similarly, the following sample demonstrates how to designate all .xml files as resource files (except those in the feeds/ directory and all of its subdirectories):

<resource-files>
  <include path="/**.xml" />
  <exclude path="/feeds/**.xml" />

You can also set HTTP headers to use when responding to requests to these resources.

<static-files>
  <include path="/my_static-files" >
    <http-header name="Access-Control-Allow-Origin" value="http://example.org" />
  </include>
</static-files>

Changing the MIME type for static files

By default, static files are served using a MIME type selected based on the filename extension. You can associate custom MIME types with filename extensions for static files in web.xml using <mime-mapping> elements.

Changing the root directory

The <public-root> is a directory in your application that contains the static files for your application. When a request for a static file is made, the <public-root> for your application is prepended to the request path. This gives the path of an application file containing the content that is being requested.

The default <public-root> is /.

For example, the following would map the URL path /index.html to the application file path /static/index.html:

<public-root>/static</public-root>

Static cache expiration

Unless told otherwise, web proxies and browsers retain files they load from a website for a limited period of time.

You can configure a cache duration for specific static file handlers by providing an expiration attribute to <static-files><include ... >. The value is a string of numbers and units, separated by spaces, where units can be `d` for days, `h` for hours, `m` for minutes, and `s` for seconds. For example, `"4d 5h"` sets cache expiration to 4 days and 5 hours after the file is first requested. If the expiration time is omitted, the production server defaults to 10 minutes.

(Script handlers can set cache durations by returning the appropriate HTTP headers to the browser.)

For example:

<static-files>
  <include path="/**.png" expiration="4d 5h" />
</static-files>

System properties and environment variables

The appengine-web.xml file can define system properties and environment variables that are set when the application is running.

<system-properties>
  <property name="myapp.maximum-message-length" value="140" />
  <property name="myapp.notify-every-n-signups" value="1000" />
  <property name="myapp.notify-url" value="http://www.example.com/signupnotify" />
</system-properties>

<env-variables>
  <env-var name="DEFAULT_ENCODING" value="UTF-8" />
</env-variables>

Configuring Secure URLs (SSL)

By default, any user can access any URL using either HTTP or HTTPS. You can configure an app to require HTTPS for certain URLs in the deployment descriptor. See Deployment Descriptor: Secure URLs.

If you want to disallow the use of HTTPS for the application, put the following in the appengine-web.xml file:

<ssl-enabled>false</ssl-enabled>

There is no way to disallow HTTPS for some URL paths and not others in the Java runtime environment.

Enabling sessions

App Engine includes an implementation of sessions, using the servlet session interface. The implementation stores session data in the App Engine datastore for persistence, and also uses memcache for speed. As with most other servlet containers, the session attributes that are set with session.setAttribute() during the request are persisted at the end of the request.

This feature is off by default. To turn it on, add the following to appengine-web.xml:

<sessions-enabled>true</sessions-enabled>

The implementation creates datastore entities of the kind _ah_SESSION, and memcache entries using keys with a prefix of _ahs.

It's possible to reduce request latency by configuring your application to asynchronously write HTTP session data to the datastore:

<async-session-persistence enabled="true" />

With async session persistence turned on, App Engine will submit a Task Queue task to write session data to the datastore before writing the data to memcache. By default the task will be submitted to the default queue. If you'd like to use a different queue, add the queue-name attribute:

<async-session-persistence enabled="true" queue-name="myqueue"/>

Reserved URLs

Several URL paths are reserved by App Engine for features or administrative purposes. Script handler and static file handler paths will never match these paths.

The following URL paths are reserved:

  • /_ah/
  • /form

Inbound services

Before an application can receive email or XMPP messages, the application must be configured to enable the service. You enable the service for a Java app by including an <inbound-services> section in the appengine-web.xml file. The following inbound services are available:

For example, you can enable mail and XMPP messages by specifying the following in appengine-web.xml:

<inbound-services>
  <service>mail</service>
  <service>warmup</service>
</inbound-services>

Disabling precompilation

App Engine uses a "precompilation" process with the Java bytecode of an app to enhance the performance of the app in the Java runtime environment. Precompiled code functions identically to the original bytecode.

If for some reason you prefer that your app not use precompilation, you can turn it off by adding the following to your appengine-web.xml file:

<precompilation-enabled>false</precompilation-enabled>

Warmup requests

App Engine frequently needs to load application code into a fresh instance. This happens when you redeploy the application, when the load pattern has increased beyond the capacity of the current instances, or simply due to maintenance or repairs of the underlying infrastructure or physical hardware.

Loading new application code on a fresh instance can result in loading requests. Loading requests can result in increased request latency for your users, but you can avoid this latency using warmup requests. Warmup requests load application code into a new instance before any live requests reach that instance.

App Engine attempts to detect when your application needs a new instance, and (assuming that warmup requests are enabled for your application) initiates a warmup request to initialize the new instance. However, these detection attempts do not work in every case. As a result, you may encounter loading requests, even if warmup requests are enabled in your app. For example, if your app is serving no traffic, the first request to the app will always be a loading request, not a warmup request.

Warmup requests use instance hours like any other request to your App Engine application. In most cases, you won't notice an increase in instance hours, since your application is simply initializing in a warmup request instead of a loading request. Your instance hour usage will likely increase if you decide to do more work (such as precaching) during a warmup request. If you set a minimum number of idle instance, you may encounter warmup requests when those instances first start, but they will remain available after that time.

The default warmup request causes all JAR files to be indexed in memory and initializes your application and filters. The following sections describe how to configure warmup requests and create warmup logic for your application:

Configuration

Warmup requests are enabled by default for all Java applications.

If you configured your app with web.xml, you can implement warmup requests via an optional <warmup-requests-enabled> element in appengine-web.xml, which defaults to true. This causes the App Engine infrastructure to issue GET requests to /_ah/warmup, initializing <load-on-startup> servlets, ServletContextListeners, and custom warmup servlets - which allow you to initialize your application's code as it requires. You may or may not need to implement your own handler for /_ah/warmup depending on which of these methods you choose.

To disable warmup requests, specify the <warmup-requests-enabled> element with a value of false:

<warmup-requests-enabled>false</warmup-requests-enabled>

Warmup requests are not enabled by default if you configured your Java application with app.yaml. For details, please refer to Java Application Configuration Using app.yaml.

Using a <load-on-startup> servlet

The easiest way to provide warmup logic is to mark your own servlets as <load-on-startup> in web.xml. This method requires no changes to your application code, and initializes all specified servlets when your application initializes. The following example demonstrates how to load my-servlet on startup:

<servlet>
  <servlet-name>my-servlet</servlet-name>
  <servlet-class>com.company.MyServlet</servlet-class>
  <load-on-startup>1</load-on-startup>
</servlet>

These lines load the specified servlet class and invoke the servlet's init() method. The warmup request initializes the specified servlets before servicing any live requests. However, if there is no warmup request, the servlets specified in <load-on-startup> are registered upon the first request to a new instance, which result in a loading request. As noted earlier, App Engine may not issue a warmup request every time your application needs a new instance.

Using a ServletContextListener

If you have custom logic that you want to run before any of your servlets is invoked, the standard Java mechanism to arrange for that code to be executed is to register a ServletContextListener in web.xml.

<listener>
  <listener-class>com.company.MyListener</listener-class>
</listener>

And then supply a class alongside your servlet and filter code:

public class MyListener implements ServletContextListener {
  public void contextInitialized(ServletContextEvent event) {
    // This will be invoked as part of a warmup request, or the first user
    // request if no warmup request was invoked.
  }
  public void contextDestroyed(ServletContextEvent event) {
    // App Engine does not currently invoke this method.
  }
}

The ServletContextListener runs during a warmup request. If there is no warmup request, it runs upon the first request to a new instance. This may result in loading requests.

Using a custom warmup servlet

As noted earlier, the prediction algorithm to initiate warmup requests for new instances does not work in every case. Even with this feature enabled, you may encounter loading requests. Executing expensive logic (such as precaching) during a loading request may incur additional load time when a new instance starts to run.

The custom warmup servlet invokes the servlet's service method only during a warmup request. By placing expensive logic in a custom warmup servlet, you can avoid increased load times on loading requests.

To create a custom warmup servlet, simply override the built-in servlet definition for _ah_warmup in web.xml:

<servlet>
  <servlet-name>_ah_warmup</servlet-name>
  <servlet-class>com.company.MyWarmupServlet</servlet-class>

Administration console custom pages

If you have administrator-only pages in your application that are used to administer the app, you can have those pages appear in the Administration Console. The Administration Console includes the name of the page in its sidebar, and displays the page in an HTML iframe. To add a page to the Administration Console, add an <admin-console> section in your app's appengine-web.xml file, like so:

<admin-console>
  <page name="Blog Comment Admin" url="/blog/admin/comments" />
  <page name="Create a Blog Post" url="/blog/admin/newentry" />
</admin-console>

For each page, url is the URL path to the page, and name is what will appear in the Administration Console navigation. Custom page names can only contain ASCII characters. Custom page urls may be re-routed by rules in the dispatch file, if one exists.

Custom error responses

When certain errors occur, App Engine serves a generic error page. You can configure your app to serve a custom static file instead of these generic error pages, so long as the custom error data is less than 10 kilobytes. You can set up different static files to be served for each supported error code by specifying the files in your app's appengine-web.xml file. To serve custom error pages, add a <static-error-handlers> section to your appengine-web.xml, as in this example:

<static-error-handlers>
  <handler file="default_error.html" />
  <handler file="over_quota.html" error-code="over_quota" />
</static-error-handlers>

Each file entry indicates a static file that should be served in place of the generic error response. The error-code indicates which error code should cause the associated file to be served. Supported error codes are as follows:

The error-code is optional; if it's not specified, the given file is the default error response for your app.

You can optionally specify a mime-type to use when serving the custom error. See http://www.iana.org/assignments/media-types/ for a complete list of MIME types.

Using concurrent requests

When the threadsafe element in appengine-web.xml is false, App Engine sends requests serially to a given web server. When the value is true, App Engine can send multiple requests in parallel:

<threadsafe>true</threadsafe>

Custom PageSpeed configuration

Experimental!

App Engine's support for PageSpeed is an experimental, innovative, and rapidly changing new feature for Google App Engine. Unfortunately, being on the bleeding edge means that we may make backwards-incompatible changes to App Engine's support for PageSpeed. We will inform the community when this feature is no longer experimental.
 


When enabled, PageSpeed Service will be applied globally to your application. All versions of your app will be automatically optimized with the same configuration, that of the most-recently updated version. If you want to try a new configuration, perhaps to test some "risky" optimizations, you might expect that you could do so in a test Application Version while your users continue to use the default Application Version. But since updating the test version's configuration applies to all versions, the "risky" settings are applied to the version that your users use, too. Instead, to try out these settings, you could

  • Turn off PageSpeed for your application, update PageSpeed configuration, and view your site using the PageSpeed chrome extension; or
  • Copy the relevant parts of your application to a separate test application that has its own application ID and versions.

PageSpeed is a family of tools for optimizing the performance of web pages. You can use the Application Console to enable PageSpeed with a solid set of safe default optimizations. You can also edit your application configuration to fine-tune PageSpeed. You can configure PageSpeed to ignore some URLs; you can turn on some "risky" optimizations that don't work for all sites but might work for yours. For a custom PageSpeed configuration, you can add a pagespeed section to your application configuration. An example that shows the possible parts (but not all choices) of pagespeed:

<pagespeed>
  <domain-to-rewrite>*.cdn.myapp.com</domain-to-rewrite>
  <domain-to-rewrite>www.flickr.com</domain-to-rewrite>
  <url-blacklist>http://*/*.svg</url-blacklist>
  <url-blacklist>http://secure.foo.com/*</url-blacklist>
  <enabled-rewriter>CollapseWhitespace</enabled-rewriter>
  <disabled-rewriter>CombineJs</disabled-rewriter>
  <disabled-rewriter>ProxyImages</disabled-rewriter>
</pagespeed>
</appengine-web-app>

This section may have any or all of the following parts:

domain-to-rewrite
Which domains' content PageSpeed should rewrite. Normally, PageSpeed only rewrites data served by your application. But you can tell PageSpeed to rewrite content from other domains when your application shows that data. For example, your application might use a free image hosting service on some other domain. Since the image hosting site isn't part of your application, PageSpeed doesn't optimize those images by default. You might want it to optimize these images: they can be compressed and cached by Google, and thus displayed faster. You might not want it to optimize these images: Since pagespeed optimizes the images, you will be charged whenever those images are served.
url-blacklist
This is a list of URLS; the * character is a wildcard. PageSpeed won't try to optimize URLs matching these URLs. This can be especially useful if you turn on some "risky" optimizations that break content for some URLs but make the rest of your site much much faster. By default, there is no blacklist.
enabled-rewriter, disabled-rewriter
By default, some "safe" optimization rewriters are turned on. Other "risky" optimizations are not turned on. You can choose more optimizations to use by listing them in enabled-rewriter; you can turn off default rewriters by listing them in disabled-rewriter. The following rewriters are available:

HTML Rewriters

CSS Rewriters

Image Rewriters

JavaScript Rewriters

Auto ID policy

If you are setting entity identifiers automatically, you can change the method employed by setting the auto ID policy. You can choose default (default is also applied if you specify nothing) or legacy. Please note however that the legacy option will be deprecated in a future release and will eventually be removed. For more information, see our blog post where we announced the change.

<auto-id-policy>
  default
</auto-id-policy>

Authentification requise

Vous devez être connecté à Google+ pour effectuer cette opération.

Connexion en cours…

Le site Google pour les développeurs a besoin de votre autorisation pour effectuer cette opération.