/* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ import javax.servlet.http.*; import javax.servlet.*; import javax.servlet.annotation.*; import java.io.*; import java.sql.*; /** * * @author Administrator */ @WebServlet(name = "testServlet", urlPatterns = {"/testServlet"}, initParams = { @WebInitParam(name = "driver", value = "com.mysql.cj.jdbc.Driver") , @WebInitParam(name = "url", value = "jdbc:mysql://localhost:3306/taobao") , @WebInitParam(name = "user", value = "root") , @WebInitParam(name = "pass", value = "admin")}) public class TestServlet extends HttpServlet { // 重写init方法 public void init(ServletConfig config) throws ServletException { // 重写该方法,应该首先调用父类的init()方法 super.init(config); } // 响应客户端请求的方法 public void service(HttpServletRequest request,HttpServletResponse response) throws ServletException, java.io.IOException { try { // 获取ServletConfig对象 ServletConfig config = getServletConfig(); // 通过ServletConfig对象获取配置参数:dirver String driver = config.getInitParameter("driver"); // 通过ServletConfig对象获取配置参数:url String url = config.getInitParameter("url"); // 通过ServletConfig对象获取配置参数:user String user = config.getInitParameter("user"); // 通过ServletConfig对象获取配置参数:pass String pass = config.getInitParameter("pass"); // 注册驱动 Class.forName(driver); // 获取数据库连接 Connection conn = DriverManager.getConnection(url, user, pass); // 创建Statement对象 Statement stmt = conn.createStatement(); // 执行查询,获取ResuletSet对象 ResultSet rs = stmt.executeQuery("select * from person"); response.setContentType("text/html;charSet=UTF-8"); // 获取页面输出流 PrintStream out = new PrintStream(response.getOutputStream()); // 输出HTML标签 out.println("<html>"); out.println("<head>"); out.println("<title>访问Servlet初始化参数测试</title>"); out.println("</head>"); out.println("<body>"); out.println("<table bgcolor="#9999dd" border="1"" + "width="480">"); // 遍历结果集 while (rs.next()) { // 输出结果集内容 out.println("<tr>"); out.println("<td>" + rs.getInt(1) + "</td>"); out.println("<td>" + rs.getString(2) + "</td>"); out.println("<td>" + rs.getInt(3) + "</td>"); out.println("</tr>"); } out.println("</table>"); out.println("</body>"); out.println("</html>"); } catch (Exception e) { e.printStackTrace(); } } }
<?xml version="1.0" encoding="UTF-8"?> <web-app metadata-complete="true" version="3.1" 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"> <!-- 配置Servlet的名字 --> <servlet> <!-- 指定Servlet的名字, 相当于指定@WebServlet的name属性 --> <servlet-name>firstServlet</servlet-name> <!-- 指定Servlet的实现类 --> <servlet-class>FirstServlet</servlet-class> </servlet> <!-- 配置Servlet的URL --> <servlet-mapping> <!-- 指定Servlet的名字 --> <servlet-name>firstServlet</servlet-name> <!-- 指定Servlet映射的URL地址, 相当于指定@WebServlet的urlPatterns属性--> <url-pattern>/aa</url-pattern> </servlet-mapping> <servlet> <!-- Servlet名 --> <servlet-name>timerServlet</servlet-name> <!-- Servlet的实现类 --> <servlet-class>TimerServlet</servlet-class> <!-- 配置应用启动时,创建Servlet实例 ,相当于指定@WebServlet的loadOnStartup属性--> <load-on-startup>1</load-on-startup> </servlet> <servlet> <!-- 配置Servlet名 --> <servlet-name>testServlet</servlet-name> <!-- 指定Servlet的实现类 --> <servlet-class>TestServlet</servlet-class> <!-- 配置Servlet的初始化参数:driver --> <init-param> <param-name>driver</param-name> <param-value>com.mysql.cj.jdbc.Driver</param-value> </init-param> <!-- 配置Servlet的初始化参数:url --> <init-param> <param-name>url</param-name> <param-value>jdbc:mysql://localhost:3306/taobao</param-value> </init-param> <!-- 配置Servlet的初始化参数:user --> <init-param> <param-name>user</param-name> <param-value>root</param-value> </init-param> <!-- 配置Servlet的初始化参数:pass --> <init-param> <param-name>pass</param-name> <param-value>admin</param-value> </init-param> </servlet> <servlet-mapping> <!-- 确定Servlet名 --> <servlet-name>testServlet</servlet-name> <!-- 配置Servlet映射的URL --> <url-pattern>/testServlet</url-pattern> </servlet-mapping> <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>