1 <?xml version="1.0" encoding="UTF-8"?> 2 <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 3 xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" 4 xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" 5 id="WebApp_ID" version="2.5"> 6 <display-name>Servelt_ReadingDBFromWebXml</display-name> 7 <welcome-file-list> 8 <welcome-file>index.html</welcome-file> 9 <welcome-file>index.htm</welcome-file> 10 <welcome-file>index.jsp</welcome-file> 11 <welcome-file>default.html</welcome-file> 12 <welcome-file>default.htm</welcome-file> 13 <welcome-file>default.jsp</welcome-file> 14 </welcome-file-list>
<!-- 通过getServletContext().getInitParameter("XX")读取 -->
<context-param>
<param-name>driver</param-name>
<param-value>com.mysql.jdbc.Driver</param-value>
</context-param>
<context-param>
<param-name>url</param-name>
<param-value>jdbc:mysql://localhost:3306/sample</param-value>
</context-param>
<context-param>
<param-name>username</param-name>
<param-value>root</param-value>
</context-param>
<context-param>
<param-name>password</param-name>
<param-value>admin</param-value>
</context-param>
<servlet>
<servlet-name>DBServlet</servlet-name>
<servlet-class>demo.DBServlet</servlet-class>
<!-- 通过getServletConfig().getInitParameter("XX")读取 -->
<init-param>
<param-name>driver</param-name>
<param-value>com.mysql.jdbc.Driver</param-value>
</init-param>
<init-param>
<param-name>url</param-name>
<param-value>jdbc:mysql://localhost:3306/sample</param-value>
</init-param>
<init-param>
<param-name>username</param-name>
<param-value>root</param-value>
</init-param>
<init-param>
<param-name>password</param-name>
<param-value>admin</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>DBServlet</servlet-name>
<url-pattern>/readingDB</url-pattern>
</servlet-mapping>
</web-app>
Servlet代码:
1 /** 2 * 3 */ 4 package demo; 5 6 import java.io.IOException; 7 import java.io.PrintWriter; 8 import java.sql.Connection; 9 import java.sql.DriverManager; 10 import java.sql.PreparedStatement; 11 import java.sql.ResultSet; 12 import java.sql.SQLException; 13 14 import javax.servlet.ServletException; 15 import javax.servlet.http.HttpServlet; 16 import javax.servlet.http.HttpServletRequest; 17 import javax.servlet.http.HttpServletResponse; 18 19 /** 20 * Reading DB Config from web.xml 21 */ 22 public class DBServlet extends HttpServlet { 23 24 /** 25 * 26 */ 27 private static final long serialVersionUID = 1L; 28 29 private String diverClass; 30 private String userName; 31 private String password; 32 private String url; 33 34 @Override 35 public void init() throws ServletException { 36 diverClass = /* getServletConfig(). */getServletContext().getInitParameter("driver"); 37 userName = /* getServletConfig(). */getServletContext().getInitParameter("username"); 38 password = /* getServletConfig(). */getServletContext().getInitParameter("password"); 39 url = /* getServletConfig(). */getServletContext().getInitParameter("url"); 40 41 try { 42 Class.forName(diverClass); 43 } catch (Exception e) { 44 e.printStackTrace(); 45 } 46 47 } 48 49 @Override 50 protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { 51 Connection connection = null; 52 PreparedStatement statement = null; 53 54 try { 55 connection = DriverManager.getConnection(url, userName, password); 56 statement = connection.prepareStatement("select * from customer"); 57 ResultSet rs = statement.executeQuery(); 58 59 PrintWriter printWriter = resp.getWriter(); 60 while (rs.next()) { 61 printWriter.println(rs.getString("id")); 62 printWriter.println(rs.getString("name")); 63 } 64 } catch (SQLException e) { 65 e.printStackTrace(); 66 } finally { 67 try { 68 if (statement != null) { 69 statement.close(); 70 } 71 72 if (connection != null) { 73 connection.close(); 74 } 75 } catch (SQLException e) { 76 e.printStackTrace(); 77 } 78 } 79 } 80 }
原文链接:https://www.cnblogs.com/ilahsa/archive/2012/09/29/2708460.html