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