<%-- Document : getWebParam Created on : 2020-4-11, 20:03:43 Author : Administrator --%> <%@page import="java.sql.ResultSet"%> <%@page import="java.sql.Statement"%> <%@page import="java.sql.DriverManager"%> <%@page import="java.sql.Connection"%> <%@page contentType="text/html" pageEncoding="UTF-8"%> <!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>application测试</title> </head> <body> <% //从配置参数中获取驱动 String driver = application.getInitParameter("driver"); //从配置参数中获取数据库url String url = application.getInitParameter("url"); //从配置参数中获取用户名 String user = application.getInitParameter("user"); //从配置参数中获取密码 String pass = application.getInitParameter("pass"); //注册驱动 Class.forName(driver); //获取数据库连接 Connection conn = DriverManager.getConnection(url, user, pass); //创建Statement对象 Statement stmt = conn.createStatement(); //执行查询 ResultSet rs = stmt.executeQuery("select * from person"); %> <table bgcolor="#9999dd" border="1" width="480"> <% //遍历结果集 while (rs.next()) { %> <tr> <td><%=rs.getString(1)%></td> <td><%=rs.getString(2)%></td> <td><%=rs.getString(3)%></td> </tr> <% } %> </table> </body> </html>
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" version="3.1" metadata-complete="true"> <!-- 配置第一个参数:driver --> <context-param> <param-name>driver</param-name> <param-value>com.mysql.cj.jdbc.Driver</param-value> </context-param> <!-- 配置第二个参数:url --> <context-param> <param-name>url</param-name> <param-value>jdbc:mysql://localhost:3306/taobao</param-value> </context-param> <!-- 配置第三个参数:user --> <context-param> <param-name>user</param-name> <param-value>root</param-value> </context-param> <!-- 配置第四个参数:pass --> <context-param> <param-name>pass</param-name> <param-value>admin</param-value> </context-param> <welcome-file-list> <welcome-file>index.html</welcome-file> <welcome-file>index.htm</welcome-file> <welcome-file>index.jsp</welcome-file> </welcome-file-list> </web-app>