Java 8 已停止支援,並將於 2026 年 1 月 31 日
淘汰。淘汰後,您將無法部署 Java 8 應用程式,即使貴機構先前使用機構政策重新啟用舊版執行階段的部署作業也一樣。現有的 Java 8 應用程式在
淘汰日期後,仍會繼續執行並接收流量。建議您
改用最新支援的 Java 版本。
開始使用:提供靜態內容
透過集合功能整理內容
你可以依據偏好儲存及分類內容。
瞭解如何從 Google App Engine 標準環境安全地提供靜態內容,例如 HTML 檔案、CSS 和圖片。
事前準備
設定開發環境及建立 App Engine 專案。
如果您的網站使用自訂網域名稱,請按照操作說明將自訂網域新增至您的專案。
提供網頁
App Engine 可提供靜態內容,例如 HTML 頁面和圖片等媒體。靜態內容是指不會以 JSP 或 Servlet 執行的任何內容。
以下範例是一個顯示歡迎訊息的基本 HTML 頁面。
<!DOCTYPE html>
<html>
<head>
<title>The App Engine Blog</title>
</head>
<body>
<h1>Welcome to the App Engine Blog</h1>
<p>This is being served by App Engine!</p>
</body>
</html>
靜態檔案的放置位置
您必須將提供的靜態檔案放在應用程式的 webapp
目錄中。您可以使用資料夾,但請注意,所有檔案路徑和 URI 都會相對於 webapp
目錄。
選擇靜態檔案的位置後,您必須使用 <static-files>
元素,在 appengine-web.xml
檔案中定義檔案位置。
在下方範例中,基本 appengine-web.xml
設定會將 webapp
目錄中的所有 HTML 檔案視為靜態檔案。
<appengine-web-app xmlns="http://appengine.google.com/ns/1.0">
<threadsafe>true</threadsafe>
<runtime>java8</runtime>
<static-files>
<include path="/**.html" >
</include>
</static-files>
</appengine-web-app>
您可以有多個 <include path>
元素,參照不同的目錄和不同檔案類型。請展開上一個範例:
<static-files>
<include path="/**.html" >
</include>
<include path="/images/**.jpg" >
</include>
</static-files>
從現在起,webapp/images/
目錄中所有副檔名為 .jpg
的檔案都會視為靜態檔案。
在上例中,如果我們想顯示 webapp/images
目錄中的 logo.jpg
,<img>
標記就會包含來源 URI <img src="/images/logo.jpg">
。
強制所有靜態內容採用 HTTPS
雖然 App Engine 支援透過 HTTP 或 HTTPS 提供內容,但建議您使用 HTTPS。為了設定安全網址,您必須將 <security-constraint>
元素新增至專案的 web.xml
。以下是 <security-constraint>
範例:
<security-constraint>
<web-resource-collection>
<web-resource-name>blog</web-resource-name>
<url-pattern>/*</url-pattern>
</web-resource-collection>
<user-data-constraint>
<transport-guarantee>CONFIDENTIAL</transport-guarantee>
</user-data-constraint>
</security-constraint>
使用 <transport-guarantee>CONFIDENTIAL</transport-guarantee>
後,所有要求都會自動重新導向至靜態內容的 HTTPS URI。
部署至 App Engine
您可以透過 Maven 將應用程式部署至 App Engine。
請前往專案的根目錄,並輸入以下內容:
mvn package appengine:deploy -Dapp.deploy.projectId=PROJECT_ID
將 PROJECT_ID 替換為您的 Google Cloud 專案 ID。如果 pom.xml
檔案已指定專案 ID,您就不需要在執行的指令中加入 -Dapp.deploy.projectId
屬性。
請在 Maven 部署應用程式後輸入下列指令,以便在新的應用程式中自動開啟網路瀏覽器分頁:
gcloud app browse
後續步驟
您可以使用靜態檔案,透過 App Engine 提供圖片、階層式樣式表和靜態 HTML 內容。如要拓展相關知識,請參閱利用 HTML 表單處理使用者資料。
除非另有註明,否則本頁面中的內容是採用創用 CC 姓名標示 4.0 授權,程式碼範例則為阿帕契 2.0 授權。詳情請參閱《Google Developers 網站政策》。Java 是 Oracle 和/或其關聯企業的註冊商標。
上次更新時間:2025-09-04 (世界標準時間)。
[[["容易理解","easyToUnderstand","thumb-up"],["確實解決了我的問題","solvedMyProblem","thumb-up"],["其他","otherUp","thumb-up"]],[["難以理解","hardToUnderstand","thumb-down"],["資訊或程式碼範例有誤","incorrectInformationOrSampleCode","thumb-down"],["缺少我需要的資訊/範例","missingTheInformationSamplesINeed","thumb-down"],["翻譯問題","translationIssue","thumb-down"],["其他","otherDown","thumb-down"]],["上次更新時間:2025-09-04 (世界標準時間)。"],[[["\u003cp\u003eGoogle App Engine standard environment can serve static content like HTML, CSS, and images, which are files that won't be executed as JSPs or Servlets.\u003c/p\u003e\n"],["\u003cp\u003eStatic files must be placed within the app's \u003ccode\u003ewebapp\u003c/code\u003e directory, and their locations must be defined in the \u003ccode\u003eappengine-web.xml\u003c/code\u003e file using the \u003ccode\u003e<static-files>\u003c/code\u003e element with \u003ccode\u003e<include path>\u003c/code\u003e directives.\u003c/p\u003e\n"],["\u003cp\u003eTo ensure secure serving of static content, use HTTPS by adding a \u003ccode\u003e<security-constraint>\u003c/code\u003e element with \u003ccode\u003e<transport-guarantee>CONFIDENTIAL</transport-guarantee>\u003c/code\u003e to the \u003ccode\u003eweb.xml\u003c/code\u003e file, which redirects all requests to HTTPS.\u003c/p\u003e\n"],["\u003cp\u003eDeploying the application to App Engine can be done using Maven with the command \u003ccode\u003emvn package appengine:deploy -Dapp.deploy.projectId=PROJECT_ID\u003c/code\u003e, replacing \u003ccode\u003ePROJECT_ID\u003c/code\u003e with your Google Cloud project ID.\u003c/p\u003e\n"],["\u003cp\u003eAfter deploying with Maven, the deployed app can be viewed by using the \u003ccode\u003egcloud app browse\u003c/code\u003e command to open a web browser automatically.\u003c/p\u003e\n"]]],[],null,["# Getting Started: Serving Static Content\n\nLearn how to securely serve static content such as HTML files, CSS, and images\nfrom Google App Engine standard environment.\n\nBefore you begin\n----------------\n\n1. [Configure your development environment and create your App Engine project](/appengine/docs/legacy/standard/java/building-app/environment-setup).\n\n2. If you are using a custom domain name for your website, follow the\n instructions for adding a\n [Custom Domain](/appengine/docs/legacy/standard/java/mapping-custom-domains)\n to your project.\n\nServing a web page\n------------------\n\nApp Engine can serve static content such as HTML pages and media such as images.\nStatic content is anything that will not be executed as JSPs or Servlets.\n\nThe following example is a basic HTML page that shows a welcome message. \n\n \u003c!DOCTYPE html\u003e\n \u003chtml\u003e\n \u003chead\u003e\n \u003ctitle\u003eThe App Engine Blog\u003c/title\u003e\n \u003c/head\u003e\n \u003cbody\u003e\n \u003ch1\u003eWelcome to the App Engine Blog\u003c/h1\u003e\n \u003cp\u003eThis is being served by App Engine!\u003c/p\u003e\n \u003c/body\u003e\n \u003c/html\u003e\n\nWhere to put your static files\n------------------------------\n\nYou must put your static served files within your app's `webapp` directory. You\ncan use folders, but remember that all file paths and URIs are relative to the\n`webapp` directory.\n\nAfter you choose a location for the static files, you must define their location\nin the `appengine-web.xml` file, using the\n[`\u003cstatic-files\u003e`](/appengine/docs/legacy/standard/java/config/appref#static_files)\nelement.\n\nIn the example below, a basic `appengine-web.xml` configuration treats all HTML\nfiles in the `webapp` directory as static files. \n\n \u003cappengine-web-app xmlns=\"http://appengine.google.com/ns/1.0\"\u003e\n \u003cthreadsafe\u003etrue\u003c/threadsafe\u003e\n \u003cruntime\u003ejava8\u003c/runtime\u003e\n \u003cstatic-files\u003e\n \u003cinclude path=\"/**.html\" \u003e\n \u003c/include\u003e\n \u003c/static-files\u003e\n \u003c/appengine-web-app\u003e\n\nYou can have multiple `\u003cinclude path\u003e` elements that reference different\ndirectories and different filetypes. Expanding on the previous example: \n\n \u003cstatic-files\u003e\n \u003cinclude path=\"/**.html\" \u003e\n \u003c/include\u003e\n \u003cinclude path=\"/images/**.jpg\" \u003e\n \u003c/include\u003e\n \u003c/static-files\u003e\n\nNow all files with the `.jpg` extension in the `webapp/images/` directory will\nbe treated as static files.\n\nIn the example above, if we wanted to display `logo.jpg` from the\n`webapp/images` directory, the `\u003cimg\u003e` tag would have the source URI\n`\u003cimg src=\"/images/logo.jpg\"\u003e`.\n\nForcing HTTPS for all static content\n------------------------------------\n\nAlthough App Engine supports serving content using either HTTP or HTTPS,\nyou should use HTTPS. In order to set up\n[secure URLs](/appengine/docs/legacy/standard/java/config/webxml#Secure_URLs),\nyou must add a `\u003csecurity-constraint\u003e` element to your project's\n`web.xml`. A sample `\u003csecurity-constraint\u003e` is shown here: \n\n \u003csecurity-constraint\u003e\n \u003cweb-resource-collection\u003e\n \u003cweb-resource-name\u003eblog\u003c/web-resource-name\u003e\n \u003curl-pattern\u003e/*\u003c/url-pattern\u003e\n \u003c/web-resource-collection\u003e\n \u003cuser-data-constraint\u003e\n \u003ctransport-guarantee\u003eCONFIDENTIAL\u003c/transport-guarantee\u003e\n \u003c/user-data-constraint\u003e\n \u003c/security-constraint\u003e\n\nBy using `\u003ctransport-guarantee\u003eCONFIDENTIAL\u003c/transport-guarantee\u003e`, all requests\nare automatically redirected to the HTTPS URI of the static content.\n\nDeploying to App Engine\n-----------------------\n\nYou can deploy your app to App Engine using Maven.\n\nGo to the root directory of your project and type: \n\n```\nmvn package appengine:deploy -Dapp.deploy.projectId=PROJECT_ID\n```\n\nReplace \u003cvar translate=\"no\"\u003ePROJECT_ID\u003c/var\u003e with the ID of your Google Cloud project. If\nyour `pom.xml` file already\n[specifies your\nproject ID](/appengine/docs/legacy/standard/java/maven-reference#appenginedeploy), you don't need to include the `-Dapp.deploy.projectId` property in the\ncommand you run.\n\nAfter Maven deploys your app, open a web browser tab automatically\nat your new app by typing: \n\n gcloud app browse\n\nWhat's next\n-----------\n\nStatic files can be used to serve images, Cascading Style Sheets and static\nHTML content through App Engine. To extend your knowledge, you might want to\nlook at [handling user data through HTML forms](/appengine/docs/legacy/standard/java/building-app/handling-form-data)."]]