<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>

                企業??AI智能體構建引擎,智能編排和調試,一鍵部署,支持知識庫和私有化部署方案 廣告
                # JSP 中的`application`隱式對象 > 原文: [https://beginnersbook.com/2013/11/jsp-implicit-object-application-with-examples/](https://beginnersbook.com/2013/11/jsp-implicit-object-application-with-examples/) `Application`隱式對象是`javax.servlet.ServletContext`的一個實例。它主要用于獲取初始化參數和共享屬性和它們在整個 JSP 應用中的值,這意味著`application`隱式對象設置的任何屬性都可用于所有 JSP 頁面。 **方法:** * `Object getAttribute(String attributeName)` * `void setAttribute(String attributeName, Object object)` * `void removeAttribute(String objectName)` * `Enumeration getAttributeNames()` * `String getInitParameter(String paramname)` * `Enumeration getInitParameterNames()` * `String getRealPath(String value)` * `void log(String message)` * `URL getResource(String value)` * `InputStream getResourceAsStream(String path)` * `String getServerInfo()` * `String getMajorVersion()` * `String getMinorVersion()` 1. `Object getAttribute(String attributeName)`:它返回存儲在給定屬性名稱中的對象。例如,下面的語句將返回存儲在屬性`"MyAttr"`中的對象。 ```html String s = (String)application.getAttribute("MyAttr"); ``` 2. `void setAttribute(String attributeName, Object object)`:它設置屬性的值,或者換句話說,它將屬性及其值存儲在應用程序上下文中,可在整個 JSP 應用程序中使用。示例 – ```html application.setAttribute(“MyAttribute”, “This is the value of Attribute”); ``` 上面的語句將存儲屬性及其值。如果我們在任何 JSP 頁面中使用以下語句,那么`'s'`的值是多少? ```html String s= (String) application.getAttribute(“MyAttribute”); ``` 字符串的值將是`"This is the value of Attribute"`,因為我們使用`setAttribute`方法設置它。 3. `void removeAttribute(String objectName)`:此方法用于從應用中刪除給定屬性。對于例如 - 它將從應用中刪除屬性`"MyAttr"`。如果我們嘗試使用`getAttribute`方法獲取已刪除屬性的值,則它將返回`Null`。 ```html application.removeAttribute(“MyAttr”); ``` 4. `Enumeration getAttributeNames()`:此方法返回存儲在`application`隱式對象中的所有屬性名稱的枚舉。 ```html Enumeration e= application.getAttributeNames(); ``` 5. `String getInitParameter(String paramname)`:它返回給定參數名稱的`Initialization`參數的值。示例`web.xml`: ```html <web-app> … <context-param> <param-name>parameter1</param-name> <param-value>ValueOfParameter1</param-value> </context-param> </web-app> ``` 假設上面是我的`web.xml`文件 ```html String s=application.getInitParameter(“parameter1”); ``` `s`的值將是`ValueOfParameter1`。仍然困惑它來自哪里?請參閱`web.xml`文件中的`param-value`標簽。 6. `Enumeration getInitParameterNames()`:它返回所有`Initialization`參數的枚舉。 ```html Enumeration e= application.getinitParameterNames(); ``` 7. `String getRealPath(String value)`:它將給定路徑轉換為文件系統中的絕對路徑。 ```html String abspath = application.getRealPath(“/index.html”); ``` `abspath`的值將是基于現有文件系統的完整 HTTP URL。 8. `void log(String message)`:此方法將給定消息寫入與該應用程序關聯的 JSP 引擎(JSP 容器)的默認日志文件。 ```html application.log(“This is error 404 Page not found”); ``` 上面的調用會將消息`"This is error 404 Page not found"`寫入默認日志文件。 9. `String getServerInfo()`:此方法返回 JSP 容器(JSP 引擎)的名稱和版本。 ```html application.getServerInfo(); ``` **`application`隱式對象示例** 一個 JSP 頁面,用于捕獲使用應用的命中數。在此示例中,我們使用`application`隱式對象計算 JSP 頁面的命中數。 `counter.jsp` ```html <%@ page import="java.io.*,java.util.*" %> <html> <head> <title>Application Implicit Object Example</title> </head> <body> <% //Comment: This would return null for the first time Integer counter= (Integer)application.getAttribute("numberOfVisits"); if( counter ==null || counter == 0 ){ //Comment: For the very first Visitor counter = 1; }else{ //Comment: For Others counter = counter+ 1; } application.setAttribute("numberOfVisits", counter); %> <h3>Total number of hits to this Page is: <%= counter%></h3> </body> </html> ``` **輸出截圖** 首次訪問者的點擊次數為 1。 ![application1](https://img.kancloud.cn/48/9a/489a85ca83fd6d15eea48a3ab6a692b7_592x285.jpg) 刷新頁面時,點擊次數增加了。 ![application2](https://img.kancloud.cn/56/f2/56f20e0763287b389bee4d9b4c8154d6_592x285.jpg) 如果您喜歡該教程,請在 Facebook 和 Google+ 上與您的朋友分享。
                  <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>

                              哎呀哎呀视频在线观看