首先在web.xml文件配置节点
<servlet>
<servlet-name>testServlet</servlet-name>
<servlet-class>com.yd.etc.TestServlet</servlet-class>
<!-- 配置Servlet的初始化参数:driver -->
<init-param>
<param-name>driver</param-name>
<param-value>com.mysql.jdbc.Driver</param-value>
</init-param>
<!-- 配置Servlet的初始化参数:url -->
<init-param>
<param-name>url</param-name>
<param-value>jdbc:mysql://localhost:3306/test</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>123456</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>testServlet</servlet-name>
<url-pattern>/testServlet</url-pattern>
</servlet-mapping>
再通过TestServlet来连接数据库
TestServlet.java 代码:
package com.yd.etc;
import java.io.PrintStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
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 user");
response.setContentType("text/html;charSet=gbk");
// 获取页面输出流
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.getString(1) + "</td>");
out.println("<td>" + rs.getString(2) + "</td>");
out.println("</tr>");
}
out.println("</table>");
out.println("</body>");
out.println("</html>");
} catch (Exception e) {
e.printStackTrace();
}
}
}
还有种方法直接在TestServlet 类上面注解配置
@WebServlet(name="testServlet"
, urlPatterns={"/testServlet"}
, initParams={
@WebInitParam(name="driver", value="com.mysql.jdbc.Driver"),
@WebInitParam(name="url", value="jdbc:mysql://localhost:3306/test"),
@WebInitParam(name="user", value="root"),
@WebInitParam(name="pass", value="123456")})
结果和在web.xml一样;
至于效果的怎么样,期待各位一起学习