隨筆-0  評論-0  文章-16  trackbacks-0
          Application 提供了對應用程序環境屬性訪問的方法。

          在WEB-INF目錄下面新建web.xml文件
          <?xml version="1.0" encoding="UTF-8"?>
          <web-app version="2.4"
           xmlns=" xmlns:xsi=" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
            <context-param>    <!-- 定義連接數據庫URL -->
            <param-name>url</param-name>
            <param-value>jdbc:mysql://localhost:3306/db_database15</param-value>
           </context-param>
           <context-param>   <!-- 定義連接數據庫用戶名 -->
            <param-name>name</param-name>
            <param-value>root</param-value>
           </context-param>
           <context-param>   <!-- 定義連接數據庫mim -->
            <param-name>password</param-name>
            <param-value>111</param-value>
           </context-param>
            <welcome-file-list>
              <welcome-file>index.jsp</welcome-file>
            </welcome-file-list>
          </web-app>

          t9.jsp代碼如下:

          <%@ page language="java" contentType="text/html; charset=gbk"
              pageEncoding="gbk"%>
          <%
          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>
          <meta http-equiv="Content-Type" content="text/html; charset=gbk">
          <title>Insert title here</title>
          <base href="<%=basePath%>">
          </head>
          <body>
          <%
           
             String url = application.getInitParameter("url"); //獲取初始化參數,與web.xml文件中內容對應
             String name = application.getInitParameter("name");
             String password = application.getInitParameter("password");
             out.println("URL: "+url+"<br>");
             out.println("name: "+name+"<br>");
             out.println("password: "+password+"<br>");
          %>
          </body>
          </html>



          主站蜘蛛池模板: 册亨县| 天等县| 丹巴县| 阳泉市| 博客| 宜城市| 景东| 盐城市| 四川省| 修文县| 财经| 容城县| 鄂伦春自治旗| 同仁县| 丰台区| 应用必备| 建阳市| 凌海市| 昌平区| 都江堰市| 鹤壁市| 抚顺市| 云林县| 上饶县| 丹阳市| 大理市| 惠安县| 三原县| 涟水县| 册亨县| 澳门| 凤冈县| 会泽县| 富锦市| 太白县| 九龙城区| 青冈县| 龙游县| 新沂市| 乌拉特前旗| 永川市|