Servlet 读取web.xml文件中的配置参数连接数据库
web.xml中数据库连接配置:
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
id="WebApp_ID" version="2.5">
<display-name>Servelt_ReadingDBFromWebXml</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</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代码:
/**
*
*/
package demo;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* Reading DB Config from web.xml
*/
public class DBServlet extends HttpServlet {
/**
*
*/
private static final long serialVersionUID = 1L;
private String diverClass;
private String userName;
private String password;
private String url;
@Override
public void init() throws ServletException {
diverClass = /* getServletConfig(). */getServletContext().getInitParameter("driver");
userName = /* getServletConfig(). */getServletContext().getInitParameter("username");
password = /* getServletConfig(). */getServletContext().getInitParameter("password");
url = /* getServletConfig(). */getServletContext().getInitParameter("url");
try {
Class.forName(diverClass);
} catch (Exception e) {
e.printStackTrace();
}
}
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
Connection connection = null;
PreparedStatement statement = null;
try {
connection = DriverManager.getConnection(url, userName, password);
statement = connection.prepareStatement("select * from customer");
ResultSet rs = statement.executeQuery();
PrintWriter printWriter = resp.getWriter();
while (rs.next()) {
printWriter.println(rs.getString("id"));
printWriter.println(rs.getString("name"));
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
try {
if (statement != null) {
statement.close();
}
if (connection != null) {
connection.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}