<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                合規國際互聯網加速 OSASE為企業客戶提供高速穩定SD-WAN國際加速解決方案。 廣告
                # Java JSP 示例 > 原文: [https://javatutorial.net/java-jsp-example](https://javatutorial.net/java-jsp-example) 本示例演示如何創建一個簡單的 JSP 頁面 在之前的教程中,我向您展示了如何使用 Servlet 處理[請求](https://javatutorial.net/java-servlet-example),[響應](https://javatutorial.net/java-servlet-post-example),[請求參數](https://javatutorial.net/servlet-request-info-example)和[上傳文件](https://javatutorial.net/java-servlet-file-upload)。 但是,您可能已經注意到,使用 servlet 呈現 Web 應用程序的 HTML 內容是多么不便。 重復調用`ServletOutputStream`或`PrintWriter`類上的方法以輸出內容,并且必須將 HTML 內容放入 Java 字符串中,這需要轉義引號,這確實是一個難題。 在此示例中,您將了解 Java Server Pages 以及它們如何使您的生活更加輕松。 ## 什么是 JSP? Java EE 規范的創建者意識到,使用 servlet 生成純 HTML 內容需要付出的精力比需要的多。 純 HTML 頁面的問題在于它們是靜態的。 使用 servlet 時,我們可以生成動態內容并將其呈現為 HTML。 JavaServer Pages(也稱為 JSP)是解決此問題的方法。 JSP 本質上是一種混合解決方案,結合了 Java 代碼和 HTML 標簽。 JSP 除了 Java 代碼外,還可以包含任何 HTML 標記。 ## JSP 示例 在下面的示例中,我們將顯示一個簡單的 JSP 頁面,其中顯示了當前時間。 首先讓我們看一下項目結構: ![JSP project structure](https://img.kancloud.cn/24/ca/24ca424653296ab073a2d7f494ed1385_277x190.jpg) JSP 項目結構 如您所見,結構非常簡單。 一個 Maven POM 文件,用于處理依賴關系和構建屬性: ```java <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>net.javatutorial.tutorials</groupId> <artifactId>JSPExample</artifactId> <version>1</version> <packaging>war</packaging> <name>JSPExample</name> <url>https://javatutorial.net</url> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> </properties> <dependencies> <dependency> <groupId>javax.servlet</groupId> <artifactId>javax.servlet-api</artifactId> <version>3.1.0</version> <scope>provided</scope> </dependency> <dependency> <groupId>javax.servlet.jsp</groupId> <artifactId>javax.servlet.jsp-api</artifactId> <version>2.3.1</version> <scope>provided</scope> </dependency> </dependencies> <build> <finalName>jspexample</finalName> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-war-plugin</artifactId> <version>2.3</version> <configuration> <warSourceDirectory>src/main/webapp</warSourceDirectory> </configuration> </plugin> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>3.1</version> <configuration> <source>1.8</source> <target>1.8</target> </configuration> </plugin> </plugins> </build> </project> ``` 一個僅包含應用程序顯示名稱的簡單`web.xml` ```java <?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" version="3.1"> <display-name>JSP Example</display-name> </web-app> ``` 和實際的 JSP 文件: ```java <%@ page contentType="text/html;charset=UTF-8" language="java" %> <%@ page import="java.time.LocalDateTime" %> <!DOCTYPE html> <html> <head> <title>Simple JSP Application</title> </head> <body> <h1>Hello world!</h1> <h2>Current time is <%= LocalDateTime.now() %></h2> </body> </html> ``` 現在讓我們仔細看看`index.jsp` JSP 中可以使用幾種不同類型的標簽。 在上面的示例中,我們使用指令類型`<% @page… %>`將頁面編碼設置為 UTF-8 ```java <%@ page contentType="text/html;charset=UTF-8" %> ``` 默認情況下(如果我們的 JSP 文件中不包括此行),字符編碼將設置為 ISO-8859-1,如果我們要使用非拉丁字母的特殊字符和字母,這將很不方便。 我們使用另一個指令導入`LocalDateTime` ```java <%@ page import="java.time.LocalDateTime" %> ``` 最后,為了顯示當前時間,我們使用了一個名為表達式的特殊標簽`<%= … %>`。 表達式包含簡單的 Java 代碼,這些代碼返回可以寫到客戶端輸出的內容,并且表達式將該代碼的返回變量輸出到客戶端: ```java <h2>Current time is <%= LocalDateTime.now() %></h2> ``` 部署應用程序后,轉到`http://localhost:8080/jspexample/index.jsp` 您應該會看到與此頁面相似的頁面 ![JSP page output](https://img.kancloud.cn/85/f4/85f40da6e6f6d31edf411c046755655b_625x279.jpg) JSP 頁面輸出 您可以在我們的 [GitHub 存儲庫](https://github.com/JavaTutorialNetwork/Tutorials/tree/master/JSPExample)中找到此示例的源文件。
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看