JSP具體篇——application

來源:互聯網
上載者:User

標籤:分享   3.0   getattr   www   value   通過   pat   bsp   water   

application對象

application對象用於儲存全部應用程式中的共同擁有資料。它在server啟動時自己主動建立。在server停止時自己主動銷毀。

當application對象沒有被銷毀時,全部使用者都能夠共用該application對象。與session相比,application 對象的生命週期更長,類似於“全域變數”

1.訪問應用程式初始化參數

application提供了相應用程式初始化參數進行訪問的方法。

應用程式初始化參數在web.xml檔案裡進行設定。web.xml檔案位於Web應用所在的檔案夾下的WEB-INF子檔案夾中。在web.xml中通過<context-param>標記配置應用程式的初始化參數。

 

範例:

 

在web.xml中配置了MySQL資料庫所需的url參數,實比例如以下:

<context-param>

<param-name>url</param-name>

<param-value>jdbc:mysql:127.0.0.1:3306/db_database</param-value>

</context-param>

application對象提供了兩種方法訪問應用程式的初始化參數。分別介紹例如以下:

 

a.getInitParameter()方法:

該方法使用者返回已經命名的參數值。文法格式例如以下:

application.getInitParameter(String name);

使用此方法擷取上面web.xml檔案裡的url參數的值。可使用以下的代碼

application.getInitParameter(“url”);

 

b.getAttributeNames()方法

application.getAttributeNames()返回以定義的應用程式初始化參數名的枚舉,文法格式例如以下:

application.getAttributeNames();

範例:

web.xml檔案例如以下:

<?xml version="1.0" encoding="UTF-8"?

>

<web-app version="3.0" 

xmlns="http://java.sun.com/xml/ns/javaee" 

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 

xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 

http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">

  <display-name></display-name>

  <welcome-file-list>

    <welcome-file>index.jsp</welcome-file>

  </welcome-file-list>

  

<context-param>

<param-name>url</param-name>

<param-value>jdbc:mysql:127.0.0.1:3306/db_database</param-value>

</context-param>

 

</web-app>

 

result.jsp檔案裡取得應用程式的初始化參數。

<%@ page language="java" import="java.util.*" pageEncoding="ISO-8859-1"%>

<%@ page import="java.util.*" %>

<%

String path = request.getContextPath();

String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";

%>

 

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">

<html>

  <head>

    <base href="<%=basePath%>">

    

    <title>My JSP ‘result.jsp‘ starting page</title>

    

<meta http-equiv="pragma" content="no-cache">

<meta http-equiv="cache-control" content="no-cache">

<meta http-equiv="expires" content="0">    

<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">

<meta http-equiv="description" content="This is my page">

<!--

<link rel="stylesheet" type="text/css" href="styles.css">

-->

 

  </head>

  

  <body>

    <%

     Enumeration enumeration = application.getInitParameterNames();

     while(enumeration.hasMoreElements())

     {

     String name = (String)enumeration.nextElement();

     String value = (String)application.getInitParameter(name);

     out.println(name);

     out.println(value);

     }

     %>

  </body>

</html>

2.管理應用程式的環境屬性

application對象管理應用程式環境屬性的方法例如以下:

getAttributeNames():擷取全部application對象使用的屬性名稱

 

getAttribute(String name):從application對象中擷取指定的對象名的值

 

setAttribute(String key,Object obj):設定application對象的屬性的值

 

removeAttribute(String name):從application對象中去掉指定的名稱的屬性




JSP具體篇——application

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.